zod 4.3.0-canary.20251222T195342 → 4.3.0-canary.20251222T205904

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": "zod",
3
- "version": "4.3.0-canary.20251222T195342",
3
+ "version": "4.3.0-canary.20251222T205904",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "Colin McDonnell <zod@colinhacks.com>",
@@ -1540,6 +1540,10 @@ export interface ZodMap<Key extends core.SomeType = core.$ZodType, Value extends
1540
1540
  "~standard": ZodStandardSchemaWithJSON<this>;
1541
1541
  keyType: Key;
1542
1542
  valueType: Value;
1543
+ min(minSize: number, params?: string | core.$ZodCheckMinSizeParams): this;
1544
+ nonempty(params?: string | core.$ZodCheckMinSizeParams): this;
1545
+ max(maxSize: number, params?: string | core.$ZodCheckMaxSizeParams): this;
1546
+ size(size: number, params?: string | core.$ZodCheckSizeEqualsParams): this;
1543
1547
  }
1544
1548
  export const ZodMap: core.$constructor<ZodMap> = /*@__PURE__*/ core.$constructor("ZodMap", (inst, def) => {
1545
1549
  core.$ZodMap.init(inst, def);
@@ -1547,6 +1551,10 @@ export const ZodMap: core.$constructor<ZodMap> = /*@__PURE__*/ core.$constructor
1547
1551
  inst._zod.processJSONSchema = (ctx, json, params) => processors.mapProcessor(inst, ctx, json, params);
1548
1552
  inst.keyType = def.keyType;
1549
1553
  inst.valueType = def.valueType;
1554
+ inst.min = (...args) => inst.check(core._minSize(...args));
1555
+ inst.nonempty = (params) => inst.check(core._minSize(1, params));
1556
+ inst.max = (...args) => inst.check(core._maxSize(...args));
1557
+ inst.size = (...args) => inst.check(core._size(...args));
1550
1558
  });
1551
1559
 
1552
1560
  export function map<Key extends core.SomeType, Value extends core.SomeType>(
@@ -4,6 +4,12 @@ import * as z from "zod/v4";
4
4
  const stringMap = z.map(z.string(), z.string());
5
5
  type stringMap = z.infer<typeof stringMap>;
6
6
 
7
+ const minTwo = stringMap.min(2);
8
+ const maxTwo = stringMap.max(2);
9
+ const justTwo = stringMap.size(2);
10
+ const nonEmpty = stringMap.nonempty();
11
+ const nonEmptyMax = stringMap.nonempty().max(2);
12
+
7
13
  test("type inference", () => {
8
14
  expectTypeOf<stringMap>().toEqualTypeOf<Map<string, string>>();
9
15
  });
@@ -24,6 +30,75 @@ test("valid parse", () => {
24
30
  `);
25
31
  });
26
32
 
33
+ test("valid parse: size-related methods", () => {
34
+ expect(() => {
35
+ minTwo.parse(
36
+ new Map([
37
+ ["a", "b"],
38
+ ["c", "d"],
39
+ ])
40
+ );
41
+ minTwo.parse(
42
+ new Map([
43
+ ["a", "b"],
44
+ ["c", "d"],
45
+ ["e", "f"],
46
+ ])
47
+ );
48
+ maxTwo.parse(
49
+ new Map([
50
+ ["a", "b"],
51
+ ["c", "d"],
52
+ ])
53
+ );
54
+ maxTwo.parse(new Map([["a", "b"]]));
55
+ justTwo.parse(
56
+ new Map([
57
+ ["a", "b"],
58
+ ["c", "d"],
59
+ ])
60
+ );
61
+ nonEmpty.parse(new Map([["a", "b"]]));
62
+ nonEmptyMax.parse(
63
+ new Map([
64
+ ["a", "b"],
65
+ ["c", "d"],
66
+ ])
67
+ );
68
+ }).not.toThrow();
69
+
70
+ const sizeZeroResult = stringMap.parse(new Map());
71
+ expect(sizeZeroResult.size).toBe(0);
72
+
73
+ const sizeTwoResult = minTwo.parse(
74
+ new Map([
75
+ ["a", "b"],
76
+ ["c", "d"],
77
+ ])
78
+ );
79
+ expect(sizeTwoResult.size).toBe(2);
80
+ });
81
+
82
+ test("failing when parsing empty map in nonempty ", () => {
83
+ const result = nonEmpty.safeParse(new Map());
84
+ expect(result.success).toEqual(false);
85
+ expect(result.error!.issues.length).toEqual(1);
86
+ expect(result.error!.issues[0].code).toEqual("too_small");
87
+ });
88
+
89
+ test("failing when map is bigger than max() ", () => {
90
+ const result = maxTwo.safeParse(
91
+ new Map([
92
+ ["a", "b"],
93
+ ["c", "d"],
94
+ ["e", "f"],
95
+ ])
96
+ );
97
+ expect(result.success).toEqual(false);
98
+ expect(result.error!.issues.length).toEqual(1);
99
+ expect(result.error!.issues[0].code).toEqual("too_big");
100
+ });
101
+
27
102
  test("valid parse async", async () => {
28
103
  const asyncMap = z.map(
29
104
  z.string().refine(async () => false, "bad key"),
@@ -194,3 +269,62 @@ test("map with object keys", () => {
194
269
  ]]
195
270
  `);
196
271
  });
