zod 4.1.8 → 4.1.10

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.1.8",
3
+ "version": "4.1.10",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "Colin McDonnell <zod@colinhacks.com>",
@@ -146,16 +146,14 @@ export const ZodType: core.$constructor<ZodType> = /*@__PURE__*/ core.$construct
146
146
  // base methods
147
147
  inst.check = (...checks) => {
148
148
  return inst.clone(
149
- {
150
- ...def,
149
+ util.mergeDefs(def, {
151
150
  checks: [
152
151
  ...(def.checks ?? []),
153
152
  ...checks.map((ch) =>
154
153
  typeof ch === "function" ? { _zod: { check: ch, def: { check: "custom" }, onattach: [] } } : ch
155
154
  ),
156
155
  ],
157
- }
158
- // { parent: true }
156
+ })
159
157
  );
160
158
  };
161
159
  inst.clone = (def, params) => core.clone(inst, def, params);
@@ -1196,7 +1194,10 @@ export const ZodObject: core.$constructor<ZodObject> = /*@__PURE__*/ core.$const
1196
1194
  core.$ZodObjectJIT.init(inst, def);
1197
1195
  ZodType.init(inst, def);
1198
1196
 
1199
- util.defineLazy(inst, "shape", () => def.shape);
1197
+ util.defineLazy(inst, "shape", () => {
1198
+ return def.shape;
1199
+ });
1200
+
1200
1201
  inst.keyof = () => _enum(Object.keys(inst._zod.def.shape)) as any;
1201
1202
  inst.catchall = (catchall) => inst.clone({ ...inst._zod.def, catchall: catchall as any as core.$ZodType }) as any;
1202
1203
  inst.passthrough = () => inst.clone({ ...inst._zod.def, catchall: unknown() });
