RealityShift
RealityShift14mo ago

Zod transform types

I have a zod type for our backend requirements that looks like this:
const ZCustomType = z.object({
nameFirst: z.string(),
nameLast: z.string(),
dateOfBirth: z.string().transform((v) => {
if (v) return dayjs(v, ['M/D/YYYY']).toDate();
else return undefined;
}),})
const ZCustomType = z.object({
nameFirst: z.string(),
nameLast: z.string(),
dateOfBirth: z.string().transform((v) => {
if (v) return dayjs(v, ['M/D/YYYY']).toDate();
else return undefined;
}),})
When I'm using that type through my code, it properly shows as a Date. When using that as a zod input on my trpc route, it's saying it's a string. I was assuming the transform isn't properly working since it's using dayjs but even just doing a straight transform to new Date() still shows as a string. Is there a way to make this work? (There are deeper problems that adjusting the type to z.date() wouldn't work because the API expects a Date but passes back 'M/D/YYYY' string)
No description
No description
1 Reply
Nick
Nick14mo ago
I'm guessing you haven't set up a tRPC transformer?

Did you find this page helpful?