Is incremental migration to zod 4 possible? #4440
-
|
Hello, I am wondering whether we can do an incremental migration to zod 4, since we have a ton of schemas across our microservices. Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Great question! Yes, incremental migration to Zod v4 is absolutely possible and actually recommended, especially for microservices architectures with many schemas. Migration StrategyZod v4 is specifically designed to support incremental migration through its subpath versioning strategy: 1. Side-by-Side InstallationZod v4 is published alongside v3 in the same package: npm upgrade zod@^3.25.0After upgrading, you can use both versions simultaneously: // Existing v3 code continues to work
import { z } from "zod"; // or "zod/v3"
// New v4 code
import { z } from "zod/v4";2. No Breaking Changes to v3Importantly, there were no code changes between 3. Microservice-by-Microservice MigrationYou can migrate each microservice independently: // Service A (still on v3)
import { z } from "zod/v3";
const userSchema = z.object({ name: z.string() });
// Service B (migrated to v4)
import { z } from "zod/v4";
const productSchema = z.object({ name: z.string() });4. File-by-File Migration Within ServicesEven within a single codebase, you can migrate file by file: // legacy-schemas.ts (v3)
import { z } from "zod/v3";
export const legacySchema = z.string();
// new-schemas.ts (v4)
import { z } from "zod/v4";
export const newSchema = z.string();
// main.ts (mixed usage)
import { legacySchema } from "./legacy-schemas";
import { newSchema } from "./new-schemas";Migration Timeline
Library CompatibilityMost ecosystem libraries can support both v3 and v4 simultaneously without breaking changes. Library authors should:
Bundle Size BenefitsOne key advantage: you can start using Breaking Changes to ConsiderWhile migration is incremental, be aware of these key breaking changes when you do migrate specific schemas:
See the Migration Guide for the complete list. RecommendationStart with new features and gradually migrate existing code. The side-by-side approach means there's no pressure to migrate everything at once, making this perfect for large codebases and microservice architectures. The Zod team specifically designed this versioning strategy to make migration as smooth as possible for exactly your use case! |
Beta Was this translation helpful? Give feedback.
Great question! Yes, incremental migration to Zod v4 is absolutely possible and actually recommended, especially for microservices architectures with many schemas.
Migration Strategy
Zod v4 is specifically designed to support incremental migration through its subpath versioning strategy:
1. Side-by-Side Installation
Zod v4 is published alongside v3 in the same package:
After upgrading, you can use both versions simultaneously:
2. No Breaking Changes to v3
Importantly, there were no code changes between
[email protected]and[email protected]- only the addition o…