@@ -1223,10 +1224,7 @@ export function object<T extends core.$ZodLooseShape = Partial<Record<never, cor
1223
1224
  ): ZodObject<util.Writeable<T>, core.$strip> {
1224
1225
  const def: core.$ZodObjectDef = {
1225
1226
  type: "object",
1226
- get shape() {
1227
- util.assignProp(this, "shape", shape ? util.objectClone(shape) : {});
1228
- return this.shape;
1229
- },
1227
+ shape: shape!,
1230
1228
  ...util.normalizeParams(params),
1231
1229
  };
1232
1230
  return new ZodObject(def) as any;
@@ -1240,10 +1238,7 @@ export function strictObject<T extends core.$ZodLooseShape>(
1240
1238
  ): ZodObject<T, core.$strict> {
1241
1239
  return new ZodObject({
1242
1240
  type: "object",
1243
- get shape() {
1244
- util.assignProp(this, "shape", util.objectClone(shape));
1245
- return this.shape;
1246
- },
1241
+ shape,
1247
1242
  catchall: never(),
1248
1243
  ...util.normalizeParams(params),
1249
1244
  }) as any;
@@ -1257,10 +1252,7 @@ export function looseObject<T extends core.$ZodLooseShape>(
1257
1252
  ): ZodObject<T, core.$loose> {
1258
1253
  return new ZodObject({
1259
1254
  type: "object",
1260
- get shape() {
1261
- util.assignProp(this, "shape", util.objectClone(shape));
1262
- return this.shape;
1263
- },
1255
+ shape,
1264
1256
  catchall: unknown(),
1265
1257
  ...util.normalizeParams(params),
1266
1258
  }) as any;
@@ -537,3 +537,46 @@ export type RecursiveA = z.ZodUnion<
537
537
  }>,
538
538
  ]
539
539
  >;
540
+
541
+ test("recursive type with `id` meta", () => {
542
+ const AType = z.object({
543
+ type: z.literal("a"),
544
+ name: z.string(),
545
+ });
546
+
547
+ const BType = z.object({
548
+ type: z.literal("b"),
549
+ name: z.string(),
550
+ });
551
+
552
+ const CType = z.object({
553
+ type: z.literal("c"),
554
+ name: z.string(),
555
+ });
556
+
557
+ const Schema = z.object({
558
+ type: z.literal("special").meta({ description: "Type" }),
559
+ config: z.object({
560
+ title: z.string().meta({ description: "Title" }),
561
+ get elements() {
562
+ return z.array(z.discriminatedUnion("type", [AType, BType, CType])).meta({
563
+ id: "SpecialElements",
564
+ title: "SpecialElements",
565
+ description: "Array of elements",
566
+ });
567
+ },
568
+ }),
569
+ });
570
+
571
+ Schema.parse({
572
+ type: "special",
573
+ config: {
574
+ title: "Special",
575
+ elements: [
576
+ { type: "a", name: "John" },
577
+ { type: "b", name: "Jane" },
578
+ { type: "c", name: "Jim" },
579
+ ],
580
+ },
581
+ });
582
+ });
@@ -1739,7 +1739,7 @@ export interface $ZodObjectDef<Shape extends $ZodShape = $ZodShape> extends $Zod
1739
1739
 
1740
1740
  export interface $ZodObjectInternals<
1741
1741
  /** @ts-ignore Cast variance */
1742
- out Shape extends Readonly<$ZodShape> = Readonly<$ZodShape>,
1742
+ out Shape extends $ZodShape = $ZodShape,
1743
1743
  out Config extends $ZodObjectConfig = $ZodObjectConfig,
1744
1744
  > extends _$ZodTypeInternals {
1745
1745
  def: $ZodObjectDef<Shape>;
@@ -1825,6 +1825,21 @@ function handleCatchall(
1825
1825
  export const $ZodObject: core.$constructor<$ZodObject> = /*@__PURE__*/ core.$constructor("$ZodObject", (inst, def) => {
1826
1826
  // requires cast because technically $ZodObject doesn't extend
1827
1827
  $ZodType.init(inst, def);
1828
+ // const sh = def.shape;
1829
+ const desc = Object.getOwnPropertyDescriptor(def, "shape");
1830
+ if (!desc!.get) {
1831
+ const sh = def.shape;
1832
+ Object.defineProperty(def, "shape", {
1833
+ get: () => {
1834
+ const newSh = { ...sh };
1835
+ Object.defineProperty(def, "shape", {
1836
+ value: newSh,
1837
+ });
1838
+
1839
+ return newSh;
1840
+ },
1841
+ });
1842
+ }
1828
1843
 
1829
1844
  const _normalized = util.cached(() => normalizeDef(def));
1830
1845
 
@@ -1853,7 +1868,6 @@ export const $ZodObject: core.$constructor<$ZodObject> = /*@__PURE__*/ core.$con
1853
1868
  payload.issues.push({
1854
1869
  expected: "object",
1855
1870
  code: "invalid_type",
1856
-
1857
1871
  input,
1858
1872
  inst,
1859
1873
  });
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
3
  minor: 1,
4
- patch: 8 as number,
4
+ patch: 10 as number,
5
5
  } as const;
@@ -769,10 +769,7 @@ export function object<T extends core.$ZodLooseShape = Record<never, SomeType>>(
769
769
  ): ZodMiniObject<T, core.$strip> {
770
770
  const def: core.$ZodObjectDef = {
771
771
  type: "object",
772
- get shape() {
773
- util.assignProp(this, "shape", { ...shape });
774
- return this.shape;
775
- },
772
+ shape: shape!,
776
773
  ...util.normalizeParams(params),
777
774
  };
778
775
  return new ZodMiniObject(def) as any;
@@ -785,11 +782,7 @@ export function strictObject<T extends core.$ZodLooseShape>(
785
782
  ): ZodMiniObject<T, core.$strict> {
786
783
  return new ZodMiniObject({
787
784
  type: "object",
788
- // shape: shape as core.$ZodLooseShape,
789
- get shape() {
790
- util.assignProp(this, "shape", { ...shape });
791
- return this.shape;
792
- },
785
+ shape,
793
786
  catchall: never(),
794
787
  ...util.normalizeParams(params),
795
788
  }) as any;
@@ -802,14 +795,7 @@ export function looseObject<T extends core.$ZodLooseShape>(
802
795
  ): ZodMiniObject<T, core.$loose> {
803
796
  return new ZodMiniObject({
804
797
  type: "object",
805
- // shape: shape as core.$ZodLooseShape,
806
- get shape() {
807
- util.assignProp(this, "shape", { ...shape });
808
- return this.shape;
809
- },
810
- // get optional() {
811
- // return util.optionalKeys(shape);
812
- // },
798
+ shape,
813
799
  catchall: unknown(),
814
800
  ...util.normalizeParams(params),
815
801
  }) as any;
@@ -127,15 +127,12 @@ exports.ZodType = core.$constructor("ZodType", (inst, def) => {
127
127
  Object.defineProperty(inst, "_def", { value: def });
128
128
  // base methods
129
129
  inst.check = (...checks) => {
130
- return inst.clone({
131
- ...def,
130
+ return inst.clone(index_js_1.util.mergeDefs(def, {
132
131
  checks: [
133
132
  ...(def.checks ?? []),
134
133
  ...checks.map((ch) => typeof ch === "function" ? { _zod: { check: ch, def: { check: "custom" }, onattach: [] } } : ch),
135
134
  ],
136
- }
137
- // { parent: true }
138
- );
135
+ }));
139
136
  };
140
137
  inst.clone = (def, params) => core.clone(inst, def, params);
141
138
  inst.brand = () => inst;
@@ -631,7 +628,9 @@ function keyof(schema) {
631
628
  exports.ZodObject = core.$constructor("ZodObject", (inst, def) => {
632
629
  core.$ZodObjectJIT.init(inst, def);
633
630
  exports.ZodType.init(inst, def);
634
- index_js_1.util.defineLazy(inst, "shape", () => def.shape);
631
+ index_js_1.util.defineLazy(inst, "shape", () => {
632
+ return def.shape;
633
+ });
635
634
  inst.keyof = () => _enum(Object.keys(inst._zod.def.shape));
636
635
  inst.catchall = (catchall) => inst.clone({ ...inst._zod.def, catchall: catchall });
637
636
  inst.passthrough = () => inst.clone({ ...inst._zod.def, catchall: unknown() });
@@ -653,10 +652,7 @@ exports.ZodObject = core.$constructor("ZodObject", (inst, def) => {
653
652
  function object(shape, params) {
654
653
  const def = {
655
654
  type: "object",
656
- get shape() {
657
- index_js_1.util.assignProp(this, "shape", shape ? index_js_1.util.objectClone(shape) : {});
658
- return this.shape;
659
- },
655
+ shape: shape,
660
656
  ...index_js_1.util.normalizeParams(params),
661
657
  };
662
658
  return new exports.ZodObject(def);
@@ -665,10 +661,7 @@ function object(shape, params) {
665
661
  function strictObject(shape, params) {
666
662
  return new exports.ZodObject({
667
663
  type: "object",
668
- get shape() {
669
- index_js_1.util.assignProp(this, "shape", index_js_1.util.objectClone(shape));
670
- return this.shape;
671
- },
664
+ shape,
672
665
  catchall: never(),
673
666
  ...index_js_1.util.normalizeParams(params),
674
667
  });
@@ -677,10 +670,7 @@ function strictObject(shape, params) {
677
670
  function looseObject(shape, params) {
678
671
  return new exports.ZodObject({
679
672
  type: "object",
680
- get shape() {
681
- index_js_1.util.assignProp(this, "shape", index_js_1.util.objectClone(shape));
682
- return this.shape;
683
- },
673
+ shape,
684
674
  catchall: unknown(),
685
675
  ...index_js_1.util.normalizeParams(params),
686
676
  });
@@ -10,15 +10,12 @@ export const ZodType = /*@__PURE__*/ core.$constructor("ZodType", (inst, def) =>
10
10
  Object.defineProperty(inst, "_def", { value: def });
11
11
  // base methods
12
12
  inst.check = (...checks) => {
13
- return inst.clone({
14
- ...def,
13
+ return inst.clone(util.mergeDefs(def, {
15
14
  checks: [
16
15
  ...(def.checks ?? []),
17
16
  ...checks.map((ch) => typeof ch === "function" ? { _zod: { check: ch, def: { check: "custom" }, onattach: [] } } : ch),
18
17
  ],
19
- }
20
- // { parent: true }
21
- );
18
+ }));
22
19
  };
23
20
  inst.clone = (def, params) => core.clone(inst, def, params);
24
21
  inst.brand = () => inst;
@@ -517,7 +514,9 @@ export function keyof(schema) {
517
514
  export const ZodObject = /*@__PURE__*/ core.$constructor("ZodObject", (inst, def) => {
518
515
  core.$ZodObjectJIT.init(inst, def);
519
516
  ZodType.init(inst, def);
520
- util.defineLazy(inst, "shape", () => def.shape);
517
+ util.defineLazy(inst, "shape", () => {
518
+ return def.shape;
519
+ });
521
520
  inst.keyof = () => _enum(Object.keys(inst._zod.def.shape));
522
521
  inst.catchall = (catchall) => inst.clone({ ...inst._zod.def, catchall: catchall });
523
522
  inst.passthrough = () => inst.clone({ ...inst._zod.def, catchall: unknown() });
@@ -539,10 +538,7 @@ export const ZodObject = /*@__PURE__*/ core.$constructor("ZodObject", (inst, def
539
538
  export function object(shape, params) {
540
539
  const def = {
541
540
  type: "object",
542
- get shape() {
543
- util.assignProp(this, "shape", shape ? util.objectClone(shape) : {});
544
- return this.shape;
545
- },
541
+ shape: shape,
546
542
  ...util.normalizeParams(params),
547
543
  };
548
544
  return new ZodObject(def);
@@ -551,10 +547,7 @@ export function object(shape, params) {
551
547
  export function strictObject(shape, params) {
552
548
  return new ZodObject({
553
549
  type: "object",
554
- get shape() {
555
- util.assignProp(this, "shape", util.objectClone(shape));
556
- return this.shape;
557
- },
550
+ shape,
558
551
  catchall: never(),
559
552
  ...util.normalizeParams(params),
560
553
  });
@@ -563,10 +556,7 @@ export function strictObject(shape, params) {
563
556
  export function looseObject(shape, params) {
564
557
  return new ZodObject({
565
558
  type: "object",
566
- get shape() {
567
- util.assignProp(this, "shape", util.objectClone(shape));
568
- return this.shape;
569
- },
559
+ shape,
570
560
  catchall: unknown(),
571
561
  ...util.normalizeParams(params),
572
562
  });
@@ -800,6 +800,20 @@ function handleCatchall(proms, input, payload, ctx, def, inst) {
800
800
  exports.$ZodObject = core.$constructor("$ZodObject", (inst, def) => {
801
801
  // requires cast because technically $ZodObject doesn't extend
802
802
  exports.$ZodType.init(inst, def);
803
+ // const sh = def.shape;
804
+ const desc = Object.getOwnPropertyDescriptor(def, "shape");
805
+ if (!desc.get) {
806
+ const sh = def.shape;
807
+ Object.defineProperty(def, "shape", {
808
+ get: () => {
809
+ const newSh = { ...sh };
810
+ Object.defineProperty(def, "shape", {
811
+ value: newSh,
812
+ });
813
+ return newSh;
814
+ },
815
+ });
816
+ }
803
817
  const _normalized = util.cached(() => normalizeDef(def));
804
818
  util.defineLazy(inst._zod, "propValues", () => {
805
819
  const shape = def.shape;
@@ -584,7 +584,7 @@ export interface $ZodObjectDef<Shape extends $ZodShape = $ZodShape> extends $Zod
584
584
  }
585
585
  export interface $ZodObjectInternals<
586
586
  /** @ts-ignore Cast variance */
587
- out Shape extends Readonly<$ZodShape> = Readonly<$ZodShape>, out Config extends $ZodObjectConfig = $ZodObjectConfig> extends _$ZodTypeInternals {
587
+ out Shape extends $ZodShape = $ZodShape, out Config extends $ZodObjectConfig = $ZodObjectConfig> extends _$ZodTypeInternals {
588
588
  def: $ZodObjectDef<Shape>;
589
589
  config: Config;
590
590
  isst: errors.$ZodIssueInvalidType | errors.$ZodIssueUnrecognizedKeys;
@@ -584,7 +584,7 @@ export interface $ZodObjectDef<Shape extends $ZodShape = $ZodShape> extends $Zod
584
584
  }
585
585
  export interface $ZodObjectInternals<
586
586
  /** @ts-ignore Cast variance */
587
- out Shape extends Readonly<$ZodShape> = Readonly<$ZodShape>, out Config extends $ZodObjectConfig = $ZodObjectConfig> extends _$ZodTypeInternals {
587
+ out Shape extends $ZodShape = $ZodShape, out Config extends $ZodObjectConfig = $ZodObjectConfig> extends _$ZodTypeInternals {
588
588
  def: $ZodObjectDef<Shape>;
589
589
  config: Config;
590
590
  isst: errors.$ZodIssueInvalidType | errors.$ZodIssueUnrecognizedKeys;
@@ -769,6 +769,20 @@ function handleCatchall(proms, input, payload, ctx, def, inst) {
769
769
  export const $ZodObject = /*@__PURE__*/ core.$constructor("$ZodObject", (inst, def) => {
770
770
  // requires cast because technically $ZodObject doesn't extend
771
771
  $ZodType.init(inst, def);
772
+ // const sh = def.shape;
773
+ const desc = Object.getOwnPropertyDescriptor(def, "shape");
774
+ if (!desc.get) {
775
+ const sh = def.shape;
776
+ Object.defineProperty(def, "shape", {
777
+ get: () => {
778
+ const newSh = { ...sh };
779
+ Object.defineProperty(def, "shape", {
780
+ value: newSh,
781
+ });
782
+ return newSh;
783
+ },
784
+ });
785
+ }
772
786
  const _normalized = util.cached(() => normalizeDef(def));
773
787
  util.defineLazy(inst._zod, "propValues", () => {
774
788
  const shape = def.shape;
@@ -4,5 +4,5 @@ exports.version = void 0;
4
4
  exports.version = {
5
5
  major: 4,
6
6
  minor: 1,
7
- patch: 8,
7
+ patch: 10,
8
8
  };
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
3
  minor: 1,
4
- patch: 8,
4
+ patch: 10,
5
5
  };
@@ -474,10 +474,7 @@ exports.ZodMiniObject = core.$constructor("ZodMiniObject", (inst, def) => {
474
474
  function object(shape, params) {
475
475
  const def = {
476
476
  type: "object",
477
- get shape() {
478
- index_js_1.util.assignProp(this, "shape", { ...shape });
479
- return this.shape;
480
- },
477
+ shape: shape,
481
478
  ...index_js_1.util.normalizeParams(params),
482
479
  };
483
480
  return new exports.ZodMiniObject(def);
@@ -486,11 +483,7 @@ function object(shape, params) {
486
483
  function strictObject(shape, params) {
487
484
  return new exports.ZodMiniObject({
488
485
  type: "object",
489
- // shape: shape as core.$ZodLooseShape,
490
- get shape() {
491
- index_js_1.util.assignProp(this, "shape", { ...shape });
492
- return this.shape;
493
- },
486
+ shape,
494
487
  catchall: never(),
495
488
  ...index_js_1.util.normalizeParams(params),
496
489
  });
@@ -499,14 +492,7 @@ function strictObject(shape, params) {
499
492
  function looseObject(shape, params) {
500
493
  return new exports.ZodMiniObject({
501
494
  type: "object",
502
- // shape: shape as core.$ZodLooseShape,
503
- get shape() {
504
- index_js_1.util.assignProp(this, "shape", { ...shape });
505
- return this.shape;
506
- },
507
- // get optional() {
508
- // return util.optionalKeys(shape);
509
- // },
495
+ shape,
510
496
  catchall: unknown(),
511
497
  ...index_js_1.util.normalizeParams(params),
512
498
  });
@@ -353,10 +353,7 @@ export const ZodMiniObject = /*@__PURE__*/ core.$constructor("ZodMiniObject", (i
353
353
  export function object(shape, params) {
354
354
  const def = {
355
355
  type: "object",
356
- get shape() {
357
- util.assignProp(this, "shape", { ...shape });
358
- return this.shape;
359
- },
356
+ shape: shape,
360
357
  ...util.normalizeParams(params),
361
358
  };
362
359
  return new ZodMiniObject(def);
@@ -365,11 +362,7 @@ export function object(shape, params) {
365
362
  export function strictObject(shape, params) {
366
363
  return new ZodMiniObject({
367
364
  type: "object",
368
- // shape: shape as core.$ZodLooseShape,
369
- get shape() {
370
- util.assignProp(this, "shape", { ...shape });
371
- return this.shape;
372
- },
365
+ shape,
373
366
  catchall: never(),
374
367
  ...util.normalizeParams(params),
375
368
  });
@@ -378,14 +371,7 @@ export function strictObject(shape, params) {
378
371
  export function looseObject(shape, params) {
379
372
  return new ZodMiniObject({
380
373
  type: "object",
381
- // shape: shape as core.$ZodLooseShape,
382
- get shape() {
383
- util.assignProp(this, "shape", { ...shape });
384
- return this.shape;
385
- },
386
- // get optional() {
387
- // return util.optionalKeys(shape);
388
- // },
374
+ shape,
389
375
  catchall: unknown(),
390
376
  ...util.normalizeParams(params),
391
377
  });