Skip to content

Commit fc100fc

Browse files
authored
fix(zod): DateTime, Bytes, and Decimal field inference for z.input (#2685)
1 parent a43dff5 commit fc100fc

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

packages/zod/src/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ type FieldTypeZodMap = {
106106
Int: z.ZodNumber;
107107
BigInt: z.ZodBigInt;
108108
Float: z.ZodNumber;
109-
Decimal: z.ZodType<Decimal>;
109+
Decimal: z.ZodType<Decimal, Decimal>;
110110
Boolean: z.ZodBoolean;
111-
DateTime: z.ZodType<Date>;
112-
Bytes: z.ZodType<Uint8Array>;
111+
DateTime: z.ZodDate;
112+
Bytes: z.ZodType<Uint8Array, Uint8Array>;
113113
Json: JsonZodType;
114114
};
115115

packages/zod/test/factory.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,14 @@ describe('SchemaFactory - makeModelSchema', () => {
210210
expect(userSchema.safeParse({ ...validUser, metadata: { key: BigInt(1) } }).success).toBe(false);
211211
expect(userSchema.safeParse({ ...validUser, metadata: [BigInt(1)] }).success).toBe(false);
212212
});
213+
214+
it('infers correct input types for fields', () => {
215+
const _userSchema = factory.makeModelSchema('User');
216+
type UserInput = z.input<typeof _userSchema>;
217+
expectTypeOf<UserInput['birthdate']>().toEqualTypeOf<Date | null | undefined>();
218+
expectTypeOf<UserInput['balance']>().toEqualTypeOf<Decimal>();
219+
expectTypeOf<UserInput['avatar']>().toEqualTypeOf<Uint8Array | null | undefined>();
220+
});
213221
});
214222

215223
describe('string validation attributes', () => {

0 commit comments

Comments
 (0)