diff --git a/packages/zod/src/types.ts b/packages/zod/src/types.ts index 7715a534d..029282c23 100644 --- a/packages/zod/src/types.ts +++ b/packages/zod/src/types.ts @@ -17,8 +17,8 @@ import type { TypeDefFieldIsArray, TypeDefFieldIsOptional, } from '@zenstackhq/schema'; -import type Decimal from 'decimal.js'; import type z from 'zod'; +import type { decimalSchema, bytesSchema } from './utils'; /** * Scalar-only shape returned by the no-options `makeModelSchema` overload. @@ -106,10 +106,10 @@ type FieldTypeZodMap = { Int: z.ZodNumber; BigInt: z.ZodBigInt; Float: z.ZodNumber; - Decimal: z.ZodType; + Decimal: typeof decimalSchema; Boolean: z.ZodBoolean; - DateTime: z.ZodType; - Bytes: z.ZodType; + DateTime: z.ZodDate; + Bytes: typeof bytesSchema; Json: JsonZodType; }; diff --git a/packages/zod/src/utils.ts b/packages/zod/src/utils.ts index 9081c1c86..564fb2e50 100644 --- a/packages/zod/src/utils.ts +++ b/packages/zod/src/utils.ts @@ -12,6 +12,9 @@ import Decimal from 'decimal.js'; import { z } from 'zod'; import { SchemaFactoryError } from './error'; +export const decimalSchema = z.custom((val) => Decimal.isDecimal(val)); +export const bytesSchema = z.instanceof(Uint8Array) as z.ZodCustom, Uint8Array>; + function getArgValue(expr: Expression | undefined): T | undefined { if (!expr || !ExpressionUtils.isLiteral(expr)) { return undefined; diff --git a/packages/zod/test/factory.test.ts b/packages/zod/test/factory.test.ts index 0dd616c2d..43afddc85 100644 --- a/packages/zod/test/factory.test.ts +++ b/packages/zod/test/factory.test.ts @@ -208,6 +208,14 @@ describe('SchemaFactory - makeModelSchema', () => { expect(userSchema.safeParse({ ...validUser, metadata: { key: BigInt(1) } }).success).toBe(false); expect(userSchema.safeParse({ ...validUser, metadata: [BigInt(1)] }).success).toBe(false); }); + + it('infers correct input types for fields', () => { + const _userSchema = factory.makeModelSchema('User'); + type UserInput = z.input; + expectTypeOf().toEqualTypeOf(); + expectTypeOf().toEqualTypeOf(); + expectTypeOf().toEqualTypeOf(); + }); }); describe('string validation attributes', () => {