[solved] route param validation with zod
This is rejecting
/foo/1
- shouldn't this work?3 Replies
I believe params will always be strings, I may be wrong. If that’s the case I guess that would make the param validator useless, however unless it was casted by default it would always be a string
Hey I think you will need to change your validation as the value will be coming in as string. You can use
coerce
in zod
. So you can change your validation like this { id: z.coerce.number() }
. You can check out more detail over here - https://zod.dev/?id=coercion-for-primitivesGitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
Perfect
id: z.coerce.number()
was the trick.
I assumed that the zValidator middleware would handle this by default
Would be nice if it would.
But that works, too
Thanks for the help!