Skip to content

Commit 162fe29

Browse files
committed
Add z.meta and z.describe
1 parent 0f4ce73 commit 162fe29

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

packages/zod/src/v4/classic/schemas.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2142,6 +2142,10 @@ export function superRefine<T>(
21422142
return core._superRefine(fn);
21432143
}
21442144

2145+
// Re-export describe and meta from core
2146+
export const describe = core.describe;
2147+
export const meta = core.meta;
2148+
21452149
type ZodInstanceOfParams = core.Params<
21462150
ZodCustom,
21472151
core.$ZodIssueCustom,
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, expect, it } from "vitest";
2+
import * as z from "../index.js";
3+
4+
describe("z.describe() check", () => {
5+
it("registers description in globalRegistry", () => {
6+
const schema = z.string().check(z.describe("A string"));
7+
expect(z.globalRegistry.get(schema)?.description).toBe("A string");
8+
});
9+
});
10+
11+
describe("z.meta() check", () => {
12+
it("registers metadata in globalRegistry", () => {
13+
const schema = z.number().check(z.meta({ title: "Age", description: "User's age" }));
14+
const meta = z.globalRegistry.get(schema);
15+
expect(meta?.title).toBe("Age");
16+
expect(meta?.description).toBe("User's age");
17+
});
18+
});
19+
20+
describe("combined usage", () => {
21+
it("works with multiple checks", () => {
22+
const schema = z.string().check(z.describe("Email address"), z.meta({ title: "Email" }));
23+
const meta = z.globalRegistry.get(schema);
24+
expect(meta?.description).toBe("Email address");
25+
expect(meta?.title).toBe("Email");
26+
});
27+
});

packages/zod/src/v4/core/api.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as checks from "./checks.js";
22
import type * as core from "./core.js";
33
import type * as errors from "./errors.js";
4+
import * as registries from "./registries.js";
45
import * as schemas from "./schemas.js";
56
import * as util from "./util.js";
67

@@ -1534,6 +1535,30 @@ export function _check<O = unknown>(fn: schemas.CheckFn<O>, params?: string | $Z
15341535
return ch;
15351536
}
15361537

1538+
export function describe<T>(description: string): checks.$ZodCheck<T> {
1539+
const ch = new checks.$ZodCheck({ check: "describe" });
1540+
ch._zod.onattach = [
1541+
(inst) => {
1542+
const existing = registries.globalRegistry.get(inst) ?? {};
1543+
registries.globalRegistry.add(inst, { ...existing, description });
1544+
},
1545+
];
1546+
ch._zod.check = () => {}; // no-op check
1547+
return ch;
1548+
}
1549+
1550+
export function meta<T>(metadata: registries.GlobalMeta): checks.$ZodCheck<T> {
1551+
const ch = new checks.$ZodCheck({ check: "meta" });
1552+
ch._zod.onattach = [
1553+
(inst) => {
1554+
const existing = registries.globalRegistry.get(inst) ?? {};
1555+
registries.globalRegistry.add(inst, { ...existing, ...metadata });
1556+
},
1557+
];
1558+
ch._zod.check = () => {}; // no-op check
1559+
return ch;
1560+
}
1561+
15371562
// export type $ZodCustomParams = CheckTypeParams<schemas.$ZodCustom, "fn">
15381563

15391564
///////// STRINGBOOL /////////

packages/zod/src/v4/mini/schemas.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,10 @@ export function superRefine<T>(
16141614
return core._superRefine(fn);
16151615
}
16161616

1617+
// Re-export describe and meta from core
1618+
export const describe = core.describe;
1619+
export const meta = core.meta;
1620+
16171621
// instanceof
16181622
abstract class Class {
16191623
constructor(..._args: any[]) {}

0 commit comments

Comments
 (0)