@anthonyalayo/zod 4.7.0 → 4.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anthonyalayo/zod",
3
- "version": "4.7.0",
3
+ "version": "4.7.1",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "Colin McDonnell <zod@colinhacks.com>",
@@ -165,7 +165,7 @@ export interface ZodType<
165
165
  */
166
166
  isNullable(): boolean;
167
167
  apply<T, TArgs extends unknown[] = []>(fn: (schema: this, ...args: TArgs) => T, ...args: TArgs): T;
168
- /** Returns a copy whose error messages are replaced, keyed by `"path.check"`, `"path.check.value"` or `"path"` for the schema's own type error. */
168
+ /** Returns a copy whose error messages are replaced, keyed by `"path.check"` (a check's method or `name`), `"path.check.value"` or `"path"` for the schema's own type error. */
169
169
  update(messages: ZodUpdateMessages): this;
170
170
  }
171
171
 
@@ -203,6 +203,7 @@ function _updateMessage(node: core.$ZodType, key: string, segs: string[], error:
203
203
  }
204
204
 
205
205
  function _checkMatches(def: any, name: string, value: string | undefined): boolean {
206
+ if (def.name !== undefined && def.name === name && value === undefined) return true;
206
207
  const kind: string = def.check;
207
208
  let names: string[];
208
209
  let val: unknown;
@@ -90,3 +90,19 @@ test("keeps metadata and rejects keys that match nothing", () => {
90
90
  expect(() => schema.update({ "name.min.1.extra": "x" })).toThrow('cannot resolve "name.min.1.extra"');
91
91
  expect(() => schema.update({ "name.min.2": "x" })).toThrow('no "min" check found for "name.min.2"');
92
92
  });
93
+
94
+ test("selects checks by their name label", () => {
95
+ const schema = z
96
+ .object({
97
+ title: z
98
+ .string()
99
+ .min(1, { name: "title_length" })
100
+ .max(100, { name: "title_length" })
101
+ .refine((s) => s.trim() === s, { name: "title_trimmed" }),
102
+ })
103
+ .update({ "title.title_length": "1 to 100 characters", "title.title_trimmed": "no surrounding spaces" });
104
+
105
+ expect(messages(schema, { title: "" })).toEqual(["1 to 100 characters"]);
106
+ expect(messages(schema, { title: ` ${"a".repeat(100)}` })).toEqual(["1 to 100 characters", "no surrounding spaces"]);
107
+ expect(() => schema.update({ "title.other_name": "x" })).toThrow('no "other_name" check found');
108
+ });
@@ -10,6 +10,8 @@ import * as util from "./util.js";
10
10
 
11
11
  export interface $ZodCheckDef {
12
12
  check: string;
13
+ /** Optional label; `schema.update()` can select the check by it. */
14
+ name?: string | undefined;
13
15
  error?: errors.$ZodErrorMap<never> | undefined;
14
16
  /** If true, no later checks will be executed if this check fails. Default `false`. */
15
17
  abort?: boolean | undefined;
@@ -174,6 +174,8 @@ function _updateMessage(node, key, segs, error) {
174
174
  return core.clone(node, { ...def, checks: nextChecks }, { parent: true });
175
175
  }
176
176
  function _checkMatches(def, name, value) {
177
+ if (def.name !== undefined && def.name === name && value === undefined)
178
+ return true;
177
179
  const kind = def.check;
178
180
  let names;
179
181
  let val;
@@ -83,7 +83,7 @@ export interface ZodType<out Output = unknown, out Input = unknown, out Internal
83
83
  */
84
84
  isNullable(): boolean;
85
85
  apply<T, TArgs extends unknown[] = []>(fn: (schema: this, ...args: TArgs) => T, ...args: TArgs): T;
86
- /** Returns a copy whose error messages are replaced, keyed by `"path.check"`, `"path.check.value"` or `"path"` for the schema's own type error. */
86
+ /** Returns a copy whose error messages are replaced, keyed by `"path.check"` (a check's method or `name`), `"path.check.value"` or `"path"` for the schema's own type error. */
87
87
  update(messages: ZodUpdateMessages): this;
88
88
  }
89
89
  export type ZodUpdateMessages = Record<string, string | core.$ZodErrorMap>;
@@ -83,7 +83,7 @@ export interface ZodType<out Output = unknown, out Input = unknown, out Internal
83
83
  */
84
84
  isNullable(): boolean;
85
85
  apply<T, TArgs extends unknown[] = []>(fn: (schema: this, ...args: TArgs) => T, ...args: TArgs): T;
86
- /** Returns a copy whose error messages are replaced, keyed by `"path.check"`, `"path.check.value"` or `"path"` for the schema's own type error. */
86
+ /** Returns a copy whose error messages are replaced, keyed by `"path.check"` (a check's method or `name`), `"path.check.value"` or `"path"` for the schema's own type error. */
87
87
  update(messages: ZodUpdateMessages): this;
88
88
  }
89
89
  export type ZodUpdateMessages = Record<string, string | core.$ZodErrorMap>;
@@ -46,6 +46,8 @@ function _updateMessage(node, key, segs, error) {
46
46
  return core.clone(node, { ...def, checks: nextChecks }, { parent: true });
47
47
  }
48
48
  function _checkMatches(def, name, value) {
49
+ if (def.name !== undefined && def.name === name && value === undefined)
50
+ return true;
49
51
  const kind = def.check;
50
52
  let names;
51
53
  let val;
@@ -4,6 +4,8 @@ import type * as schemas from "./schemas.cjs";
4
4
  import * as util from "./util.cjs";
5
5
  export interface $ZodCheckDef {
6
6
  check: string;
7
+ /** Optional label; `schema.update()` can select the check by it. */
8
+ name?: string | undefined;
7
9
  error?: errors.$ZodErrorMap<never> | undefined;
8
10
  /** If true, no later checks will be executed if this check fails. Default `false`. */
9
11
  abort?: boolean | undefined;
@@ -4,6 +4,8 @@ import type * as schemas from "./schemas.js";
4
4
  import * as util from "./util.js";
5
5
  export interface $ZodCheckDef {
6
6
  check: string;
7
+ /** Optional label; `schema.update()` can select the check by it. */
8
+ name?: string | undefined;
7
9
  error?: errors.$ZodErrorMap<never> | undefined;
8
10
  /** If true, no later checks will be executed if this check fails. Default `false`. */
9
11
  abort?: boolean | undefined;