@@ -40,6 +40,27 @@ export type ParserBuilder<T> = Required<Parser<T>> &
40
40
*/
41
41
withOptions < This , Shallow > ( this : This , options : Options < Shallow > ) : This
42
42
43
+ /**
44
+ * Pass in a validation function to perform runtime checks on the parsed
45
+ * value. If the validation fails, the value will be set to `null` (or
46
+ * the default value if specified).
47
+ *
48
+ * Validation must be synchronous, and must throw an error on invalid
49
+ * inputs.
50
+ *
51
+ * Example with Zod:
52
+ * ```ts
53
+ * const [posInt, setPosInt] = useQueryState(
54
+ * 'value',
55
+ * parseAsInteger.withValidation(z.number().positive().parse)
56
+ * )
57
+ * ```
58
+ *
59
+ * @param this
60
+ * @param validate
61
+ */
62
+ withValidation < This > ( this : This , validate : ( input : unknown ) => T ) : This
63
+
43
64
/**
44
65
* Specifying a default value makes the hook state non-nullable when the
45
66
* query is missing from the URL.
@@ -119,6 +140,18 @@ export function createParser<T>(
119
140
eq : ( a , b ) => a === b ,
120
141
...parser ,
121
142
parseServerSide : parseServerSideNullable ,
143
+ withValidation ( validate ) {
144
+ return {
145
+ ...this ,
146
+ parse : ( value : string ) => {
147
+ const parsed = parser . parse ( value )
148
+ if ( parsed === null ) {
149
+ return null
150
+ }
151
+ return validate ( parsed )
152
+ }
153
+ }
154
+ } ,
122
155
withDefault ( defaultValue ) {
123
156
return {
124
157
...this ,
@@ -128,7 +161,7 @@ export function createParser<T>(
128
161
}
129
162
}
130
163
} ,
131
- withOptions ( options : Options ) {
164
+ withOptions ( options ) {
132
165
return {
133
166
...this ,
134
167
...options
0 commit comments