Replies: 6 comments 7 replies
-
Is it possible to make this feature support multiple validators via TypeSchema? |
Beta Was this translation helpful? Give feedback.
-
Re:questions
|
Beta Was this translation helpful? Give feedback.
-
1: read+write General: i like the feature. Curious: Did you compare this with how Tanstack Router ensures value safety? |
Beta Was this translation helpful? Give feedback.
-
I'm exploring an alternative API: adding the validation method as an option. const diceSchema = z.number().integer().min(1).max(6);
const [dice, roll] = useQueryState(
'dice',
parseAsInteger.withOptions({
validate: diceSchema.parse
})
) It might not make much difference, but passing it this way allows for a few things:
One issue with this approach is that it makes the |
Beta Was this translation helpful? Give feedback.
-
With this feature, would it be possible to pass an entire zod object to Passing an entire zod object would help with reusability because you probably use that same zod elsewhere (e.g. to validate function params). A practical example would be using one zod object to capture every param (pagination, column filters, sort, ...) for a (tanstack) table.
|
Beta Was this translation helpful? Give feedback.
-
In working on this feature, would it benefit to accept any validation library which follows the new Standard Schema spec? |
Beta Was this translation helpful? Give feedback.
-
Draft PR: #448.
There's been a few requests for using Zod to validate parsed search params, both on the server and on the client.
This makes a lot of sense since:
A proposition is to add a new method to the parsers builder API,
withValidation
, that takes in a Zod schema.parse
function:Since errors thrown in parsers result in
null
or the default value (if specified) being returned, this should work and would not actually introduce a peer dependency on Zod. It would accept any function that takes in anunknown
argument and returns the parser output type. Technically we'd only call the validator if the query value has successfully been parsed / deserialized from a string, so T would also be a suitable input type for the validator.Why not use Zod directly in place of parsers ?
Zod can behave like the
parse
method: take a string, hydrate it into a JS data type, and run some validation on top while doing so.However, it does not provide the counterpart serialization function to transform that JS data type back into a string, which is necessary on query state updates.
Also, it would require all Zod schema definitions to be rooted in
z.string().transform(s => whatever)
, which is not what existing application logic would define (schema definitions are likely to be reused or derived from domain-specific definitions).Caveats & Limitations
Validation needs to happen synchronously. This is because the parsing + validation is done in each hook's internal state initialisation, (getting the correct value from the URL on mount), which must return a value synchronously.
Questions
withValidation
compose validations or reset them? Eg:Beta Was this translation helpful? Give feedback.
All reactions