Installation
Install both the Zod adapter and Zod itself:Basic Usage
Import thezodValidator function and pass it a Zod schema:
Using Zod Schemas Directly
You can also pass Zod schemas directly without the adapter for simpler use cases: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 afallback helper for graceful error handling:
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 aZodError 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
- A
ValidatorAdapterthat can be used withvalidateSearch
zodValidator(options)
Advanced usage with options.
Parameters:
options.schema- The Zod schemaoptions.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 schemafallbackValue- The value to use if validation fails
- 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