Proper way to type validateSearch with optional arguments for a route ? #7444
-
|
Hello to all, import { z } from "zod";
const schema = z.object({
val: z.number().default(-5),
});
const myValidator = (args: z.input<typeof schema>): z.output<typeof schema> => {
console.log("Dummy");
return schema.parse(args);
}
export const Route = createFileRoute("/optimizer")({
component: RouteComponent,
validateSearch: myValidator, // search param 'val' is seen as mandatory by typescript when using links to this route
//validateSearch: schema // search param 'val' is seen as optional by typescript when using links to this route
});
// You can check this out by placing your IDE's cursor on 'MyType' on the following expression
type MyType = (typeof Route)["types"]["fullSearchSchemaInput"]After looking at the quite heavy handed typescript metaprogramming in the source code, it seems that For the zod schema, it works, because tanstack router handles specially the case of a zod validator by checking for the presence of an attribute called ['~standard']['types']['input'] I can cheat my way by casting my function into the original zod validator but it looks kludgy. Thank you for your time, I am looking forward to any input you might have on this topic ! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
check out https://tanstack.com/router/latest/docs/api/router/RouteOptionsType#validatesearch-method
|
Beta Was this translation helpful? Give feedback.
-
|
Thanks a lot, somewhat I missed this piece of doc, it works flawlessly. Thank you for your work maintaining this project |
Beta Was this translation helpful? Give feedback.
check out https://tanstack.com/router/latest/docs/api/router/RouteOptionsType#validatesearch-method