You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/docs/content/library-authors.mdx
+33-19Lines changed: 33 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -61,13 +61,27 @@ To support Zod 4, update the minimum version for your `"zod"` peer dependency to
61
61
}
62
62
```
63
63
64
-
Starting with `v3.25.0`, Zod 4 is available at a `/v4` subpath.
64
+
Starting with `v3.25.0`, the Zod 4 core package is available at a `/v4/core` subpath. Read the [Versioning in Zod 4](https://github.com/colinhacks/zod/issues/4371) writeup for full context on this versioning approach.
65
65
66
66
```ts
67
67
import*asz4from"zod/v4/core";
68
68
```
69
69
70
-
Library code should *not* import from the package root (`"zod"`)! Instead, import from the version-specific subpaths: `"zod/v3"` and `"zod/v4/core"`. This way, your code is future-proofed against major version bumps down the line.
-`"zod"` — ❌ In 3.x releases, this exports Zod 3. In a [future 4.x release](https://github.com/colinhacks/zod/issues/4371), this will export Zod 4. Use the permalinks instead.
78
+
-`"zod/v4"`— ❌ This subpath is the home of Zod 4 "Classic". If you reference classes from the standard module, your library will not work with Zod Mini. This is extremely discouraged. Use `"zod/v4/core"` instead, which exports the `$`-prefixed subclasses that are extended by Zod Classic and Zod Mini. The internals of the classic & mini subclasses are identical; they only differ in which helper methods they implement.
79
+
80
+
## Do I need to publish a new major version?
81
+
82
+
No, you should not need to publish a new major version of your library to support Zod 4 (unless you are dropping support for Zod 3, which isn't recommended).
83
+
84
+
You will need to bump your peer dependency to `^3.25.0`, thus your users will need to `npm upgrade zod`. But there were no breaking changes made to Zod 3 between `[email protected]` and `[email protected]`; in fact, there were no code changes whatsoever. As code changes will be required on the part of your users, I do not believe this constitutes a breaking change. I recommend against publishing a new major version.
71
85
72
86
## How to support Zod 3 and Zod 4 simultaneously?
73
87
@@ -88,7 +102,7 @@ To differentiate between Zod 3 and Zod 4 schemas at runtime, check for the `"_zo
88
102
89
103
```ts
90
104
importtype*asz3from"zod/v3";
91
-
importtype*asv4from"zod/v4/core";
105
+
importtype*asz4from"zod/v4/core";
92
106
93
107
declareconst schema:z3.ZodTypeAny|v4.$ZodType;
94
108
@@ -101,15 +115,15 @@ if ("_zod" in schema) {
101
115
102
116
## How to support Zod and Zod Mini simultaneously?
103
117
104
-
Your library code should only import from `zod/v4/core`. This sub-package defines the interfaces, classes, and utilities that are shared between `zod/v4` and `zod/v4-mini`.
118
+
Your library code should only import from `"zod/v4/core"`. This sub-package defines the interfaces, classes, and utilities that are shared between `zod/v4` and `zod/v4-mini`.
@@ -165,39 +179,39 @@ Accepting user-defined schemas is the a fundamental operation for any library bu
165
179
When starting out, it may be tempting to write a function that accepts a Zod schema like this:
166
180
167
181
```ts
168
-
import*aszfrom"zod/v4";
182
+
import*asz4from"zod/v4/core";
169
183
170
-
function inferSchema<T>(schema:z.core.$ZodType<T>) {
184
+
function inferSchema<T>(schema:z4.$ZodType<T>) {
171
185
returnschema;
172
186
}
173
187
```
174
188
175
-
This approach is incorrect, and limits TypeScript's ability to properly infer the argument. No matter what you pass in, the type of `schema` will be an instance of `ZodType`.
189
+
This approach is incorrect, and limits TypeScript's ability to properly infer the argument. No matter what you pass in, the type of `schema` will be an instance of `$ZodType`.
176
190
177
191
```ts
178
192
inferSchema(z.string());
179
-
// => z.core.$ZodType<string>
193
+
// => $ZodType<string>
180
194
```
181
195
182
196
This approach loses type information, namely _which subclass_ the input actually is (in this case, `ZodString`). That means you can't call any string-specific methods like `.min()` on the result of `inferSchema`. Instead, your generic parameter should extend the core Zod schema interface:
183
197
184
198
```ts
185
-
function inferSchema<Textendsz.core.$ZodType>(schema:T) {
199
+
function inferSchema<Textendsz4.$ZodType>(schema:T) {
186
200
returnschema;
187
201
}
188
202
189
203
inferSchema(z.string());
190
-
// => ZodString
204
+
// => ZodString ✅
191
205
```
192
206
193
207
To constrain the input schema to a specific subclass:
194
208
195
209
```ts
196
210
197
-
import*aszfrom"zod/v4";
211
+
import*asz4from"zod/v4/core";
198
212
199
213
// only accepts object schemas
200
-
function inferSchema<T>(schema:z.core.$ZodObject) {
214
+
function inferSchema<T>(schema:z4.$ZodObject) {
201
215
returnschema;
202
216
}
203
217
```
@@ -206,10 +220,10 @@ To constrain the inferred output type of the input schema:
206
220
207
221
```ts
208
222
209
-
import*aszfrom"zod/v4";
223
+
import*asz4from"zod/v4/core";
210
224
211
225
// only accepts string schemas
212
-
function inferSchema<Textendsz.core.$ZodType<string>>(schema:T) {
226
+
function inferSchema<Textendsz4.$ZodType<string>>(schema:T) {
213
227
returnschema;
214
228
}
215
229
@@ -220,10 +234,10 @@ inferSchema(z.number());
220
234
// // Type 'number' is not assignable to type 'string'
221
235
```
222
236
223
-
To parse data with the schema, use the top-level `z.parse`/`z.safeParse`/`z.parseAsync`/`z.safeParseAsync` functions. The `z.core.$ZodType` subclass has no methods on it. The usual parsing methods are implemented by Zod and Zod Mini, but are not available in Zod Core.
237
+
To parse data with the schema, use the top-level `z4.parse`/`z4.safeParse`/`z4.parseAsync`/`z4.safeParseAsync` functions. The `z4.$ZodType` subclass has no methods on it. The usual parsing methods are implemented by Zod and Zod Mini, but are not available in Zod Core.
224
238
225
239
```ts
226
-
function parseData<Textendsz.core.$ZodType>(data:unknown, schema:T):z.output<T> {
240
+
function parseData<Textendsz4.$ZodType>(data:unknown, schema:T):z4.output<T> {
0 commit comments