Open rawnly opened 1 year ago
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 valueAsNumber
but thats difficult to work with MUI.
Right now I am making it work as below... would be good to have auto coercion setup....
export const insertLicSchema = createInsertSchema(lic_table, {
mobile_number: z.coerce.number()
})
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));
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: