Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 27ccaf1

Browse filesBrowse files
committed
3.25.27. Use for override. Add .value to ZodLiteral.
1 parent d93271e commit 27ccaf1
Copy full SHA for 27ccaf1

File tree

Expand file treeCollapse file tree

8 files changed

+29
-15
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

8 files changed

+29
-15
lines changed
Open diff view settings
Collapse file

‎packages/docs/content/error-customization.mdx‎

Copy file name to clipboardExpand all lines: packages/docs/content/error-customization.mdx
+6-6Lines changed: 6 additions & 6 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ z.string().min(5, {
181181
Return `undefined` to avoid setting an error message. This is useful for customizing certain error messages but not others. Zod will yield control to the next error map in the [precedence chain](#error-precedence).
182182

183183
```ts
184-
z.string().min(5, {
185-
error: (iss) => {
186-
// customize "too_small" error message
187-
if (iss.code === "too_small") {
188-
return `Password must have ${iss.minimum} characters or more`;
184+
z.int64({
185+
error: (issue) => {
186+
// override too_big error message
187+
if (issue.code === "too_big") {
188+
return { message: `Value must be <${issue.maximum}` };
189189
}
190190

191-
// use default error otherwise
191+
// defer to default
192192
return undefined;
193193
},
194194
});
Collapse file

‎packages/docs/content/json-schema.mdx‎

Copy file name to clipboardExpand all lines: packages/docs/content/json-schema.mdx
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ z.toJSONSchema(mySchema, {
414414
});
415415
```
416416

417+
Note that unrepresentable types will throw an `Error` before this functions is called. If you are trying to define custom behavior for an unrepresentable type, you'll need to use set the `unrepresentable: "any"` alongside `override`.
417418

418419
### `io`
419420

Collapse file

‎packages/zod/package.json‎

Copy file name to clipboardExpand all lines: packages/zod/package.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "zod",
3-
"version": "3.25.26",
3+
"version": "3.25.27",
44
"type": "module",
55
"author": "Colin McDonnell <zod@colinhacks.com>",
66
"description": "TypeScript-first schema declaration and validation library with static type inference",
Collapse file

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

Copy file name to clipboardExpand all lines: packages/zod/src/v4/classic/schemas.ts
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,11 +1540,21 @@ export function nativeEnum<T extends util.EnumLike>(entries: T, params?: string
15401540
export interface ZodLiteral<T extends util.Primitive = util.Primitive> extends ZodType {
15411541
_zod: core.$ZodLiteralInternals<T>;
15421542
values: Set<T>;
1543+
/** @legacy Use `.values` instead. Accessing this property will throw an error if the literal accepts multiple values. */
1544+
value: T;
15431545
}
15441546
export const ZodLiteral: core.$constructor<ZodLiteral> = /*@__PURE__*/ core.$constructor("ZodLiteral", (inst, def) => {
15451547
core.$ZodLiteral.init(inst, def);
15461548
ZodType.init(inst, def);
15471549
inst.values = new Set(def.values);
1550+
Object.defineProperty(inst, "value", {
1551+
get() {
1552+
if (def.values.length > 1) {
1553+
throw new Error("This schema contains multiple valid literal values. Use `.values` instead.");
1554+
}
1555+
return def.values[0];
1556+
},
1557+
});
15481558
});
15491559

15501560
export function literal<const T extends Array<util.Literal>>(
Collapse file

‎packages/zod/src/v4/classic/tests/literal.test.ts‎

Copy file name to clipboardExpand all lines: packages/zod/src/v4/classic/tests/literal.test.ts
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,8 @@ test("literal bigint default error message", () => {
8080
expect(result.error!.issues.length).toEqual(1);
8181
expect(result.error!.issues[0].message).toEqual(`Invalid input: expected 12n`);
8282
});
83+
84+
test(".value getter", () => {
85+
expect(z.literal("tuna").value).toEqual("tuna");
86+
expect(() => z.literal([1, 2, 3]).value).toThrow();
87+
});
Collapse file

‎packages/zod/src/v4/core/schemas.ts‎

Copy file name to clipboardExpand all lines: packages/zod/src/v4/core/schemas.ts
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2777,7 +2777,7 @@ export interface $ZodLiteralDef extends $ZodTypeDef {
27772777

27782778
export interface $ZodLiteralInternals<T extends util.Primitive = util.Primitive> extends $ZodTypeInternals<T, T> {
27792779
def: $ZodLiteralDef;
2780-
values: util.PrimitiveSet;
2780+
values: Set<T>;
27812781
pattern: RegExp;
27822782
isst: errors.$ZodIssueInvalidValue;
27832783
}
Collapse file

‎packages/zod/src/v4/core/to-json-schema.ts‎

Copy file name to clipboardExpand all lines: packages/zod/src/v4/core/to-json-schema.ts
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ interface JSONSchemaGeneratorParams {
1616
* - `"any"` — Unrepresentable types become `{}` */
1717
unrepresentable?: "throw" | "any";
1818
/** Arbitrary custom logic that can be used to modify the generated JSON Schema. */
19-
override?: (ctx: { zodSchema: schemas.$ZodType; jsonSchema: JSONSchema.BaseSchema }) => void;
19+
override?: (ctx: { zodSchema: schemas.$ZodTypes; jsonSchema: JSONSchema.BaseSchema }) => void;
2020
/** Whether to extract the `"input"` or `"output"` type. Relevant to transforms, Error converting schema to JSONz, defaults, coerced primitives, etc.
2121
* - `"output" — Default. Convert the output schema.
2222
* - `"input"` — Convert the input schema. */
@@ -73,7 +73,7 @@ export class JSONSchemaGenerator {
7373
metadataRegistry: $ZodRegistry<Record<string, any>>;
7474
target: "draft-7" | "draft-2020-12";
7575
unrepresentable: "throw" | "any";
76-
override: (ctx: { zodSchema: schemas.$ZodType; jsonSchema: JSONSchema.BaseSchema }) => void;
76+
override: (ctx: { zodSchema: schemas.$ZodTypes; jsonSchema: JSONSchema.BaseSchema }) => void;
7777
io: "input" | "output";
7878

7979
counter = 0;
@@ -694,7 +694,7 @@ export class JSONSchemaGenerator {
694694

695695
if (!seen.isParent)
696696
this.override({
697-
zodSchema,
697+
zodSchema: zodSchema as schemas.$ZodTypes,
698698
jsonSchema: schema,
699699
});
700700
};
Collapse file

‎play.ts‎

Copy file name to clipboard
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import * as z from "zod/v4-mini";
1+
import * as z from "zod/v4";
22

3-
z.string().register(z.globalRegistry, {
4-
examples: ["example"], // Type 'string' is not assignable to type 'unique symbol'.
5-
});
3+
z;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.