tRPCttRPC
Powered by
RealityShiftR
tRPC•3y ago•
2 replies
RealityShift

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
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()
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)
image.png
image.png
tRPCJoin
Move Fast & Break Nothing. End-to-end typesafe APIs made easy.
5,015Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Transform Errors
Ryan GillieRRyan Gillie / ❓-help
4y ago
Zod formatting Error
Nacho EliasNNacho Elias / ❓-help
2y ago
TRPC & Zod Logging
josephJjoseph / ❓-help
3y ago