Skip to content

Zod 4.0.11 #4981

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

Merged
merged 16 commits into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@biomejs/biome": "^1.9.4",
"@types/benchmark": "^2.1.5",
"@types/semver": "^7.7.0",
"@web-std/file": "^3.0.3",
"arktype": "^2.1.19",
"benchmark": "^2.1.4",
"chalk": "^5.4.1",
Expand All @@ -32,8 +33,6 @@
"mitata": "^0.1.14",
"prettier": "^3.5.3",
"recheck": "^4.5.0",
"@web-std/file": "^3.0.3",

"rolldown": "1.0.0-beta.18",
"rollup": "^4.39.0",
"semver": "^7.7.2",
Expand All @@ -47,7 +46,7 @@
"vitest": "^2.1.9",
"zod": "workspace:*",
"zod3": "npm:zod@~3.24.0",
"zshy": "^0.0.14"
"zshy": "^0.3.3"
},
"lint-staged": {
"packages/*/src/**/*.ts": [
Expand Down
12 changes: 7 additions & 5 deletions packages/bench/benchUtil.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as zNew from "zod/v4";
import * as zOld from "zod3";
import * as zodNext from "../zod/src/index.js";
import * as zod4 from "zod4";
import * as zod3 from "zod3";

export function makeSchema<T>(factory: (z: typeof zNew) => T) {
export {zod3, zod4, zodNext}
export function makeSchema<T>(factory: (z: typeof zod4) => T) {
return {
zod3: factory(zOld as any) as T,
zod4: factory(zNew as any) as T,
zod3: factory(zod3 as any) as T,
zod4: factory(zod4 as any) as T,
// zod4Ts: factory(zodNewTs as any),
};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/bench/object-moltar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const z3Schema = z3.object({
foo: z3.string(),
num: z3.number(),
bool: z3.boolean(),
}).strict()
}).strict();
})
})

