-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add support for Zod v4 #4442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Zod v4 #4442
Conversation
thanks for starting this. however, we need to discuss first whether / how we want to continue with the whole adapter setup |
62bad67
to
2010d15
Compare
View your CI Pipeline Execution ↗ for commit 2010d15
☁️ Nx Cloud last updated this comment at |
More templates
@tanstack/arktype-adapter
@tanstack/directive-functions-plugin
@tanstack/eslint-plugin-router
@tanstack/history
@tanstack/react-router
@tanstack/react-router-devtools
@tanstack/react-router-with-query
@tanstack/react-start
@tanstack/react-start-client
@tanstack/react-start-plugin
@tanstack/react-start-server
@tanstack/router-cli
@tanstack/router-core
@tanstack/router-devtools
@tanstack/router-devtools-core
@tanstack/router-generator
@tanstack/router-plugin
@tanstack/router-utils
@tanstack/router-vite-plugin
@tanstack/server-functions-plugin
@tanstack/solid-router
@tanstack/solid-router-devtools
@tanstack/solid-start
@tanstack/solid-start-client
@tanstack/solid-start-plugin
@tanstack/solid-start-server
@tanstack/start-client-core
@tanstack/start-plugin-core
@tanstack/start-server-core
@tanstack/start-server-functions-client
@tanstack/start-server-functions-fetcher
@tanstack/start-server-functions-server
@tanstack/valibot-adapter
@tanstack/virtual-file-routes
@tanstack/zod-adapter
commit: |
this fails to build, can you please have a look? https://cloud.nx.app/runs/q1gPhPfKu4/task/tanstack-search-validator-adapters%3Abuild |
Build should be fixed There are a few things we should be aware of before merging it:
I believe that for the functionality we are adding, this code introduces too much complexity. Instead, I think we should document how to write a helper function for your Zod / validator. |
yes! do you want to create docs for this? |
@niba Is there a reason why we have to specify both a fallback and a default rather than just a fallback. I can't imagine a scenario where you would want a different value for your default vs your fallback. For example sort: fallback(z.enum(['oldest', 'newest']), 'oldest').default('oldest') vs sort: fallback(z.enum(['oldest', 'newest']), 'oldest') I did notice though that when a |
@michael-wolfenden They handle different cases, but it's true that you almost always use a combination of them. I also don't know of a scenario where you wouldn't want to use @schiller-manuel const schema = z.object({
page: z.number().default(1),
filter: z.string().default(''),
sort: z.enum(['newest', 'oldest', 'price']).default('newest').catch("newest"),
})
/*
Zod 3 output: {
page?: number | undefined;
filter?: string | undefined;
sort?: unknown;
}
*/
type Zod3Type = StandardSchemaV1.InferInput<typeof schema>
/*
Zod 4 output: {
page?: number | undefined;
filter?: string | undefined;
sort?: z4.core.util.Whatever | "newest" | "oldest" | "price";
} */
type Zod4Type = StandardSchemaV1.InferInput<typeof schemaZ4> I don't understand why Zod4 generates this |
absolutely! |
"react": "^19.0.0", | ||
"react-dom": "^19.0.0" | ||
}, | ||
"peerDependencies": { | ||
"zod": "^3.23.8", | ||
"zod": "^3.25.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://zod.dev/library-authors
With zod 4.0.x released last week, this line needs to be modified slightly to include version four releases.
"zod": "^3.25.0 || ^4.0.0"
This value also needs to be set for devDependencies too.
@@ -1,4 +1,5 @@ | |||
import { z } from 'zod' | |||
import * as z3 from 'zod/v3' | |||
import * as z4 from 'zod/v4' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This import should be modified slightly to include the /core
path.
import * as z4 from "zod/v4/core";
@hanneswidrig thanks for the review! Zod v4 almost supported TanStack Router without requiring an adapter. The only issue was related to type inference when using I forwarded our issue to the Zod team, and they've already fixed the problem we were facing (colinhacks/zod#4851). With this change, the export const Route = createFileRoute('/shop/products/')({
validateSearch: z.object({
page: z.number().default(1),
filter: z.string().default(''),
sort: z.enum(['newest', 'oldest', 'price']).default('newest').catch("newest"),
}),
}) I'm waiting for the new release of zod before opening a PR to update the documentation in Tanstack Router. Should we close this one? @schiller-manuel |
@niba very cool! thanks for that! yes, let's close this one and create a new one for the docs. |
fixes #4322
In Zod v4, we still need a fallback trick to prevent type loss.
Added tests for Zod v4 and the fallback case
related discussion #4092