Can you use zod to validate params (not search params but actually params)?
I'm trying to use zod to validate my query parameters.
example URL: /app/project/$projectId
I'm trying to use zod to say that projectId is a number with the following zod schema:
const projectRouteParams = z.object({
projectId: z.number().int().nonnegative(),
});
in my createRoute I define my parseParams like so:
parseParams: params => projectRouteParams.parse(params)
When I try to go to for example "/app/project/1", I get the TanStack router error invalid type for projectId: "Expected number, received string"
It is working if I define my parseParams like this:
parseParams: ({ projectId }) => ({ projectId: Number(projectId) });
I really like to use zod for validating the type of my route params as I am already using it for my searchParams (which are working with zod already)
Any help is appreciated 🙂
2 Replies
foreign-sapphire•2y ago
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
stormy-goldOP•2y ago
I was already using it for the stringifyParams to convert the number to a string. I'll try it for the parseParams property as well. Thanks in advance!
It's working, thanks!