const z4LibSchema = z4lib.object({
number: z4lib.number(),
Expand Down
2 changes: 1 addition & 1 deletion packages/bench/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"valibot": "^1.0.0",
"zod": "workspace:*",
"zod3": "npm:zod@~3.24.0",
"zod4": "npm:zod@3.25.68"
"zod4": "npm:zod@4.0.8"
},
"scripts": {
"bench": "tsx --conditions @zod/source index.ts"
Expand Down
31 changes: 31 additions & 0 deletions packages/bench/single-element-enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@


import { metabench } from "./metabench.js";
import { zod4, zodNext } from "./benchUtil.js";



console.log("Single-element enum benchmarking");

const z4LibSchema = zod4.enum(['a']);

const z4Schema = zodNext.enum(['a']);


const DATA = Array.from({ length: 10000 }, () => 'a');

console.log(z4Schema.parse(DATA[0]));
console.log(z4LibSchema.parse(DATA[0]));

const bench = metabench("single-element enum parse", {

zodNext() {
for(const _ of DATA) z4LibSchema.parse("a");
},
zod4(){
for(const _ of DATA) z4Schema.parse("a");
}

});

await bench.run();
35 changes: 35 additions & 0 deletions packages/bench/single-item-union.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@


import { metabench } from "./metabench.js";
import { randomString, zod4, zodNext } from "./benchUtil.js";



console.log("Single item union");

const z4LibSchema = zod4.union([
zod4.object({ value: zod4.string() })
]);

const z4Schema = zodNext.union([
zodNext.object({ value: zodNext.string() })
])


const DATA = Array.from({ length: 100 }, () => ({ value: randomString(15) }));

console.log(z4Schema.parse(DATA[0]));
console.log(z4LibSchema.parse(DATA[0]));

const bench = metabench("single item union", {

zodNext() {
z4LibSchema.parse({value:"asdfadfs"});
},
zod4(){
z4Schema.parse({value:"asdfadfs"});
}

});

await bench.run();
128 changes: 68 additions & 60 deletions packages/docs/content/api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ To validate against some common string formats:
z.email();
z.uuid();
z.url();
z.hostname();
z.emoji(); // validates a single emoji character
z.base64();
z.base64url();
Expand Down Expand Up @@ -2145,70 +2146,73 @@ schema.parse({


### `.superRefine()`

<Callout type="warn">
In Zod 4, `.superRefine()` has been deprecated in favor of `.check()`

<Accordions>
<Accordion title="View .superRefine() example">

<Tabs groupId="lib" items={["Zod", "Zod Mini"]}>
<Tab value="Zod">
```ts
const UniqueStringArray = z.array(z.string()).superRefine((val, ctx) => {
if (val.length > 3) {
ctx.addIssue({
code: "too_big",
maximum: 3,
origin: "array",
inclusive: true,
message: "Too many items 😡",
input: val,
});
}

if (val.length !== new Set(val).size) {
ctx.addIssue({
code: "custom",
message: `No duplicates allowed.`,
input: val,
});
}

The regular `.refine` API only generates a single issue with a `"custom"` error code, but `.superRefine()` makes it possible to create multiple issues using any of Zod's [internal issue types](https://github.com/colinhacks/zod/blob/main/packages/zod/src/v4/core/errors.ts).

<Tabs groupId="lib" items={["Zod", "Zod Mini"]}>
<Tab value="Zod">
```ts
const UniqueStringArray = z.array(z.string()).superRefine((val, ctx) => {
if (val.length > 3) {
ctx.addIssue({
code: "too_big",
maximum: 3,
origin: "array",
inclusive: true,
message: "Too many items 😡",
input: val,
});
```
</Tab>
<Tab value="Zod Mini">
```ts
// no equivalent, use `.check(checkFn)` instead



















```
</Tab>
</Tabs>
</Accordion>
</Accordions>
</Callout>
}

if (val.length !== new Set(val).size) {
ctx.addIssue({
code: "custom",
message: `No duplicates allowed.`,
input: val,
});
}
});


```
</Tab>
<Tab value="Zod Mini">
```ts
const UniqueStringArray = z.array(z.string()).check(
z.superRefine((val, ctx) => {
if (val.length > 3) {
ctx.addIssue({
code: "too_big",
maximum: 3,
origin: "array",
inclusive: true,
message: "Too many items 😡",
input: val,
});
}

if (val.length !== new Set(val).size) {
ctx.addIssue({
code: "custom",
message: `No duplicates allowed.`,
input: val,
});
}
})
);
```
</Tab>
</Tabs>

### `.check()`

<Callout>
**Note** — The `.check()` API is a more low-level API that's generally more complex than `.superRefine()`. It can be faster in performance-sensitive code paths, but it's also more verbose.
</Callout>

<Accordions>
<Accordion title="View example">
The `.refine()` API is syntactic sugar atop a more versatile (and verbose) API called `.check()`. You can use this API to create multiple issues in a single refinement or have full control of the generated issue objects.

<Tabs groupId="lib" items={["Zod", "Zod Mini"]}>
Expand Down Expand Up @@ -2267,8 +2271,8 @@ const UniqueStringArray = z.array(z.string()).check((ctx) => {
```
</Tab>
</Tabs>

The regular `.refine` API only generates issues with a `"custom"` error code, but `.check()` makes it possible to throw other issue types. For more information on Zod's internal issue types, read the [Error customization](./error-customization) docs.
</Accordion>
</Accordions>

## Pipes

Expand Down Expand Up @@ -2317,6 +2321,10 @@ z.parse(castToString, true); // => "true"
</Tab>
</Tabs>

<Callout type="warn">
Refinement functions should never throw. Thrown errors are not caught by Zod.
</Callout>

{/* The output type of the schema is inferred from the transform function:

<Tabs groupId="lib" items={["Zod", "Zod Mini"]}>
Expand Down
3 changes: 2 additions & 1 deletion packages/resolution/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"rootDir": "./src",
"declaration": true,
"emitDeclarationOnly": false,
"skipLibCheck": false
"skipLibCheck": false,
"types": []
},
"include": ["src/*.ts", "src/*.mts", "src/*.cts"],
}
2 changes: 1 addition & 1 deletion packages/zod/jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zod/zod",
"version": "4.0.10",
"version": "4.0.11",
"exports": {
"./package.json": "./package.json",
".": "./src/index.ts",
Expand Down
8 changes: 4 additions & 4 deletions packages/zod/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zod",
"version": "4.0.10",
"version": "4.0.11",
"type": "module",
"author": "Colin McDonnell <[email protected]>",
"description": "TypeScript-first schema declaration and validation library with static type inference",
Expand Down Expand Up @@ -41,9 +41,9 @@
"./v4/locales": "./src/v4/locales/index.ts",
"./v4/locales/*": "./src/v4/locales/*"
},
"sourceDialects": [
"@zod/source"
]
"conditions": {
"@zod/source": "src"
}
},
"exports": {
"./package.json": "./package.json",
Expand Down
1 change: 1 addition & 0 deletions packages/zod/src/v4/classic/checks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ export {
_trim as trim,
_toLowerCase as toLowerCase,
_toUpperCase as toUpperCase,
type $RefinementCtx as RefinementCtx,
} from "../core/index.js";
Loading