-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Upgrades zod from v3 to v4 #4954
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
Open
aaronchilcott
wants to merge
8
commits into
drizzle-team:main
from
aaronchilcott:4570-Upgrade-Zod-from-v3-to-v4
Open
Upgrades zod from v3 to v4 #4954
aaronchilcott
wants to merge
8
commits into
drizzle-team:main
from
aaronchilcott:4570-Upgrade-Zod-from-v3-to-v4
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Zod v3 to v4 Migration analysis - Pre-existing version: `zod@^3.20.2` (devDependency) - Files using Zod: 19 files - No problematic patterns detected (no `invalid_type_error`, `ZodError.errors`, `ctx.path`, etc.) - Main patterns: `.optional()`, `.default()`, `.strict()`, `.merge()`, standard schemas ## Migration Steps ### 1: Upgrade to Zod v4 with v3 compatibility - Install `zod@^4.0.0` in drizzle-kit (v4.1.12 installed) - Run tests to ensure no immediate breakage (found type error due to @hono/zod-validator) ### Step 2: Update imports to use v3 subpath temporarily - Update all Zod imports from `'zod'` to `'zod/v3'` (19 files updated) - Run tests to verify everything still works **Issue Found:** @hono/zod-validator v0.2.1 doesn't support Zod v4 yet (issue drizzle-team#1148 in honojs/middleware). The type definitions from 'zod/v3' are not compatible with @hono/zod-validator's expected types, even though they should be runtime-compatible. **Solution Applied:** - Added type assertion (`as any`) to schema in studio.ts:517 for @hono/zod-validator compatibility - Added explicit type annotation for column parameter in studio.ts:648 - TypeScript compilation passes
#### Files migrated (19 total): - src/snapshotsDiffer.ts - src/serializer/studio.ts - src/schemaValidator.ts - src/cli/validations/sqlite.ts - src/cli/commands/utils.ts - src/serializer/sqliteSchema.ts - src/serializer/singlestoreSchema.ts - src/serializer/pgSchema.ts - src/serializer/mysqlSchema.ts - src/cli/validations/studio.ts (⚠️ intersection fix applied) - src/cli/validations/singlestore.ts - src/cli/validations/postgres.ts - src/cli/validations/mysql.ts - src/cli/validations/libsql.ts - src/cli/validations/common.ts - src/cli/validations/cli.ts - src/cli/commands/singlestorePushUtils.ts - src/cli/commands/mysqlPushUtils.ts - src/cli/commands/migrate.ts #### For each file: 1. Change import from `'zod/v3'` to `'zod'` (or `'zod/v4'`) 2. Ensure tests are passing
Skips modernization of `.strict()` since .strict() still works and there's no mention of performance impact, and we have 115 occurrences, I recommend we skip modernizing .strict() for now. Here's why: 1. Still functional - .strict() works in v4 2. Large scope - 115 occurrences would require significant refactoring 3. Risk vs reward - No performance benefit mentioned 4. Backward compatibility - Easier to maintain 5. Rewrite of drizzle-kit in drizzle-team#4439 may mean this kind of modernization is thrown away in later releases
The following impacts have been addressed to support the migration to zod v4 for the packages: drizzle-zod and drizzle-kit # Changes **drizzle-zod** (v0.8.0) - peerDependencies: `"zod": ">=4.0.0"` - devDependencies: `"zod": "^4.1.1"` - Files: drizzle-zod/package.json:68,79 **drizzle-kit** (v0.32.0) - devDependencies: `"zod": "^4.1.12"` - Files: drizzle-kit/package.json:116 - Heavy internal usage in validation and serialization modules **drizzle-orm** (v0.40.0) - devDependencies: `"zod": "^4.0.0"` - Files: drizzle-orm/package.json:204 **integration-tests** - dependencies: `"zod": "^4.1.1"` - Files: integration-tests/package.json:79 **examples/libsql** - dependencies: `"zod": "^4.1.1"` - Files: examples/libsql/package.json:22 - Updated Dependencies: `@hono/zod-validator: ^0.4.1`, `znv: ^0.5.0` # Out of date package-lock.json regeneration These examples had zod 3.x references in their package-lock.json files from transitive dependencies. All lock files have been regenerated. Note: These examples don't directly depend on zod. The old zod 3.x references were from transitive dependencies through drizzle-kit. After regenerating lock files, they now pull the latest drizzle-kit which uses Zod 4.x. **examples/pg-proxy** - package-lock.json **examples/mysql-proxy** - package-lock.json **examples/better-sqlite3** - package-lock.json **examples/aws-lambda** - package-lock.json # Indirect references The following examples don't have explicit zod dependencies but use drizzle-kit: - **examples/cloudflare-d1** - uses drizzle-kit ^0.17.0 - **examples/postgresjs** - uses drizzle-kit 0.17.1-609a4f0 - **examples/neon-cloudflare** - needs verification - **examples/sqlite-proxy** - needs verification # Known Breaking Changes (Zod 3.x → 4.x) Reference: https://github.com/colinhacks/zod/releases/tag/v4.0.0 Key changes to watch for: - Error formatting changes - `.parse()` behavior refinements - Type inference improvements
Updates change log files for what is included in this family of commits (for drizzle-team#4570)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
This change updates the zod dependency from v3 to v4.
Primarily this impacts drizzle-kit and drizzle-zod, however, there are also indirect impacts on some of the examples in drizzle-orm.
This is PR 1 of 2 aimed at resolving #4570. This branch is purely about upgrading zod to v4 while the other branch introduces new the new functionality which will resolve #4570 entirely. This approach of two PRs is intended to simplify the review process, I feel it will be clearer for the reviewer(s) to understand the upgrade and the improvements if they are kept separate.
Changelogs
Because I wasn't certain which release version these changes would go in to, here are the changelogs:
drizzle-orm
drizzle-zod
drizzle-kit