H
Hono6mo ago
tcurdt

[solved] route param validation with zod

.delete('/foo/:id', zValidator('param', z.object({
id: z.number()
})), async (c) => {
const { id } = c.req.valid('param')
.delete('/foo/:id', zValidator('param', z.object({
id: z.number()
})), async (c) => {
const { id } = c.req.valid('param')
This is rejecting /foo/1 - shouldn't this work?
3 Replies
Nico
Nico6mo ago
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
Aditya Mathur
Aditya Mathur6mo ago
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-primitives
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
tcurdt
tcurdtOP6mo ago
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!
Want results from more Discord servers?
Add your server