T
TanStack2mo ago
vicious-gold

Handle Server function Zod Validation Errors

Question for everyone using zod schemas as validateInput how are you showing the errors into the actual UI? Because it seems like zod errors are actually serialized as string.
9 Replies
stormy-gold
stormy-gold2mo ago
do you mean inputValidator ?
vicious-gold
vicious-goldOP2mo ago
yes, using a server function inside a mutation lets say, the error we get its a "raw" string kind of, wondering whats a good DX to get the actual errors out of the functon so we can use in forms for example
stormy-gold
stormy-gold2mo ago
you can add a custom serialization adapter that serializes zod errors
vicious-gold
vicious-goldOP2mo ago
Is there any docs i can look into to add a custom serializer?
stormy-gold
stormy-gold2mo ago
unfortunately i didnt have the time yet we have an example see e2e/react-start/serialization-adapters/src/start.tsx
vicious-gold
vicious-goldOP2mo ago
This is exactly what i needed @Manuel Schiller ! Thanks !!
stormy-gold
stormy-gold2mo ago
nice when you have the adapter ready, please share
vicious-gold
vicious-goldOP2mo ago
Well, im thinking if this should be the way, like the validation on TSR still throws a 500 on validation, should probably be a 400/409. Because this triggers errors on both server and client, for a validation error humm, thinking i should probably just validate inside the business logic itself.
No description
harsh-harlequin
harsh-harlequin5w ago
@nikus I'm using a functiom middleware to catch the error, setResponseStatus, and rethrow the error.

Did you find this page helpful?