Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Zod 4 Support #818

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

Draft
ksinghal wants to merge 2 commits into get-convex:main
base: main
Choose a base branch
Loading
from vendpark:main
Draft

Zod 4 Support #818

ksinghal wants to merge 2 commits into get-convex:main from vendpark:main

Conversation

@ksinghal
Copy link

@ksinghal ksinghal commented Oct 15, 2025
edited by ianmacartney
Loading

Summary

  • This is an initial effort towards implementing zod4 support.
  • Add Zod v4 monolithic integration for Convex in packages/convex-helpers/server/zod4.ts.
  • Strongly typed implementation (no loose any for public APIs); lints clean.

Key Changes

  • New API surface in server/zod4.ts: zid, zBrand, zCustomQuery/Mutation/Action, withSystemFields, zodToConvex, zodToConvexFields, zodOutputToConvex, convexToZod, toConvexJS, fromConvexJS, convexCodec.
  • Package export: add ./server/zod4 subpath (types + default) in packages/convex-helpers/package.json.

Documentation

  • Expanded Zod Validation section for v4 in packages/convex-helpers/README.md (see README for usage and examples).

Tests

  • Added smoke test packages/convex-helpers/server/zod4.test.ts (parity suite to follow).

Peer Dependency

  • Update zod peer range to ^3.22.4 || ^4.1.12 (v4.1+ required).

Compatibility

  • Existing v3 helper (server/zod) remains unchanged; new v4 functionality is opt-in via ./server/zod4.

Follow-ups

  • Port full v3 parity tests to zod4.test.ts.
  • Add an end-to-end example module using server/zod4.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Fixes #558
Fixes #793

lorenzolewis and AtAFork reacted with eyes emoji
Copy link
Author

Huge credit to @panzacoder for his implementation in Zodvex, which this is heavily inspired by!!

panzacoder, natedunn, and AtAFork reacted with rocket emoji

Copy link

pkg-pr-new bot commented Oct 15, 2025

Open in StackBlitz

npm i https://pkg.pr.new/get-convex/convex-helpers@818

commit: e69aae5

ExtraArgs
>,
) {
return customFnBuilder(query, customization) as any;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Casting any here would just override the types, no? Same for zCustomMuation and zCustomAction. Seems to be the case so far in my limited testing:

Image

Nicolapps reacted with heart emoji
Copy link

@natedunn natedunn Oct 17, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Different spot but same idea?

CleanShot 2025年10月16日 at 11 49 33

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very quick update before bed: re-adding CustomBuilder as seen in the v3 version, instead of any does seem to fix the issue related to the custom functions. We might need to generally review the any usage in order to preserve type-safety. I might have some time tomorrow to take a full pass on it.

AtAFork reacted with eyes emoji
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't have enough time to complete a full pass yet. But making some progress though my own adjustments and using a bit from @panzacoder's wonderful zodvex project, mostly helping me with the zodToConvex() feature. Hopefully that is ok, let me know if you have any objection.

@@ -0,0 +1,642 @@
import { z } from "zod";
Copy link
Member

@Nicolapps Nicolapps Oct 20, 2025
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { z } from "zod";
import { z } from "zod/v4/core";

I think that this is change is necessary. With the current import, users of convex-helpers that use Zod 3 would get the /zod4 types incorrectly defined based on Zod 3 (see https://zod.dev/library-authors?id=how-to-support-zod-4). According to the same page, we need to import from zod/v4/core, unless we do something that can’t work with Zod Mini (but they strongly recommend against doing so).

Copy link

@ksinghal I have big overhaul to fix some issues I found in type-safety and that were addressed by @Nicolapps. It also introduces some further fixes provided by @panzacoder's Zodvex. Would you mind if I made some edits to this PR or should I create a fresh one since its a fairly sizable edit? I am ok either way, I just need to get v4 support shipped finally. 😅

ksinghal and AtAFork reacted with heart emoji

Copy link
Author

please go ahead and make the edits here if you like!

Copy link

panzacoder commented Oct 21, 2025
edited
Loading

@ksinghal I have big overhaul to fix some issues I found in type-safety and that were addressed by @Nicolapps. It also introduces some further fixes provided by @panzacoder's Zodvex. Would you mind if I made some edits to this PR or should I create a fresh one since its a fairly sizable edit? I am ok either way, I just need to get v4 support shipped finally. 😅

I'm also happy to contribute here. Don't mind that my code is being borrowed (obv it's OSS) but I probably would have opened a PR here already if I thought that was what the convex team wanted.

@natedunn if you've already put some work into it I'll wait for your edits, otherwise I can make some contributions later today / this week.

natedunn and AtAFork reacted with heart emoji

Copy link

natedunn commented Oct 24, 2025
edited
Loading

@panzacoder Honestly my approach was just to edit this PR fairly minimally, but I ran into a few type issues that your approach seemed to solve already. Both of you guys had solid, sizable contributions. I'm just bringing them together and patching up a few things. 🙏

Was going to push changes up today but noticed that this PR isn't set up to accept edits. Mind checking that @ksinghal? Appreciate it

AtAFork reacted with eyes emoji

Copy link
Author

Unbelievable - it seems I am unable to access the "allow maintainers to edit" box based on this discussion: https://github.com/orgs/community/discussions/5634. Can't believe that's the case so let me know if im just not looking in the right place.

Copy link

natedunn commented Oct 24, 2025
edited
Loading

@ksinghal Yeah, I was curious about how that flow would even work. Given its a few years old and there doesn't seem to be a work around, I think I should create another PR? Why I hate that is it robs you of the PR but also still doesn't allow you or @panzacoder to submit edits. Only reviews. 🙄

I could also just send you my updates through my own fork too. But again, no flow for further edits. Annoying.

Copy link

You could open a PR into this branch if you want to keep it focused here I think?

Copy link
Author

Don't worry I don't care for the credit at all, I just want to help get it working. I am happy with whatever solution works for you!

AtAFork reacted with heart emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@Nicolapps Nicolapps Nicolapps left review comments

+1 more reviewer

@natedunn natedunn natedunn left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Zid can't be in a z.array Update zod validation to support Zod 4

AltStyle によって変換されたページ (->オリジナル) /