T
TanStack4mo ago
rival-black

Search params are automatically converted to number type.

I have a search param that is and id, for now the ids are numbers, eg 123, but they could also be uuids, which are just strings. If I type my id param as a z.string(), but pass 123 the validation fails, since it converts it to a number. How can I force it to accept it as a string? Using z.coerce changes the param to %22123%22 which I don't want.
3 Replies
sensitive-blue
sensitive-blue4mo ago
Does z.union([z.string(), z.number()]) work?
rival-black
rival-blackOP4mo ago
Yes, used a union and then a .toString() for now, not ideal but does the trick
grumpy-cyan
grumpy-cyan4mo ago
Afaik the router uses JSON.parse and JSON.serialize by default to serialize/deserialize query params There are options to override this Can read about it here: https://tanstack.com/router/latest/docs/framework/react/guide/custom-search-param-serialization but, yeah, if your search is ?meow=123 it gets parsed as 123 (number), if you have meow="123" (likely the quotes get encoded), you get "123" (string)

Did you find this page helpful?