272
+
273
+ test("min/max", async () => {
274
+ const schema = stringMap.min(4).max(5);
275
+
276
+ const r1 = schema.safeParse(
277
+ new Map([
278
+ ["a", "a"],
279
+ ["b", "b"],
280
+ ["c", "c"],
281
+ ["d", "d"],
282
+ ])
283
+ );
284
+ expect(r1.success).toEqual(true);
285
+
286
+ const r2 = schema.safeParse(
287
+ new Map([
288
+ ["a", "a"],
289
+ ["b", "b"],
290
+ ["c", "c"],
291
+ ])
292
+ );
293
+ expect(r2.success).toEqual(false);
294
+ expect(r2.error!.issues).toMatchInlineSnapshot(`
295
+ [
296
+ {
297
+ "code": "too_small",
298
+ "inclusive": true,
299
+ "message": "Too small: expected map to have >=4 entries",
300
+ "minimum": 4,
301
+ "origin": "map",
302
+ "path": [],
303
+ },
304
+ ]
305
+ `);
306
+
307
+ const r3 = schema.safeParse(
308
+ new Map([
309
+ ["a", "a"],
310
+ ["b", "b"],
311
+ ["c", "c"],
312
+ ["d", "d"],
313
+ ["e", "e"],
314
+ ["f", "f"],
315
+ ])
316
+ );
317
+ expect(r3.success).toEqual(false);
318
+ expect(r3.error!.issues).toMatchInlineSnapshot(`
319
+ [
320
+ {
321
+ "code": "too_big",
322
+ "inclusive": true,
323
+ "maximum": 5,
324
+ "message": "Too big: expected map to have <=5 entries",
325
+ "origin": "map",
326
+ "path": [],
327
+ },
328
+ ]
329
+ `);
330
+ });
@@ -31,6 +31,7 @@ const error: () => errors.$ZodErrorMap = () => {
31
31
  file: { unit: "bytes", verb: "to have" },
32
32
  array: { unit: "items", verb: "to have" },
33
33
  set: { unit: "items", verb: "to have" },
34
+ map: { unit: "entries", verb: "to have" },
34
35
  };
35
36
 
