When search param is set to a stringified number ("11000"), validating or getting the param returns a number but is typed as string #430
Replies: 3 comments
-
|
Hey @maxwellwalin I know it's been a while, but I call out a (limited) solution for this issue in my thread here! I'm hoping for the ability to have more nuanced parsing behavior per usage of |
Beta Was this translation helpful? Give feedback.
-
|
@maxwellwalin @briavicenti For now you can use a preprocess using Zod import { createFileRoute } from '@tanstack/react-router';
import { z } from 'zod';
const resetPasswordSchema = z.object({
token: z.preprocess((val) => (typeof val === 'number' ? val.toString() : val), z.string().optional()),
});
export const Route = createFileRoute('/auth/reset-password/')({
validateSearch: (search) => resetPasswordSchema.parse(search),
}); |
Beta Was this translation helpful? Give feedback.
-
|
i strongly believe the default behavior is incorrect. search params are (for better or worse) strings, and auto-magically converting them to numbers or booleans is very error-prone. especially since tanstack-router offers a really nice API for typing your query string via in particular, if i specify that a parameter is an optional string, the current behavior will fail validation if the users tries to search for any string that could be parsed as a number. i really don't think this is a sensible default. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am currently navigating to a URL with the following search:
The clientId can be any string, including a stringified number like the above. The problem is that when parsing or getting this search parameter, it evaluates as a number.
I've attempted using preSearchFilters and postSearchFilters as I do not see a method of parsing/stringifying the search available, but that did not work (as I assume that is not what those options are for)!
Please let me know if this is a bug, or I am blindly missing the option that would resolve this. I appreciate the help.
Beta Was this translation helpful? Give feedback.
All reactions