zod 4.0.14 → 4.0.16

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.0.14",
3
+ "version": "4.0.16",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "author": "Colin McDonnell <zod@colinhacks.com>",
@@ -1038,9 +1038,9 @@ export function array<T extends core.SomeType>(element: T, params?: string | cor
1038
1038
  }
1039
1039
 
1040
1040
  // .keyof
1041
- export function keyof<T extends ZodObject>(schema: T): ZodLiteral<Exclude<keyof T["_zod"]["output"], symbol>> {
1041
+ export function keyof<T extends ZodObject>(schema: T): ZodEnum<util.KeysEnum<T["_zod"]["output"]>> {
1042
1042
  const shape = schema._zod.def.shape;
1043
- return literal(Object.keys(shape)) as any;
1043
+ return _enum(Object.keys(shape)) as any;
1044
1044
  }
1045
1045
 
1046
1046
  // ZodObject
@@ -1149,7 +1149,7 @@ export function object<T extends core.$ZodLooseShape = Partial<Record<never, cor
1149
1149
  const def: core.$ZodObjectDef = {
1150
1150
  type: "object",
1151
1151
  get shape() {
1152
- util.assignProp(this, "shape", { ...shape });
1152
+ util.assignProp(this, "shape", shape ? util.objectClone(shape) : {});
1153
1153
  return this.shape;
1154
1154
  },
1155
1155
  ...util.normalizeParams(params),
@@ -1166,7 +1166,7 @@ export function strictObject<T extends core.$ZodLooseShape>(
1166
1166
  return new ZodObject({
1167
1167
  type: "object",
1168
1168
  get shape() {
1169
- util.assignProp(this, "shape", { ...shape });
1169
+ util.assignProp(this, "shape", util.objectClone(shape));
1170
1170
  return this.shape;
1171
1171
  },
1172
1172
  catchall: never(),
@@ -1183,7 +1183,7 @@ export function looseObject<T extends core.$ZodLooseShape>(
1183
1183
  return new ZodObject({
1184
1184
  type: "object",
1185
1185
  get shape() {
1186
- util.assignProp(this, "shape", { ...shape });
1186
+ util.assignProp(this, "shape", util.objectClone(shape));
1187
1187
  return this.shape;
1188
1188
  },
1189
1189
  catchall: unknown(),
@@ -1215,11 +1215,13 @@ export function union<const T extends readonly core.SomeType[]>(
1215
1215
  }
1216
1216
 
1217
1217
  // ZodDiscriminatedUnion
1218
- export interface ZodDiscriminatedUnion<Options extends readonly core.SomeType[] = readonly core.$ZodType[]>
1219
- extends ZodUnion<Options>,
1220
- core.$ZodDiscriminatedUnion<Options> {
1221
- _zod: core.$ZodDiscriminatedUnionInternals<Options>;
1222
- def: core.$ZodDiscriminatedUnionDef<Options>;
1218
+ export interface ZodDiscriminatedUnion<
1219
+ Options extends readonly core.SomeType[] = readonly core.$ZodType[],
1220
+ Disc extends string = string,
1221
+ > extends ZodUnion<Options>,
1222
+ core.$ZodDiscriminatedUnion<Options, Disc> {
1223
+ _zod: core.$ZodDiscriminatedUnionInternals<Options, Disc>;
1224
+ def: core.$ZodDiscriminatedUnionDef<Options, Disc>;
1223
1225
  }
1224
1226
  export const ZodDiscriminatedUnion: core.$constructor<ZodDiscriminatedUnion> = /*@__PURE__*/ core.$constructor(
1225
1227
  "ZodDiscriminatedUnion",
@@ -1231,11 +1233,12 @@ export const ZodDiscriminatedUnion: core.$constructor<ZodDiscriminatedUnion> = /
1231
1233
 
1232
1234
  export function discriminatedUnion<
1233
1235
  Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]],
1236
+ Disc extends string,
1234
1237
  >(
1235
- discriminator: string,
1238
+ discriminator: Disc,
1236
1239
  options: Types,
1237
1240
  params?: string | core.$ZodDiscriminatedUnionParams
1238
- ): ZodDiscriminatedUnion<Types> {
1241
+ ): ZodDiscriminatedUnion<Types, Disc> {
1239
1242
  // const [options, params] = args;
1240
1243
  return new ZodDiscriminatedUnion({
1241
1244
  type: "union",
@@ -1685,7 +1688,7 @@ export function _default<T extends core.SomeType>(
1685
1688
  type: "default",
1686
1689
  innerType: innerType as any as core.$ZodType,
1687
1690
  get defaultValue() {
1688
- return typeof defaultValue === "function" ? (defaultValue as Function)() : defaultValue;
1691
+ return typeof defaultValue === "function" ? (defaultValue as Function)() : util.shallowClone(defaultValue);
1689
1692
  },
1690
1693
  }) as any;
1691
1694
  }
@@ -1713,7 +1716,7 @@ export function prefault<T extends core.SomeType>(
1713
1716
  type: "prefault",
1714
1717
  innerType: innerType as any as core.$ZodType,
1715
1718
  get defaultValue() {
1716
- return typeof defaultValue === "function" ? (defaultValue as Function)() : defaultValue;
1719
+ return typeof defaultValue === "function" ? (defaultValue as Function)() : util.shallowClone(defaultValue);
1717
1720
  },
1718
1721
  }) as any;
1719
1722
  }
@@ -311,3 +311,15 @@ test("partial should not clobber defaults", () => {
311
311
  }
312
312
  `);
313
313
  });
314
+
315
+ test("defaulted object schema returns shallow clone", () => {
316
+ const schema = z
317
+ .object({
318
+ a: z.string(),
319
+ })
320
+ .default({ a: "x" });
321
+ const result1 = schema.parse(undefined);
322
+ const result2 = schema.parse(undefined);
323
+ expect(result1).not.toBe(result2);
324
+ expect(result1).toEqual(result2);
325
+ });
@@ -261,6 +261,21 @@ test("inferred enum type", async () => {
261
261
  expectTypeOf<Enum>().toEqualTypeOf<"a" | "b">();
262
262
  });
263
263
 
264
+ test("z.keyof returns enum", () => {
265
+ const User = z.object({ name: z.string(), age: z.number() });
266
+ const keysSchema = z.keyof(User);
267
+ expect(keysSchema.enum).toEqual({
268
+ name: "name",
269
+ age: "age",
270
+ });
271
+ expect(keysSchema._zod.def.entries).toEqual({
272
+ name: "name",
273
+ age: "age",
274
+ });
275
+ type Keys = z.infer<typeof keysSchema>;
276
+ expectTypeOf<Keys>().toEqualTypeOf<"name" | "age">();
277
+ });
278
+
264
279
  test("inferred partial object type with optional properties", async () => {
265
280
  const Partial = z.object({ a: z.string(), b: z.string().optional() }).partial();
266
281
  type Partial = z.infer<typeof Partial>;
@@ -35,3 +35,15 @@ test("prefault inside object", () => {
35
35
  email: string;
36
36
  }>();
37
37
  });
38
+
39
+ test("object schema with prefault should return shallow clone", () => {
40
+ const schema = z
41
+ .object({
42
+ a: z.string(),
43
+ })
44
+ .default({ a: "x" });
45
+ const result1 = schema.parse(undefined);
46
+ const result2 = schema.parse(undefined);
47
+ expect(result1).not.toBe(result2);
48
+ expect(result1).toEqual(result2);
49
+ });
@@ -446,6 +446,68 @@ test("recursion compatibility", () => {
446
446
  });
447
447
  });
448
448
 
449
+ test("recursive object with .check()", () => {
450
+ const Category = z
451
+ .object({
452
+ id: z.string(),
453
+ name: z.string(),
454
+ get subcategories() {
455
+ return z.array(Category).optional();
456
+ },
457
+ })
458
+ .check((ctx) => {
459
+ // Check for duplicate IDs among direct subcategories
460
+ if (ctx.value.subcategories) {
461
+ const siblingIds = new Set<string>();
462
+ ctx.value.subcategories.forEach((sub, index) => {
463
+ if (siblingIds.has(sub.id)) {
464
+ ctx.issues.push({
465
+ code: "custom",
466
+ message: `Duplicate sibling ID found: ${sub.id}`,
467
+ path: ["subcategories", index, "id"],
468
+ input: ctx.value,
469
+ });
470
+ }
471
+ siblingIds.add(sub.id);
472
+ });
473
+ }
474
+ });
475
+
476
+ // Valid - siblings have unique IDs
477
+ const validData = {
478
+ id: "electronics",
479
+ name: "Electronics",
480
+ subcategories: [
481
+ {
482
+ id: "computers",
483
+ name: "Computers",
484
+ subcategories: [
485
+ { id: "laptops", name: "Laptops" },
486
+ { id: "desktops", name: "Desktops" },
487
+ ],
488
+ },
489
+ {
490
+ id: "phones",
491
+ name: "Phones",
492
+ },
493
+ ],
494
+ };
495
+
496
+ // Invalid - duplicate sibling IDs
497
+ const invalidData = {
498
+ id: "electronics",
499
+ name: "Electronics",
500
+ subcategories: [
501
+ { id: "computers", name: "Computers" },
502
+ { id: "phones", name: "Phones" },
503
+ { id: "computers", name: "Computers Again" }, // Duplicate at index 2
504
+ ],
505
+ };
506
+
507
+ expect(() => Category.parse(validData)).not.toThrow();
508
+ expect(() => Category.parse(invalidData)).toThrow();
509
+ });
510
+
449
511
  // biome-ignore lint: sadf
450
512
  export type RecursiveA = z.ZodUnion<
451
513
  [
@@ -314,8 +314,8 @@ export function _ksuid<T extends schemas.$ZodKSUID>(
314
314
  }
315
315
 
316
316
  // IPv4
317
- export type $ZodIPv4Params = StringFormatParams<schemas.$ZodIPv4, "pattern" | "when">;
318
- export type $ZodCheckIPv4Params = CheckStringFormatParams<schemas.$ZodIPv4, "pattern" | "when">;
317
+ export type $ZodIPv4Params = StringFormatParams<schemas.$ZodIPv4, "pattern" | "when" | "version">;
318
+ export type $ZodCheckIPv4Params = CheckStringFormatParams<schemas.$ZodIPv4, "pattern" | "when" | "version">;
319
319
  export function _ipv4<T extends schemas.$ZodIPv4>(
320
320
  Class: util.SchemaClass<T>,
321
321
  params?: string | $ZodIPv4Params | $ZodCheckIPv4Params
@@ -330,8 +330,8 @@ export function _ipv4<T extends schemas.$ZodIPv4>(
330
330
  }
331
331
 
332
332
  // IPv6
333
- export type $ZodIPv6Params = StringFormatParams<schemas.$ZodIPv6, "pattern" | "when">;
334
- export type $ZodCheckIPv6Params = CheckStringFormatParams<schemas.$ZodIPv6, "pattern" | "when">;
333
+ export type $ZodIPv6Params = StringFormatParams<schemas.$ZodIPv6, "pattern" | "when" | "version">;
334
+ export type $ZodCheckIPv6Params = CheckStringFormatParams<schemas.$ZodIPv6, "pattern" | "when" | "version">;
335
335
  export function _ipv6<T extends schemas.$ZodIPv6>(
336
336
  Class: util.SchemaClass<T>,
337
337
  params?: string | $ZodIPv6Params | $ZodCheckIPv6Params
@@ -1081,12 +1081,15 @@ export interface $ZodTypeDiscriminable extends schemas.$ZodType {
1081
1081
  _zod: $ZodTypeDiscriminableInternals;
1082
1082
  }
1083
1083
  export type $ZodDiscriminatedUnionParams = TypeParams<schemas.$ZodDiscriminatedUnion, "options" | "discriminator">;
1084
- export function _discriminatedUnion<Types extends [$ZodTypeDiscriminable, ...$ZodTypeDiscriminable[]]>(
1084
+ export function _discriminatedUnion<
1085
+ Types extends [$ZodTypeDiscriminable, ...$ZodTypeDiscriminable[]],
1086
+ Disc extends string,
1087
+ >(
1085
1088
  Class: util.SchemaClass<schemas.$ZodDiscriminatedUnion>,
1086
- discriminator: string,
1089
+ discriminator: Disc,
1087
1090
  options: Types,
1088
1091
  params?: string | $ZodDiscriminatedUnionParams
1089
- ): schemas.$ZodDiscriminatedUnion<Types> {
1092
+ ): schemas.$ZodDiscriminatedUnion<Types, Disc> {
1090
1093
  return new Class({
1091
1094
  type: "union",
1092
1095
  options,
@@ -1318,7 +1321,7 @@ export function _default<T extends schemas.$ZodObject>(
1318
1321
  type: "default",
1319
1322
  innerType,
1320
1323
  get defaultValue() {
1321
- return typeof defaultValue === "function" ? (defaultValue as Function)() : defaultValue;
1324
+ return typeof defaultValue === "function" ? (defaultValue as Function)() : util.shallowClone(defaultValue);
1322
1325
  },
1323
1326
  }) as any;
1324
1327
  }
@@ -84,8 +84,8 @@ export class $ZodAsyncError extends Error {
84
84
  // export type output<T extends schemas.$ZodType> = T["_zod"]["output"];
85
85
  // export type input<T extends schemas.$ZodType> = T["_zod"]["input"];
86
86
  // export type output<T extends schemas.$ZodType> = T["_zod"]["output"];
87
- export type input<T> = T extends { _zod: { input: any } } ? Required<T["_zod"]>["input"] : unknown;
88
- export type output<T> = T extends { _zod: { output: any } } ? Required<T["_zod"]>["output"] : unknown;
87
+ export type input<T> = T extends { _zod: { input: any } } ? T["_zod"]["input"] : unknown;
88
+ export type output<T> = T extends { _zod: { output: any } } ? T["_zod"]["output"] : unknown;
89
89
 
90
90
  // Mk2
91
91
  // export type input<T> = T extends { _zod: { "~input": any } }
@@ -2010,20 +2010,27 @@ export const $ZodUnion: core.$constructor<$ZodUnion> = /*@__PURE__*/ core.$const
2010
2010
  //////////////////////////////////////////////////////
2011
2011
  //////////////////////////////////////////////////////
2012
2012
 
2013
- export interface $ZodDiscriminatedUnionDef<Options extends readonly SomeType[] = readonly $ZodType[]>
2014
- extends $ZodUnionDef<Options> {
2015
- discriminator: string;
2013
+ export interface $ZodDiscriminatedUnionDef<
2014
+ Options extends readonly SomeType[] = readonly $ZodType[],
2015
+ Disc extends string = string,
2016
+ > extends $ZodUnionDef<Options> {
2017
+ discriminator: Disc;
2016
2018
  unionFallback?: boolean;
2017
2019
  }
2018
2020
 
2019
- export interface $ZodDiscriminatedUnionInternals<Options extends readonly SomeType[] = readonly $ZodType[]>
2020
- extends $ZodUnionInternals<Options> {
2021
- def: $ZodDiscriminatedUnionDef<Options>;
2021
+ export interface $ZodDiscriminatedUnionInternals<
2022
+ Options extends readonly SomeType[] = readonly $ZodType[],
2023
+ Disc extends string = string,
2024
+ > extends $ZodUnionInternals<Options> {
2025
+ def: $ZodDiscriminatedUnionDef<Options, Disc>;
2022
2026
  propValues: util.PropValues;
2023
2027
  }
2024
2028
 
2025
- export interface $ZodDiscriminatedUnion<T extends readonly SomeType[] = readonly $ZodType[]> extends $ZodType {
2026
- _zod: $ZodDiscriminatedUnionInternals<T>;
2029
+ export interface $ZodDiscriminatedUnion<
2030
+ Options extends readonly SomeType[] = readonly $ZodType[],
2031
+ Disc extends string = string,
2032
+ > extends $ZodType {
2033
+ _zod: $ZodDiscriminatedUnionInternals<Options, Disc>;
2027
2034
  }
2028
2035
 
2029
2036
  export const $ZodDiscriminatedUnion: core.$constructor<$ZodDiscriminatedUnion> =
@@ -284,6 +284,10 @@ export function defineLazy<T, K extends keyof T>(object: T, key: K, getter: () =
284
284
  });
285
285
  }
286
286
 
287
+ export function objectClone(obj: object) {
288
+ return Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
289
+ }
290
+
287
291
  export function assignProp<T extends object, K extends PropertyKey>(
288
292
  target: T,
289
293
  prop: K,
@@ -385,6 +389,11 @@ export function isPlainObject(o: any): o is Record<PropertyKey, unknown> {
385
389
  return true;
386
390
  }
387
391
 
392
+ export function shallowClone(o: any): any {
393
+ if (isPlainObject(o)) return { ...o };
394
+ return o;
395
+ }
396
+
388
397
  export function numKeys(data: any): number {
389
398
  let keyCount = 0;
390
399
  for (const key in data) {
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
3
  minor: 0,
4
- patch: 14 as number,
4
+ patch: 16 as number,
5
5
  } as const;
@@ -712,9 +712,9 @@ export function array<T extends SomeType>(element: SomeType, params?: any): ZodM
712
712
  }
713
713
 
714
714
  // .keyof
715
- export function keyof<T extends ZodMiniObject>(schema: T): ZodMiniLiteral<Exclude<keyof T["shape"], symbol>> {
715
+ export function keyof<T extends ZodMiniObject>(schema: T): ZodMiniEnum<util.KeysEnum<T["shape"]>> {
716
716
  const shape = schema._zod.def.shape;
717
- return literal(Object.keys(shape)) as any;
717
+ return _enum(Object.keys(shape)) as any;
718
718
  }
719
719
 
720
720
  // ZodMiniObject
@@ -907,9 +907,11 @@ export function union<const T extends readonly SomeType[]>(
907
907
  }
908
908
 
909
909
  // ZodMiniDiscriminatedUnion
910
- export interface ZodMiniDiscriminatedUnion<Options extends readonly SomeType[] = readonly core.$ZodType[]>
911
- extends ZodMiniUnion<Options> {
912
- _zod: core.$ZodDiscriminatedUnionInternals<Options>;
910
+ export interface ZodMiniDiscriminatedUnion<
911
+ Options extends readonly SomeType[] = readonly core.$ZodType[],
912
+ Disc extends string = string,
913
+ > extends ZodMiniUnion<Options> {
914
+ _zod: core.$ZodDiscriminatedUnionInternals<Options, Disc>;
913
915
  }
914
916
  export const ZodMiniDiscriminatedUnion: core.$constructor<ZodMiniDiscriminatedUnion> = /*@__PURE__*/ core.$constructor(
915
917
  "ZodMiniDiscriminatedUnion",
@@ -921,17 +923,18 @@ export const ZodMiniDiscriminatedUnion: core.$constructor<ZodMiniDiscriminatedUn
921
923
 
922
924
  export function discriminatedUnion<
923
925
  Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]],
926
+ Disc extends string,
924
927
  >(
925
- discriminator: string,
928
+ discriminator: Disc,
926
929
  options: Types,
927
930
  params?: string | core.$ZodDiscriminatedUnionParams
928
- ): ZodMiniDiscriminatedUnion<Types> {
931
+ ): ZodMiniDiscriminatedUnion<Types, Disc> {
929
932
  return new ZodMiniDiscriminatedUnion({
930
933
  type: "union",
931
934
  options,
932
935
  discriminator,
933
936
  ...util.normalizeParams(params),
934
- }) as ZodMiniDiscriminatedUnion<Types>;
937
+ }) as ZodMiniDiscriminatedUnion<Types, Disc>;
935
938
  }
936
939
 
937
940
  // ZodMiniIntersection
@@ -1253,7 +1256,7 @@ export function _default<T extends SomeType>(
1253
1256
  type: "default",
1254
1257
  innerType: innerType as any as core.$ZodType,
1255
1258
  get defaultValue() {
1256
- return typeof defaultValue === "function" ? (defaultValue as Function)() : defaultValue;
1259
+ return typeof defaultValue === "function" ? (defaultValue as Function)() : util.shallowClone(defaultValue);
1257
1260
  },
1258
1261
  }) as any;
1259
1262
  }
@@ -1278,7 +1281,7 @@ export function prefault<T extends SomeType>(
1278
1281
  type: "prefault",
1279
1282
  innerType: innerType as any as core.$ZodType,
1280
1283
  get defaultValue() {
1281
- return typeof defaultValue === "function" ? (defaultValue as Function)() : defaultValue;
1284
+ return typeof defaultValue === "function" ? (defaultValue as Function)() : util.shallowClone(defaultValue);
1282
1285
  },
1283
1286
  }) as any;
1284
1287
  }
@@ -869,3 +869,16 @@ test("def typing", () => {
869
869
  z.catch(z.string(), "fallback").def.type satisfies "catch";
870
870
  z.file().def.type satisfies "file";
871
871
  });
872
+
873
+ test("defaulted object schema returns shallow clone", () => {
874
+ const schema = z._default(
875
+ z.object({
876
+ a: z.string(),
877
+ }),
878
+ { a: "x" }
879
+ );
880
+ const result1 = schema.parse(undefined);
881
+ const result2 = schema.parse(undefined);
882
+ expect(result1).not.toBe(result2);
883
+ expect(result1).toEqual(result2);
884
+ });
@@ -89,6 +89,12 @@ test("z.keyof", () => {
89
89
  type UserKeys = z.infer<typeof userKeysSchema>;
90
90
  expectTypeOf<UserKeys>().toEqualTypeOf<"name" | "age" | "email">();
91
91
  expect(userKeysSchema).toBeDefined();
92
+ expect(userKeysSchema._zod.def.type).toBe("enum");
93
+ expect(userKeysSchema._zod.def.entries).toEqual({
94
+ name: "name",
95
+ age: "age",
96
+ email: "email",
97
+ });
92
98
  expect(z.safeParse(userKeysSchema, "name").success).toBe(true);
93
99
  expect(z.safeParse(userKeysSchema, "age").success).toBe(true);
94
100
  expect(z.safeParse(userKeysSchema, "email").success).toBe(true);
@@ -590,7 +590,7 @@ function array(element, params) {
590
590
  // .keyof
591
591
  function keyof(schema) {
592
592
  const shape = schema._zod.def.shape;
593
- return literal(Object.keys(shape));
593
+ return _enum(Object.keys(shape));
594
594
  }
595
595
  exports.ZodObject = core.$constructor("ZodObject", (inst, def) => {
596
596
  core.$ZodObject.init(inst, def);
@@ -615,7 +615,7 @@ function object(shape, params) {
615
615
  const def = {
616
616
  type: "object",
617
617
  get shape() {
618
- index_js_1.util.assignProp(this, "shape", { ...shape });
618
+ index_js_1.util.assignProp(this, "shape", shape ? index_js_1.util.objectClone(shape) : {});
619
619
  return this.shape;
620
620
  },
621
621
  ...index_js_1.util.normalizeParams(params),
@@ -627,7 +627,7 @@ function strictObject(shape, params) {
627
627
  return new exports.ZodObject({
628
628
  type: "object",
629
629
  get shape() {
630
- index_js_1.util.assignProp(this, "shape", { ...shape });
630
+ index_js_1.util.assignProp(this, "shape", index_js_1.util.objectClone(shape));
631
631
  return this.shape;
632
632
  },
633
633
  catchall: never(),
@@ -639,7 +639,7 @@ function looseObject(shape, params) {
639
639
  return new exports.ZodObject({
640
640
  type: "object",
641
641
  get shape() {
642
- index_js_1.util.assignProp(this, "shape", { ...shape });
642
+ index_js_1.util.assignProp(this, "shape", index_js_1.util.objectClone(shape));
643
643
  return this.shape;
644
644
  },
645
645
  catchall: unknown(),
@@ -920,7 +920,7 @@ function _default(innerType, defaultValue) {
920
920
  type: "default",
921
921
  innerType: innerType,
922
922
  get defaultValue() {
923
- return typeof defaultValue === "function" ? defaultValue() : defaultValue;
923
+ return typeof defaultValue === "function" ? defaultValue() : index_js_1.util.shallowClone(defaultValue);
924
924
  },
925
925
  });
926
926
  }
@@ -934,7 +934,7 @@ function prefault(innerType, defaultValue) {
934
934
  type: "prefault",
935
935
  innerType: innerType,
936
936
  get defaultValue() {
937
- return typeof defaultValue === "function" ? defaultValue() : defaultValue;
937
+ return typeof defaultValue === "function" ? defaultValue() : index_js_1.util.shallowClone(defaultValue);
938
938
  },
939
939
  });
940
940
  }
@@ -395,7 +395,7 @@ export interface ZodArray<T extends core.SomeType = core.$ZodType> extends _ZodT
395
395
  }
396
396
  export declare const ZodArray: core.$constructor<ZodArray>;
397
397
  export declare function array<T extends core.SomeType>(element: T, params?: string | core.$ZodArrayParams): ZodArray<T>;
398
- export declare function keyof<T extends ZodObject>(schema: T): ZodLiteral<Exclude<keyof T["_zod"]["output"], symbol>>;
398
+ export declare function keyof<T extends ZodObject>(schema: T): ZodEnum<util.KeysEnum<T["_zod"]["output"]>>;
399
399
  export interface ZodObject<
400
400
  /** @ts-ignore Cast variance */
401
401
  out Shape extends core.$ZodShape = core.$ZodLooseShape, out Config extends core.$ZodObjectConfig = core.$strip> extends _ZodType<core.$ZodObjectInternals<Shape, Config>>, core.$ZodObject<Shape, Config> {
@@ -440,12 +440,12 @@ export interface ZodUnion<T extends readonly core.SomeType[] = readonly core.$Zo
440
440
  }
441
441
  export declare const ZodUnion: core.$constructor<ZodUnion>;
442
442
  export declare function union<const T extends readonly core.SomeType[]>(options: T, params?: string | core.$ZodUnionParams): ZodUnion<T>;
443
- export interface ZodDiscriminatedUnion<Options extends readonly core.SomeType[] = readonly core.$ZodType[]> extends ZodUnion<Options>, core.$ZodDiscriminatedUnion<Options> {
444
- _zod: core.$ZodDiscriminatedUnionInternals<Options>;
445
- def: core.$ZodDiscriminatedUnionDef<Options>;
443
+ export interface ZodDiscriminatedUnion<Options extends readonly core.SomeType[] = readonly core.$ZodType[], Disc extends string = string> extends ZodUnion<Options>, core.$ZodDiscriminatedUnion<Options, Disc> {
444
+ _zod: core.$ZodDiscriminatedUnionInternals<Options, Disc>;
445
+ def: core.$ZodDiscriminatedUnionDef<Options, Disc>;
446
446
  }
447
447
  export declare const ZodDiscriminatedUnion: core.$constructor<ZodDiscriminatedUnion>;
448
- export declare function discriminatedUnion<Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]]>(discriminator: string, options: Types, params?: string | core.$ZodDiscriminatedUnionParams): ZodDiscriminatedUnion<Types>;
448
+ export declare function discriminatedUnion<Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]], Disc extends string>(discriminator: Disc, options: Types, params?: string | core.$ZodDiscriminatedUnionParams): ZodDiscriminatedUnion<Types, Disc>;
449
449
  export interface ZodIntersection<A extends core.SomeType = core.$ZodType, B extends core.SomeType = core.$ZodType> extends _ZodType<core.$ZodIntersectionInternals<A, B>>, core.$ZodIntersection<A, B> {
450
450
  }
451
451
  export declare const ZodIntersection: core.$constructor<ZodIntersection>;
@@ -395,7 +395,7 @@ export interface ZodArray<T extends core.SomeType = core.$ZodType> extends _ZodT
395
395
  }
396
396
  export declare const ZodArray: core.$constructor<ZodArray>;
397
397
  export declare function array<T extends core.SomeType>(element: T, params?: string | core.$ZodArrayParams): ZodArray<T>;
398
- export declare function keyof<T extends ZodObject>(schema: T): ZodLiteral<Exclude<keyof T["_zod"]["output"], symbol>>;
398
+ export declare function keyof<T extends ZodObject>(schema: T): ZodEnum<util.KeysEnum<T["_zod"]["output"]>>;
399
399
  export interface ZodObject<
400
400
  /** @ts-ignore Cast variance */
401
401
  out Shape extends core.$ZodShape = core.$ZodLooseShape, out Config extends core.$ZodObjectConfig = core.$strip> extends _ZodType<core.$ZodObjectInternals<Shape, Config>>, core.$ZodObject<Shape, Config> {
@@ -440,12 +440,12 @@ export interface ZodUnion<T extends readonly core.SomeType[] = readonly core.$Zo
440
440
  }
441
441
  export declare const ZodUnion: core.$constructor<ZodUnion>;
442
442
  export declare function union<const T extends readonly core.SomeType[]>(options: T, params?: string | core.$ZodUnionParams): ZodUnion<T>;
443
- export interface ZodDiscriminatedUnion<Options extends readonly core.SomeType[] = readonly core.$ZodType[]> extends ZodUnion<Options>, core.$ZodDiscriminatedUnion<Options> {
444
- _zod: core.$ZodDiscriminatedUnionInternals<Options>;
445
- def: core.$ZodDiscriminatedUnionDef<Options>;
443
+ export interface ZodDiscriminatedUnion<Options extends readonly core.SomeType[] = readonly core.$ZodType[], Disc extends string = string> extends ZodUnion<Options>, core.$ZodDiscriminatedUnion<Options, Disc> {
444
+ _zod: core.$ZodDiscriminatedUnionInternals<Options, Disc>;
445
+ def: core.$ZodDiscriminatedUnionDef<Options, Disc>;
446
446
  }
447
447
  export declare const ZodDiscriminatedUnion: core.$constructor<ZodDiscriminatedUnion>;
448
- export declare function discriminatedUnion<Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]]>(discriminator: string, options: Types, params?: string | core.$ZodDiscriminatedUnionParams): ZodDiscriminatedUnion<Types>;
448
+ export declare function discriminatedUnion<Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]], Disc extends string>(discriminator: Disc, options: Types, params?: string | core.$ZodDiscriminatedUnionParams): ZodDiscriminatedUnion<Types, Disc>;
449
449
  export interface ZodIntersection<A extends core.SomeType = core.$ZodType, B extends core.SomeType = core.$ZodType> extends _ZodType<core.$ZodIntersectionInternals<A, B>>, core.$ZodIntersection<A, B> {
450
450
  }
451
451
  export declare const ZodIntersection: core.$constructor<ZodIntersection>;
@@ -484,7 +484,7 @@ export function array(element, params) {
484
484
  // .keyof
485
485
  export function keyof(schema) {
486
486
  const shape = schema._zod.def.shape;
487
- return literal(Object.keys(shape));
487
+ return _enum(Object.keys(shape));
488
488
  }
489
489
  export const ZodObject = /*@__PURE__*/ core.$constructor("ZodObject", (inst, def) => {
490
490
  core.$ZodObject.init(inst, def);
@@ -509,7 +509,7 @@ export function object(shape, params) {
509
509
  const def = {
510
510
  type: "object",
511
511
  get shape() {
512
- util.assignProp(this, "shape", { ...shape });
512
+ util.assignProp(this, "shape", shape ? util.objectClone(shape) : {});
513
513
  return this.shape;
514
514
  },
515
515
  ...util.normalizeParams(params),
@@ -521,7 +521,7 @@ export function strictObject(shape, params) {
521
521
  return new ZodObject({
522
522
  type: "object",
523
523
  get shape() {
524
- util.assignProp(this, "shape", { ...shape });
524
+ util.assignProp(this, "shape", util.objectClone(shape));
525
525
  return this.shape;
526
526
  },
527
527
  catchall: never(),
@@ -533,7 +533,7 @@ export function looseObject(shape, params) {
533
533
  return new ZodObject({
534
534
  type: "object",
535
535
  get shape() {
536
- util.assignProp(this, "shape", { ...shape });
536
+ util.assignProp(this, "shape", util.objectClone(shape));
537
537
  return this.shape;
538
538
  },
539
539
  catchall: unknown(),
@@ -815,7 +815,7 @@ export function _default(innerType, defaultValue) {
815
815
  type: "default",
816
816
  innerType: innerType,
817
817
  get defaultValue() {
818
- return typeof defaultValue === "function" ? defaultValue() : defaultValue;
818
+ return typeof defaultValue === "function" ? defaultValue() : util.shallowClone(defaultValue);
819
819
  },
820
820
  });
821
821
  }
@@ -829,7 +829,7 @@ export function prefault(innerType, defaultValue) {
829
829
  type: "prefault",
830
830
  innerType: innerType,
831
831
  get defaultValue() {
832
- return typeof defaultValue === "function" ? defaultValue() : defaultValue;
832
+ return typeof defaultValue === "function" ? defaultValue() : util.shallowClone(defaultValue);
833
833
  },
834
834
  });
835
835
  }
package/v4/core/api.cjs CHANGED
@@ -882,7 +882,7 @@ function _default(Class, innerType, defaultValue) {
882
882
  type: "default",
883
883
  innerType,
884
884
  get defaultValue() {
885
- return typeof defaultValue === "function" ? defaultValue() : defaultValue;
885
+ return typeof defaultValue === "function" ? defaultValue() : util.shallowClone(defaultValue);
886
886
  },
887
887
  });
888
888
  }
package/v4/core/api.d.cts CHANGED
@@ -63,11 +63,11 @@ export declare function _xid<T extends schemas.$ZodXID>(Class: util.SchemaClass<
63
63
  export type $ZodKSUIDParams = StringFormatParams<schemas.$ZodKSUID, "when">;
64
64
  export type $ZodCheckKSUIDParams = CheckStringFormatParams<schemas.$ZodKSUID, "when">;
65
65
  export declare function _ksuid<T extends schemas.$ZodKSUID>(Class: util.SchemaClass<T>, params?: string | $ZodKSUIDParams | $ZodCheckKSUIDParams): T;
66
- export type $ZodIPv4Params = StringFormatParams<schemas.$ZodIPv4, "pattern" | "when">;
67
- export type $ZodCheckIPv4Params = CheckStringFormatParams<schemas.$ZodIPv4, "pattern" | "when">;
66
+ export type $ZodIPv4Params = StringFormatParams<schemas.$ZodIPv4, "pattern" | "when" | "version">;
67
+ export type $ZodCheckIPv4Params = CheckStringFormatParams<schemas.$ZodIPv4, "pattern" | "when" | "version">;
68
68
  export declare function _ipv4<T extends schemas.$ZodIPv4>(Class: util.SchemaClass<T>, params?: string | $ZodIPv4Params | $ZodCheckIPv4Params): T;
69
- export type $ZodIPv6Params = StringFormatParams<schemas.$ZodIPv6, "pattern" | "when">;
70
- export type $ZodCheckIPv6Params = CheckStringFormatParams<schemas.$ZodIPv6, "pattern" | "when">;
69
+ export type $ZodIPv6Params = StringFormatParams<schemas.$ZodIPv6, "pattern" | "when" | "version">;
70
+ export type $ZodCheckIPv6Params = CheckStringFormatParams<schemas.$ZodIPv6, "pattern" | "when" | "version">;
71
71
  export declare function _ipv6<T extends schemas.$ZodIPv6>(Class: util.SchemaClass<T>, params?: string | $ZodIPv6Params | $ZodCheckIPv6Params): T;
72
72
  export type $ZodCIDRv4Params = StringFormatParams<schemas.$ZodCIDRv4, "pattern" | "when">;
73
73
  export type $ZodCheckCIDRv4Params = CheckStringFormatParams<schemas.$ZodCIDRv4, "pattern" | "when">;
@@ -210,7 +210,7 @@ export interface $ZodTypeDiscriminable extends schemas.$ZodType {
210
210
  _zod: $ZodTypeDiscriminableInternals;
211
211
  }
212
212
  export type $ZodDiscriminatedUnionParams = TypeParams<schemas.$ZodDiscriminatedUnion, "options" | "discriminator">;
213
- export declare function _discriminatedUnion<Types extends [$ZodTypeDiscriminable, ...$ZodTypeDiscriminable[]]>(Class: util.SchemaClass<schemas.$ZodDiscriminatedUnion>, discriminator: string, options: Types, params?: string | $ZodDiscriminatedUnionParams): schemas.$ZodDiscriminatedUnion<Types>;
213
+ export declare function _discriminatedUnion<Types extends [$ZodTypeDiscriminable, ...$ZodTypeDiscriminable[]], Disc extends string>(Class: util.SchemaClass<schemas.$ZodDiscriminatedUnion>, discriminator: Disc, options: Types, params?: string | $ZodDiscriminatedUnionParams): schemas.$ZodDiscriminatedUnion<Types, Disc>;
214
214
  export type $ZodIntersectionParams = TypeParams<schemas.$ZodIntersection, "left" | "right">;
215
215
  export declare function _intersection<T extends schemas.$ZodObject, U extends schemas.$ZodObject>(Class: util.SchemaClass<schemas.$ZodIntersection>, left: T, right: U): schemas.$ZodIntersection<T, U>;
216
216
  export type $ZodTupleParams = TypeParams<schemas.$ZodTuple, "items" | "rest">;
package/v4/core/api.d.ts CHANGED
@@ -63,11 +63,11 @@ export declare function _xid<T extends schemas.$ZodXID>(Class: util.SchemaClass<
63
63
  export type $ZodKSUIDParams = StringFormatParams<schemas.$ZodKSUID, "when">;
64
64
  export type $ZodCheckKSUIDParams = CheckStringFormatParams<schemas.$ZodKSUID, "when">;
65
65
  export declare function _ksuid<T extends schemas.$ZodKSUID>(Class: util.SchemaClass<T>, params?: string | $ZodKSUIDParams | $ZodCheckKSUIDParams): T;
66
- export type $ZodIPv4Params = StringFormatParams<schemas.$ZodIPv4, "pattern" | "when">;
67
- export type $ZodCheckIPv4Params = CheckStringFormatParams<schemas.$ZodIPv4, "pattern" | "when">;
66
+ export type $ZodIPv4Params = StringFormatParams<schemas.$ZodIPv4, "pattern" | "when" | "version">;
67
+ export type $ZodCheckIPv4Params = CheckStringFormatParams<schemas.$ZodIPv4, "pattern" | "when" | "version">;
68
68
  export declare function _ipv4<T extends schemas.$ZodIPv4>(Class: util.SchemaClass<T>, params?: string | $ZodIPv4Params | $ZodCheckIPv4Params): T;
69
- export type $ZodIPv6Params = StringFormatParams<schemas.$ZodIPv6, "pattern" | "when">;
70
- export type $ZodCheckIPv6Params = CheckStringFormatParams<schemas.$ZodIPv6, "pattern" | "when">;
69
+ export type $ZodIPv6Params = StringFormatParams<schemas.$ZodIPv6, "pattern" | "when" | "version">;
70
+ export type $ZodCheckIPv6Params = CheckStringFormatParams<schemas.$ZodIPv6, "pattern" | "when" | "version">;
71
71
  export declare function _ipv6<T extends schemas.$ZodIPv6>(Class: util.SchemaClass<T>, params?: string | $ZodIPv6Params | $ZodCheckIPv6Params): T;
72
72
  export type $ZodCIDRv4Params = StringFormatParams<schemas.$ZodCIDRv4, "pattern" | "when">;
73
73
  export type $ZodCheckCIDRv4Params = CheckStringFormatParams<schemas.$ZodCIDRv4, "pattern" | "when">;
@@ -210,7 +210,7 @@ export interface $ZodTypeDiscriminable extends schemas.$ZodType {
210
210
  _zod: $ZodTypeDiscriminableInternals;
211
211
  }
212
212
  export type $ZodDiscriminatedUnionParams = TypeParams<schemas.$ZodDiscriminatedUnion, "options" | "discriminator">;
213
- export declare function _discriminatedUnion<Types extends [$ZodTypeDiscriminable, ...$ZodTypeDiscriminable[]]>(Class: util.SchemaClass<schemas.$ZodDiscriminatedUnion>, discriminator: string, options: Types, params?: string | $ZodDiscriminatedUnionParams): schemas.$ZodDiscriminatedUnion<Types>;
213
+ export declare function _discriminatedUnion<Types extends [$ZodTypeDiscriminable, ...$ZodTypeDiscriminable[]], Disc extends string>(Class: util.SchemaClass<schemas.$ZodDiscriminatedUnion>, discriminator: Disc, options: Types, params?: string | $ZodDiscriminatedUnionParams): schemas.$ZodDiscriminatedUnion<Types, Disc>;
214
214
  export type $ZodIntersectionParams = TypeParams<schemas.$ZodIntersection, "left" | "right">;
215
215
  export declare function _intersection<T extends schemas.$ZodObject, U extends schemas.$ZodObject>(Class: util.SchemaClass<schemas.$ZodIntersection>, left: T, right: U): schemas.$ZodIntersection<T, U>;
216
216
  export type $ZodTupleParams = TypeParams<schemas.$ZodTuple, "items" | "rest">;
package/v4/core/api.js CHANGED
@@ -747,7 +747,7 @@ export function _default(Class, innerType, defaultValue) {
747
747
  type: "default",
748
748
  innerType,
749
749
  get defaultValue() {
750
- return typeof defaultValue === "function" ? defaultValue() : defaultValue;
750
+ return typeof defaultValue === "function" ? defaultValue() : util.shallowClone(defaultValue);
751
751
  },
752
752
  });
753
753
  }
@@ -30,12 +30,12 @@ export type input<T> = T extends {
30
30
  _zod: {
31
31
  input: any;
32
32
  };
33
- } ? Required<T["_zod"]>["input"] : unknown;
33
+ } ? T["_zod"]["input"] : unknown;
34
34
  export type output<T> = T extends {
35
35
  _zod: {
36
36
  output: any;
37
37
  };
38
- } ? Required<T["_zod"]>["output"] : unknown;
38
+ } ? T["_zod"]["output"] : unknown;
39
39
  export type { output as infer };
40
40
  export interface $ZodConfig {
41
41
  /** Custom error map. Overrides `config().localeError`. */
package/v4/core/core.d.ts CHANGED
@@ -30,12 +30,12 @@ export type input<T> = T extends {
30
30
  _zod: {
31
31
  input: any;
32
32
  };
33
- } ? Required<T["_zod"]>["input"] : unknown;
33
+ } ? T["_zod"]["input"] : unknown;
34
34
  export type output<T> = T extends {
35
35
  _zod: {
36
36
  output: any;
37
37
  };
38
- } ? Required<T["_zod"]>["output"] : unknown;
38
+ } ? T["_zod"]["output"] : unknown;
39
39
  export type { output as infer };
40
40
  export interface $ZodConfig {
41
41
  /** Custom error map. Overrides `config().localeError`. */
@@ -619,16 +619,16 @@ export interface $ZodUnion<T extends readonly SomeType[] = readonly $ZodType[]>
619
619
  _zod: $ZodUnionInternals<T>;
620
620
  }
621
621
  export declare const $ZodUnion: core.$constructor<$ZodUnion>;
622
- export interface $ZodDiscriminatedUnionDef<Options extends readonly SomeType[] = readonly $ZodType[]> extends $ZodUnionDef<Options> {
623
- discriminator: string;
622
+ export interface $ZodDiscriminatedUnionDef<Options extends readonly SomeType[] = readonly $ZodType[], Disc extends string = string> extends $ZodUnionDef<Options> {
623
+ discriminator: Disc;
624
624
  unionFallback?: boolean;
625
625
  }
626
- export interface $ZodDiscriminatedUnionInternals<Options extends readonly SomeType[] = readonly $ZodType[]> extends $ZodUnionInternals<Options> {
627
- def: $ZodDiscriminatedUnionDef<Options>;
626
+ export interface $ZodDiscriminatedUnionInternals<Options extends readonly SomeType[] = readonly $ZodType[], Disc extends string = string> extends $ZodUnionInternals<Options> {
627
+ def: $ZodDiscriminatedUnionDef<Options, Disc>;
628
628
  propValues: util.PropValues;
629
629
  }
630
- export interface $ZodDiscriminatedUnion<T extends readonly SomeType[] = readonly $ZodType[]> extends $ZodType {
631
- _zod: $ZodDiscriminatedUnionInternals<T>;
630
+ export interface $ZodDiscriminatedUnion<Options extends readonly SomeType[] = readonly $ZodType[], Disc extends string = string> extends $ZodType {
631
+ _zod: $ZodDiscriminatedUnionInternals<Options, Disc>;
632
632
  }
633
633
  export declare const $ZodDiscriminatedUnion: core.$constructor<$ZodDiscriminatedUnion>;
634
634
  export interface $ZodIntersectionDef<Left extends SomeType = $ZodType, Right extends SomeType = $ZodType> extends $ZodTypeDef {
@@ -619,16 +619,16 @@ export interface $ZodUnion<T extends readonly SomeType[] = readonly $ZodType[]>
619
619
  _zod: $ZodUnionInternals<T>;
620
620
  }
621
621
  export declare const $ZodUnion: core.$constructor<$ZodUnion>;
622
- export interface $ZodDiscriminatedUnionDef<Options extends readonly SomeType[] = readonly $ZodType[]> extends $ZodUnionDef<Options> {
623
- discriminator: string;
622
+ export interface $ZodDiscriminatedUnionDef<Options extends readonly SomeType[] = readonly $ZodType[], Disc extends string = string> extends $ZodUnionDef<Options> {
623
+ discriminator: Disc;
624
624
  unionFallback?: boolean;
625
625
  }
626
- export interface $ZodDiscriminatedUnionInternals<Options extends readonly SomeType[] = readonly $ZodType[]> extends $ZodUnionInternals<Options> {
627
- def: $ZodDiscriminatedUnionDef<Options>;
626
+ export interface $ZodDiscriminatedUnionInternals<Options extends readonly SomeType[] = readonly $ZodType[], Disc extends string = string> extends $ZodUnionInternals<Options> {
627
+ def: $ZodDiscriminatedUnionDef<Options, Disc>;
628
628
  propValues: util.PropValues;
629
629
  }
630
- export interface $ZodDiscriminatedUnion<T extends readonly SomeType[] = readonly $ZodType[]> extends $ZodType {
631
- _zod: $ZodDiscriminatedUnionInternals<T>;
630
+ export interface $ZodDiscriminatedUnion<Options extends readonly SomeType[] = readonly $ZodType[], Disc extends string = string> extends $ZodType {
631
+ _zod: $ZodDiscriminatedUnionInternals<Options, Disc>;
632
632
  }
633
633
  export declare const $ZodDiscriminatedUnion: core.$constructor<$ZodDiscriminatedUnion>;
634
634
  export interface $ZodIntersectionDef<Left extends SomeType = $ZodType, Right extends SomeType = $ZodType> extends $ZodTypeDef {
package/v4/core/util.cjs CHANGED
@@ -14,6 +14,7 @@ exports.nullish = nullish;
14
14
  exports.cleanRegex = cleanRegex;
15
15
  exports.floatSafeRemainder = floatSafeRemainder;
16
16
  exports.defineLazy = defineLazy;
17
+ exports.objectClone = objectClone;
17
18
  exports.assignProp = assignProp;
18
19
  exports.mergeDefs = mergeDefs;
19
20
  exports.cloneDef = cloneDef;
@@ -23,6 +24,7 @@ exports.randomString = randomString;
23
24
  exports.esc = esc;
24
25
  exports.isObject = isObject;
25
26
  exports.isPlainObject = isPlainObject;
27
+ exports.shallowClone = shallowClone;
26
28
  exports.numKeys = numKeys;
27
29
  exports.escapeRegex = escapeRegex;
28
30
  exports.clone = clone;
@@ -132,6 +134,9 @@ function defineLazy(object, key, getter) {
132
134
  configurable: true,
133
135
  });
134
136
  }
137
+ function objectClone(obj) {
138
+ return Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
139
+ }
135
140
  function assignProp(target, prop, value) {
136
141
  Object.defineProperty(target, prop, {
137
142
  value,
@@ -213,6 +218,11 @@ function isPlainObject(o) {
213
218
  }
214
219
  return true;
215
220
  }
221
+ function shallowClone(o) {
222
+ if (isPlainObject(o))
223
+ return { ...o };
224
+ return o;
225
+ }
216
226
  function numKeys(data) {
217
227
  let keyCount = 0;
218
228
  for (const key in data) {
@@ -120,6 +120,7 @@ export declare function nullish(input: any): boolean;
120
120
  export declare function cleanRegex(source: string): string;
121
121
  export declare function floatSafeRemainder(val: number, step: number): number;
122
122
  export declare function defineLazy<T, K extends keyof T>(object: T, key: K, getter: () => T[K]): void;
123
+ export declare function objectClone(obj: object): any;
123
124
  export declare function assignProp<T extends object, K extends PropertyKey>(target: T, prop: K, value: K extends keyof T ? T[K] : any): void;
124
125
  export declare function mergeDefs(...defs: Record<string, any>[]): any;
125
126
  export declare function cloneDef(schema: schemas.$ZodType): any;
@@ -135,6 +136,7 @@ export declare const allowsEval: {
135
136
  value: boolean;
136
137
  };
137
138
  export declare function isPlainObject(o: any): o is Record<PropertyKey, unknown>;
139
+ export declare function shallowClone(o: any): any;
138
140
  export declare function numKeys(data: any): number;
139
141
  export declare const getParsedType: (data: any) => ParsedTypes;
140
142
  export declare const propertyKeyTypes: Set<string>;
package/v4/core/util.d.ts CHANGED
@@ -120,6 +120,7 @@ export declare function nullish(input: any): boolean;
120
120
  export declare function cleanRegex(source: string): string;
121
121
  export declare function floatSafeRemainder(val: number, step: number): number;
122
122
  export declare function defineLazy<T, K extends keyof T>(object: T, key: K, getter: () => T[K]): void;
123
+ export declare function objectClone(obj: object): any;
123
124
  export declare function assignProp<T extends object, K extends PropertyKey>(target: T, prop: K, value: K extends keyof T ? T[K] : any): void;
124
125
  export declare function mergeDefs(...defs: Record<string, any>[]): any;
125
126
  export declare function cloneDef(schema: schemas.$ZodType): any;
@@ -135,6 +136,7 @@ export declare const allowsEval: {
135
136
  value: boolean;
136
137
  };
137
138
  export declare function isPlainObject(o: any): o is Record<PropertyKey, unknown>;
139
+ export declare function shallowClone(o: any): any;
138
140
  export declare function numKeys(data: any): number;
139
141
  export declare const getParsedType: (data: any) => ParsedTypes;
140
142
  export declare const propertyKeyTypes: Set<string>;
package/v4/core/util.js CHANGED
@@ -86,6 +86,9 @@ export function defineLazy(object, key, getter) {
86
86
  configurable: true,
87
87
  });
88
88
  }
89
+ export function objectClone(obj) {
90
+ return Object.create(Object.getPrototypeOf(obj), Object.getOwnPropertyDescriptors(obj));
91
+ }
89
92
  export function assignProp(target, prop, value) {
90
93
  Object.defineProperty(target, prop, {
91
94
  value,
@@ -167,6 +170,11 @@ export function isPlainObject(o) {
167
170
  }
168
171
  return true;
169
172
  }
173
+ export function shallowClone(o) {
174
+ if (isPlainObject(o))
175
+ return { ...o };
176
+ return o;
177
+ }
170
178
  export function numKeys(data) {
171
179
  let keyCount = 0;
172
180
  for (const key in data) {
@@ -4,5 +4,5 @@ exports.version = void 0;
4
4
  exports.version = {
5
5
  major: 4,
6
6
  minor: 0,
7
- patch: 14,
7
+ patch: 16,
8
8
  };
@@ -1,5 +1,5 @@
1
1
  export const version = {
2
2
  major: 4,
3
3
  minor: 0,
4
- patch: 14,
4
+ patch: 16,
5
5
  };
@@ -435,7 +435,7 @@ function array(element, params) {
435
435
  // .keyof
436
436
  function keyof(schema) {
437
437
  const shape = schema._zod.def.shape;
438
- return literal(Object.keys(shape));
438
+ return _enum(Object.keys(shape));
439
439
  }
440
440
  exports.ZodMiniObject = core.$constructor("ZodMiniObject", (inst, def) => {
441
441
  core.$ZodObject.init(inst, def);
@@ -686,7 +686,7 @@ function _default(innerType, defaultValue) {
686
686
  type: "default",
687
687
  innerType: innerType,
688
688
  get defaultValue() {
689
- return typeof defaultValue === "function" ? defaultValue() : defaultValue;
689
+ return typeof defaultValue === "function" ? defaultValue() : index_js_1.util.shallowClone(defaultValue);
690
690
  },
691
691
  });
692
692
  }
@@ -699,7 +699,7 @@ function prefault(innerType, defaultValue) {
699
699
  type: "prefault",
700
700
  innerType: innerType,
701
701
  get defaultValue() {
702
- return typeof defaultValue === "function" ? defaultValue() : defaultValue;
702
+ return typeof defaultValue === "function" ? defaultValue() : index_js_1.util.shallowClone(defaultValue);
703
703
  },
704
704
  });
705
705
  }
@@ -179,7 +179,7 @@ export interface ZodMiniArray<T extends SomeType = core.$ZodType> extends _ZodMi
179
179
  }
180
180
  export declare const ZodMiniArray: core.$constructor<ZodMiniArray>;
181
181
  export declare function array<T extends SomeType>(element: T, params?: string | core.$ZodArrayParams): ZodMiniArray<T>;
182
- export declare function keyof<T extends ZodMiniObject>(schema: T): ZodMiniLiteral<Exclude<keyof T["shape"], symbol>>;
182
+ export declare function keyof<T extends ZodMiniObject>(schema: T): ZodMiniEnum<util.KeysEnum<T["shape"]>>;
183
183
  export interface ZodMiniObject<
184
184
  /** @ts-ignore Cast variance */
185
185
  out Shape extends core.$ZodShape = core.$ZodShape, out Config extends core.$ZodObjectConfig = core.$strip> extends ZodMiniType<any, any, core.$ZodObjectInternals<Shape, Config>>, core.$ZodObject<Shape, Config> {
@@ -216,11 +216,11 @@ export interface ZodMiniUnion<T extends readonly SomeType[] = readonly core.$Zod
216
216
  }
217
217
  export declare const ZodMiniUnion: core.$constructor<ZodMiniUnion>;
218
218
  export declare function union<const T extends readonly SomeType[]>(options: T, params?: string | core.$ZodUnionParams): ZodMiniUnion<T>;
219
- export interface ZodMiniDiscriminatedUnion<Options extends readonly SomeType[] = readonly core.$ZodType[]> extends ZodMiniUnion<Options> {
220
- _zod: core.$ZodDiscriminatedUnionInternals<Options>;
219
+ export interface ZodMiniDiscriminatedUnion<Options extends readonly SomeType[] = readonly core.$ZodType[], Disc extends string = string> extends ZodMiniUnion<Options> {
220
+ _zod: core.$ZodDiscriminatedUnionInternals<Options, Disc>;
221
221
  }
222
222
  export declare const ZodMiniDiscriminatedUnion: core.$constructor<ZodMiniDiscriminatedUnion>;
223
- export declare function discriminatedUnion<Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]]>(discriminator: string, options: Types, params?: string | core.$ZodDiscriminatedUnionParams): ZodMiniDiscriminatedUnion<Types>;
223
+ export declare function discriminatedUnion<Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]], Disc extends string>(discriminator: Disc, options: Types, params?: string | core.$ZodDiscriminatedUnionParams): ZodMiniDiscriminatedUnion<Types, Disc>;
224
224
  export interface ZodMiniIntersection<A extends SomeType = core.$ZodType, B extends SomeType = core.$ZodType> extends _ZodMiniType<core.$ZodIntersectionInternals<A, B>> {
225
225
  }
226
226
  export declare const ZodMiniIntersection: core.$constructor<ZodMiniIntersection>;
@@ -179,7 +179,7 @@ export interface ZodMiniArray<T extends SomeType = core.$ZodType> extends _ZodMi
179
179
  }
180
180
  export declare const ZodMiniArray: core.$constructor<ZodMiniArray>;
181
181
  export declare function array<T extends SomeType>(element: T, params?: string | core.$ZodArrayParams): ZodMiniArray<T>;
182
- export declare function keyof<T extends ZodMiniObject>(schema: T): ZodMiniLiteral<Exclude<keyof T["shape"], symbol>>;
182
+ export declare function keyof<T extends ZodMiniObject>(schema: T): ZodMiniEnum<util.KeysEnum<T["shape"]>>;
183
183
  export interface ZodMiniObject<
184
184
  /** @ts-ignore Cast variance */
185
185
  out Shape extends core.$ZodShape = core.$ZodShape, out Config extends core.$ZodObjectConfig = core.$strip> extends ZodMiniType<any, any, core.$ZodObjectInternals<Shape, Config>>, core.$ZodObject<Shape, Config> {
@@ -216,11 +216,11 @@ export interface ZodMiniUnion<T extends readonly SomeType[] = readonly core.$Zod
216
216
  }
217
217
  export declare const ZodMiniUnion: core.$constructor<ZodMiniUnion>;
218
218
  export declare function union<const T extends readonly SomeType[]>(options: T, params?: string | core.$ZodUnionParams): ZodMiniUnion<T>;
219
- export interface ZodMiniDiscriminatedUnion<Options extends readonly SomeType[] = readonly core.$ZodType[]> extends ZodMiniUnion<Options> {
220
- _zod: core.$ZodDiscriminatedUnionInternals<Options>;
219
+ export interface ZodMiniDiscriminatedUnion<Options extends readonly SomeType[] = readonly core.$ZodType[], Disc extends string = string> extends ZodMiniUnion<Options> {
220
+ _zod: core.$ZodDiscriminatedUnionInternals<Options, Disc>;
221
221
  }
222
222
  export declare const ZodMiniDiscriminatedUnion: core.$constructor<ZodMiniDiscriminatedUnion>;
223
- export declare function discriminatedUnion<Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]]>(discriminator: string, options: Types, params?: string | core.$ZodDiscriminatedUnionParams): ZodMiniDiscriminatedUnion<Types>;
223
+ export declare function discriminatedUnion<Types extends readonly [core.$ZodTypeDiscriminable, ...core.$ZodTypeDiscriminable[]], Disc extends string>(discriminator: Disc, options: Types, params?: string | core.$ZodDiscriminatedUnionParams): ZodMiniDiscriminatedUnion<Types, Disc>;
224
224
  export interface ZodMiniIntersection<A extends SomeType = core.$ZodType, B extends SomeType = core.$ZodType> extends _ZodMiniType<core.$ZodIntersectionInternals<A, B>> {
225
225
  }
226
226
  export declare const ZodMiniIntersection: core.$constructor<ZodMiniIntersection>;
@@ -323,7 +323,7 @@ export function array(element, params) {
323
323
  // .keyof
324
324
  export function keyof(schema) {
325
325
  const shape = schema._zod.def.shape;
326
- return literal(Object.keys(shape));
326
+ return _enum(Object.keys(shape));
327
327
  }
328
328
  export const ZodMiniObject = /*@__PURE__*/ core.$constructor("ZodMiniObject", (inst, def) => {
329
329
  core.$ZodObject.init(inst, def);
@@ -575,7 +575,7 @@ export function _default(innerType, defaultValue) {
575
575
  type: "default",
576
576
  innerType: innerType,
577
577
  get defaultValue() {
578
- return typeof defaultValue === "function" ? defaultValue() : defaultValue;
578
+ return typeof defaultValue === "function" ? defaultValue() : util.shallowClone(defaultValue);
579
579
  },
580
580
  });
581
581
  }
@@ -588,7 +588,7 @@ export function prefault(innerType, defaultValue) {
588
588
  type: "prefault",
589
589
  innerType: innerType,
590
590
  get defaultValue() {
591
- return typeof defaultValue === "function" ? defaultValue() : defaultValue;
591
+ return typeof defaultValue === "function" ? defaultValue() : util.shallowClone(defaultValue);
592
592
  },
593
593
  });
594
594
  }