Capítulo 11 de 24
Demonstrates giving search params sane fallback values using zod's .catch() inside validateSearch, so a route like /posts/post can be linked with a partial search object (or none) and still receive fully-typed, defaulted values (postId, color) in the loader and component. This is a code-based single-file example (src/main.tsx).
createRootRoute / createRoute / .addChildren)@tanstack/react-router, @tanstack/react-query, zod, redaxiossrc/main.tsx: fetch helpers, root/index/posts-layout/posts-index/post routes, validateSearch with zod defaults, loaderDeps tying the loader to the validated search, and the component reading useSearch().const postRoute = createRoute({
getParentRoute: () => postsLayoutRoute,
path: 'post',
validateSearch: (
input: { postId: number; color?: 'white' | 'red' | 'green' } & SearchSchemaInput,
) =>
z
.object({
postId: z.number().catch(1),
color: z.enum(['white', 'red', 'green']).catch('white'),
})
.parse(input),
loaderDeps: ({ search: { postId } }) => ({ postId }),
errorComponent: PostErrorComponent,
loader: ({ deps: { postId } }) => fetchPost(postId),
component: PostComponent,
})
function PostComponent() {
const post = postRoute.useLoaderData()
const { color } = postRoute.useSearch()
return (
<div className="space-y-2">
<h4 className="text-xl font-bold">{post.title}</h4>
<div className={`text-sm text-${color}-300`}>{post.body}</div>
</div>
)
}
z.number().catch(1) and z.enum([...]).catch('white') supply default values whenever the incoming search param is missing or fails validation, instead of throwing or requiring the caller to always pass every key..catch(defaultValue) over .optional() when a search param should always resolve to a concrete typed value in the loader/component, it removes undefined branches from downstream code.loaderDeps should mirror only the search fields the loader actually needs (postId here, not color), so navigating with a different color doesn't needlessly re-trigger the loader.search object (e.g. search={{ postId: post.id, color: index % 2 ? 'red' : undefined }}) is safe because the validator fills in the rest with defaults.SearchSchemaInput in the validator's input type documents that the raw input may be a broader/optional shape than the parsed output.validateSearch; this one focuses on defaulting/fallback behavior with plain zod, ch012 focuses on using different validation libraries (zod, valibot, arktype) via TanStack's adapter packages for the same job.