36
37
  function getSizing(origin: string): { unit: string; verb: string } | null {
@@ -826,6 +826,10 @@ exports.ZodMap = core.$constructor("ZodMap", (inst, def) => {
826
826
  inst._zod.processJSONSchema = (ctx, json, params) => processors.mapProcessor(inst, ctx, json, params);
827
827
  inst.keyType = def.keyType;
828
828
  inst.valueType = def.valueType;
829
+ inst.min = (...args) => inst.check(core._minSize(...args));
830
+ inst.nonempty = (params) => inst.check(core._minSize(1, params));
831
+ inst.max = (...args) => inst.check(core._maxSize(...args));
832
+ inst.size = (...args) => inst.check(core._size(...args));
829
833
  });
830
834
  function map(keyType, valueType, params) {
831
835
  return new exports.ZodMap({
@@ -514,6 +514,10 @@ export interface ZodMap<Key extends core.SomeType = core.$ZodType, Value extends
514
514
  "~standard": ZodStandardSchemaWithJSON<this>;
515
515
  keyType: Key;
516
516
  valueType: Value;
517
+ min(minSize: number, params?: string | core.$ZodCheckMinSizeParams): this;
518
+ nonempty(params?: string | core.$ZodCheckMinSizeParams): this;
519
+ max(maxSize: number, params?: string | core.$ZodCheckMaxSizeParams): this;
520
+ size(size: number, params?: string | core.$ZodCheckSizeEqualsParams): this;
517
521
  }
518
522
  export declare const ZodMap: core.$constructor<ZodMap>;
519
523
  export declare function map<Key extends core.SomeType, Value extends core.SomeType>(keyType: Key, valueType: Value, params?: string | core.$ZodMapParams): ZodMap<Key, Value>;
@@ -514,6 +514,10 @@ export interface ZodMap<Key extends core.SomeType = core.$ZodType, Value extends
514
514
  "~standard": ZodStandardSchemaWithJSON<this>;
515
515
  keyType: Key;
516
516
  valueType: Value;
517
+ min(minSize: number, params?: string | core.$ZodCheckMinSizeParams): this;
518
+ nonempty(params?: string | core.$ZodCheckMinSizeParams): this;
519
+ max(maxSize: number, params?: string | core.$ZodCheckMaxSizeParams): this;
520
+ size(size: number, params?: string | core.$ZodCheckSizeEqualsParams): this;
517
521
  }
518
522
  export declare const ZodMap: core.$constructor<ZodMap>;
519
523
  export declare function map<Key extends core.SomeType, Value extends core.SomeType>(keyType: Key, valueType: Value, params?: string | core.$ZodMapParams): ZodMap<Key, Value>;
@@ -709,6 +709,10 @@ export const ZodMap = /*@__PURE__*/ core.$constructor("ZodMap", (inst, def) => {
709
709
  inst._zod.processJSONSchema = (ctx, json, params) => processors.mapProcessor(inst, ctx, json, params);
710
710
  inst.keyType = def.keyType;
711
711
  inst.valueType = def.valueType;
712
+ inst.min = (...args) => inst.check(core._minSize(...args));
713
+ inst.nonempty = (params) => inst.check(core._minSize(1, params));
714
+ inst.max = (...args) => inst.check(core._maxSize(...args));
715
+ inst.size = (...args) => inst.check(core._size(...args));
712
716
  });
713
717
  export function map(keyType, valueType, params) {
714
718
  return new ZodMap({
package/v4/locales/en.cjs CHANGED
@@ -53,6 +53,7 @@ const error = () => {
53
53
  file: { unit: "bytes", verb: "to have" },
54
54
  array: { unit: "items", verb: "to have" },
55
55
  set: { unit: "items", verb: "to have" },
56
+ map: { unit: "entries", verb: "to have" },
56
57
  };
57
58
  function getSizing(origin) {
58
59
  return Sizable[origin] ?? null;
package/v4/locales/en.js CHANGED
@@ -25,6 +25,7 @@ const error = () => {
25
25
  file: { unit: "bytes", verb: "to have" },
26
26
  array: { unit: "items", verb: "to have" },
27
27
  set: { unit: "items", verb: "to have" },
28
+ map: { unit: "entries", verb: "to have" },
28
29
  };
29
30
  function getSizing(origin) {
30
31
  return Sizable[origin] ?? null;