Skip to content

Commit abfda7b

Browse files
committed
Update search parameters guide for zod v4
Zod v4 does not require use of the adapter and can use the schemas directly. See #4322
1 parent 3bc18d4 commit abfda7b

1 file changed

Lines changed: 27 additions & 6 deletions

File tree

docs/router/guide/search-params.md

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ For validation libraries we recommend using adapters which infer the correct `in
196196

197197
### Zod
198198

199-
An adapter is provided for [Zod](https://zod.dev/) which will pipe through the correct `input` type and `output` type
199+
An adapter is provided for [Zod](https://zod.dev/) which will pipe through the correct `input` type and `output` type.
200+
201+
For Zod v3:
200202

201203
```tsx
202204
import { zodValidator } from '@tanstack/zod-adapter'
@@ -213,13 +215,30 @@ export const Route = createFileRoute('/shop/products/')({
213215
})
214216
```
215217

216-
The important part here is the following use of `Link` no longer requires `search` params
218+
With Zod v4, you should directly use the schema in `validateSearch`:
219+
220+
```tsx
221+
import { z } from 'zod'
222+
223+
const productSearchSchema = z.object({
224+
page: z.number().default(1),
225+
filter: z.string().default(''),
226+
sort: z.enum(['newest', 'oldest', 'price']).default('newest'),
227+
})
228+
229+
export const Route = createFileRoute('/shop/products/')({
230+
// With Zod v4, we can use the schema without the adapter
231+
validateSearch: productSearchSchema,
232+
})
233+
```
234+
235+
The important part here is the following use of `Link` no longer requires `search` params:
217236

218237
```tsx
219238
<Link to="/shop/products" />
220239
```
221240

222-
However the use of `catch` here overrides the types and makes `page`, `filter` and `sort` `unknown` causing type loss. We have handled this case by providing a `fallback` generic function which retains the types but provides a `fallback` value when validation fails
241+
In Zod v3, the use of `catch` here overrides the types and makes `page`, `filter` and `sort` `unknown` causing type loss. We have handled this case by providing a `fallback` generic function which retains the types but provides a `fallback` value when validation fails:
223242

224243
```tsx
225244
import { fallback, zodValidator } from '@tanstack/zod-adapter'
@@ -240,7 +259,9 @@ export const Route = createFileRoute('/shop/products/')({
240259

241260
Therefore when navigating to this route, `search` is optional and retains the correct types.
242261

243-
While not recommended, it is also possible to configure `input` and `output` type in case the `output` type is more accurate than the `input` type
262+
In Zod v4, schemas may use `catch` instead of the fallback and will retain type inference throughout.
263+
264+
While not recommended, it is also possible to configure `input` and `output` type in case the `output` type is more accurate than the `input` type:
244265

245266
```tsx
246267
const productSearchSchema = z.object({
@@ -457,7 +478,7 @@ Now that you've learned how to read your route's search params, you'll be happy
457478

458479
The best way to update search params is to use the `search` prop on the `<Link />` component.
459480

460-
If the search for the current page shall be updated and the `from` prop is specified, the `to` prop can be omitted.
481+
If the search for the current page shall be updated and the `from` prop is specified, the `to` prop can be omitted.
461482
Here's an example:
462483

463484
```tsx title="src/routes/shop/products.tsx"
@@ -478,7 +499,7 @@ const ProductList = () => {
478499

479500
If you want to update the search params in a generic component that is rendered on multiple routes, specifying `from` can be challenging.
480501

481-
In this scenario you can set `to="."` which will give you access to loosely typed search params.
502+
In this scenario you can set `to="."` which will give you access to loosely typed search params.
482503
Here is an example that illustrates this:
483504

484505
```tsx

0 commit comments

Comments
 (0)