-
-
Notifications
You must be signed in to change notification settings - Fork 764
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE]: Type Coercion in drizzle-zod
#776
Comments
If this is something you might consider, I can work on a PR. It shouldn't be too difficult to implement as what I saw |
Will be happy to contribute to this if somebody can guide me how... The issue I am facing is that RHF outputs all values as string.. there is a work around to use Right now I am making it work as below... would be good to have auto coercion setup....
|
Would love to see this too - though this refinement API seems to be pretty nice for this type of thing: export const insertListingSchema = createInsertSchema(listings, {
price: ({ price }) => z.coerce.number().min(500).pipe(price)
}); |
In order to handle date coercion, I transform the Zod schemas. This may be useful for someone: /**
* Make the following schema changes:
* - Add coercion to ZodDate's (handling optional/nullable wrappers)
*
* @param schema
* @returns
*/
export function fixType<TSchema extends z.ZodTypeAny>(schema: TSchema): TSchema {
if(schema._def.typeName === z.ZodFirstPartyTypeKind.ZodNullable) {
const s = schema as unknown as z.ZodNullable<any>;
const u = s.unwrap();
return fixType(u).nullable();
} else if(schema._def.typeName === z.ZodFirstPartyTypeKind.ZodOptional) {
const s = schema as unknown as z.ZodOptional<any>;
const u = s.unwrap();
return fixType(u).optional();
} else if(schema._def.typeName === z.ZodFirstPartyTypeKind.ZodDate) {
return z.coerce.date() as unknown as TSchema;
// return z.string().datetime({ offset:true });
} else {
return schema;
}
}
/**
* Transform Zod schema to convert Date's to use coercion to handle/parse
* string inputs.
*
* @param schema
* @returns
*/
export function transformSchema<TSchema extends z.AnyZodObject>(schema: TSchema) {
// also see https://github.com/colinhacks/zod/discussions/2050#discussioncomment-5018870
const entries = Object.entries( schema.shape ) as
[ keyof TSchema[ 'shape' ], z.ZodTypeAny ][]
const ret = schema.merge(z.object(
Object.fromEntries(
entries.map(([k,v]) => ([k,fixType(v)]))
),
));
return ret as TSchema;
} This is used as follows: const zodTable = transformSchema(createSelectSchema(table)); |
Available in |
Describe what you want
It would be nice to being able to support zod types coercion when using drizzle-zod.
A real world example below:
Consider the following table:
Later on my endpoint:
Something like the following could help writing less boilerplate code and avoid code duplication:
The text was updated successfully, but these errors were encountered: