-
-
Couldn't load subscription status.
- Fork 7
Open
Description
Here's an example:
const stringToNumber: toZod<string> = z.string().transform((val) => val.length);
const a = stringToNumber.parse('string'); // => 6
It'd be nice to know that the value passed to the schema WAS a string, but that the parsed object would return a number? Have you run into this?
My use case is that:
- All my endpoints that take dates (in queries or request bodies) hit my backend as strings.
- I'd like to .refine() the schema to make sure that they are ISO strings even
- But after parsing with zod I'd love to use Date objects again.
So this is my example:
const stringToNumber: toZod<string> = z.string().transform((val) => new Date(val));
const a = stringToNumber.parse('2022-01-01'); => // Date('2022-01-01')
CasperEngl, XanderStoffels and mustafakibar
Metadata
Metadata
Assignees
Labels
No labels