Import the arkTypeValidator function and pass it an ArkType schema:
import { createRoute } from '@tanstack/react-router'import { arkTypeValidator } from '@tanstack/arktype-adapter'import { type } from 'arktype'const invoicesRoute = createRoute({ getParentRoute: () => rootRoute, path: 'invoices', validateSearch: arkTypeValidator( type({ page: 'number', filter: 'string?', }), ),})
Now your search parameters are validated and typed:
function Invoices() { const search = invoicesRoute.useSearch() // search.page is number // search.filter is string | undefined return <div>Page {search.page}</div>}
TypeScript-like syntax - Write schemas that look like TypeScript types
Runtime and type-level - Single source of truth for types and validation
Fast - Optimized for runtime performance
Concise - Less boilerplate than other validation libraries
// This is ArkType:const user = type({ name: 'string', age: 'number', email: 'string.email',})// Looks just like TypeScript!type User = { name: string age: number email: string}
import { type } from 'arktype'arkTypeValidator( type({ positive: 'number>0', // Greater than 0 page: 'number>=1', // At least 1 percentage: 'number>=0<=100', // Between 0 and 100 integer: 'integer', // Whole numbers only positiveInt: 'integer>0', // Positive integers }),)
import { type } from 'arktype'arkTypeValidator( type({ tags: 'string[]', // Array of strings numbers: 'number[]', // Array of numbers roles: "('admin' | 'user')[]", // Array of union minItems: 'string[]>=1', // At least 1 item maxItems: 'string[]<=10', // At most 10 items }),)
ArkType allows you to transform values during parsing:
import { type } from 'arktype'arkTypeValidator( type({ // String to Date createdAt: 'string', // Then use .pipe() for transformation // String to number price: 'string.numeric', // Validates numeric string }),)
When validation fails, ArkType provides detailed error information:
import { type } from 'arktype'// This will fail if page is not a valid numberarkTypeValidator( type({ page: 'number', }),)// Errors include:// - Path to invalid field// - Expected type// - Actual value received