Skip to main content
The Zod adapter enables type-safe search parameter validation using Zod, the most popular TypeScript-first schema validation library.

Installation

Install both the Zod adapter and Zod itself:

Basic Usage

Import the zodValidator function and pass it a Zod schema:
Now your search parameters are validated and typed:

Using Zod Schemas Directly

You can also pass Zod schemas directly without the adapter for simpler use cases:
TanStack Router automatically detects Zod schemas and handles them appropriately.

Advanced Validation

Zod provides powerful validation features that work seamlessly with TanStack Router:

String Transformations

Number Validation

Date Handling

Arrays and Complex Types

Default Values

Use Zod’s .default() or .catch() methods to provide fallback values:

Fallback Helper

The Zod adapter includes a fallback helper for graceful error handling:
If parsing fails, the fallback value is used instead of throwing an error.

Input/Output Types

Control whether you want input or output types from your schema:

Type Safety

The Zod adapter provides complete type inference:

Validation in Functions

You can also use Zod validation in a function for more control:

Error Handling

When validation fails, Zod throws a ZodError with detailed information:

Real-World Example

Here’s a complete example with pagination, filtering, and sorting:

API Reference

zodValidator(schema)

Creates a validator adapter from a Zod schema. Parameters:
  • schema - A Zod schema or options object
Returns:
  • A ValidatorAdapter that can be used with validateSearch

zodValidator(options)

Advanced usage with options. Parameters:
  • options.schema - The Zod schema
  • options.input - Whether to use input or output types (default: 'input')
  • options.output - Whether to use input or output types (default: 'output')

fallback(schema, fallbackValue)

Creates a schema that returns a fallback value on validation error. Parameters:
  • schema - A Zod schema
  • fallbackValue - The value to use if validation fails
Returns:
  • A Zod pipeline that catches errors and returns the fallback

Next Steps

Valibot Adapter

Lightweight alternative to Zod

Search Params Guide

Learn more about search parameters