tRPCttRPC
Powered by
Nacho EliasN
tRPC•2y ago•
1 reply
Nacho Elias

Zod error not being formatted

I'm using Zod for validation and whenever I get a validation error I'd like to be able to get the error message properly formatted. In the docs there's a section for this where it's sepcified that we can use the errorFormatter to achieve this. However, it's not doing anything at all. Btw, if I add a console.log within the errorFormatter nothing's being shown in the console, which is kind of weird if you ask me. However superjson is working completely fine, so I dont think its a configuration problem....

Here's my code

const t = initTRPC.context<typeof createTRPCContext>().create({
  transformer: superjson,
  errorFormatter: ({ shape, error }) => ({
    ...shape,
    data: {
      ...shape.data,
      zodError: error.cause instanceof ZodError ? error.cause.flatten() : null,
    },
  }),
});
const t = initTRPC.context<typeof createTRPCContext>().create({
  transformer: superjson,
  errorFormatter: ({ shape, error }) => ({
    ...shape,
    data: {
      ...shape.data,
      zodError: error.cause instanceof ZodError ? error.cause.flatten() : null,
    },
  }),
});


This is my validation
export const GetProfileSchema = z.object({
  id: z.string().uuid({ message: "Invalid Id" }),
});
export const GetProfileSchema = z.object({
  id: z.string().uuid({ message: "Invalid Id" }),
});


And this is what I get in my client in error.message instead of "Invalid ID"
[
  {
    "validation": "uuid",
    "code": "invalid_string",
    "message": "Invalid Id",
    "path": [
      "id"
    ]
  }
]
[
  {
    "validation": "uuid",
    "code": "invalid_string",
    "message": "Invalid Id",
    "path": [
      "id"
    ]
  }
]
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

Zod formatting Error
Nacho EliasNNacho Elias / ❓-help
2y ago
Zod Error Middleware
zomarsZzomars / ❓-help
4y ago
Usage with zod-validation-error
jlarmstJjlarmst / ❓-help
3y ago
onSuccess mutation not being called
PeformPPeform / ❓-help
17mo ago