zod 3.25.74 → 3.25.76

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.
@@ -47,7 +47,7 @@ interface EmitParams {
47
47
  | {
48
48
  /** */
49
49
  registry: $ZodRegistry<{ id?: string | undefined }>;
50
- uri: (id: string) => string;
50
+ uri?: ((id: string) => string) | undefined;
51
51
  defs: Record<string, JSONSchema.BaseSchema>;
52
52
  }
53
53
  | undefined;
@@ -227,9 +227,10 @@ export class JSONSchemaGenerator {
227
227
  case "unknown": {
228
228
  break;
229
229
  }
230
- case "undefined":
231
- case "never": {
232
- _json.not = {};
230
+ case "undefined": {
231
+ if (this.unrepresentable === "throw") {
232
+ throw new Error("Undefined cannot be represented in JSON Schema");
233
+ }
233
234
  break;
234
235
  }
235
236
  case "void": {
@@ -238,6 +239,10 @@ export class JSONSchemaGenerator {
238
239
  }
239
240
  break;
240
241
  }
242
+ case "never": {
243
+ _json.not = {};
244
+ break;
245
+ }
241
246
  case "date": {
242
247
  if (this.unrepresentable === "throw") {
243
248
  throw new Error("Date cannot be represented in JSON Schema");
@@ -609,6 +614,8 @@ export class JSONSchemaGenerator {
609
614
  // initialize result with root schema fields
610
615
  // Object.assign(result, seen.cached);
611
616
 
617
+ // returns a ref to the schema
618
+ // defId will be empty if the ref points to an external schema (or #)
612
619
  const makeURI = (entry: [schemas.$ZodType<unknown, unknown>, Seen]): { ref: string; defId?: string } => {
613
620
  // comparing the seen objects because sometimes
614
621
  // multiple schemas map to the same seen object.
@@ -620,12 +627,15 @@ export class JSONSchemaGenerator {
620
627
  const externalId = params.external.registry.get(entry[0])?.id; // ?? "__shared";// `__schema${this.counter++}`;
621
628
 
622
629
  // check if schema is in the external registry
623
- if (externalId) return { ref: params.external.uri(externalId) };
630
+ const uriGenerator = params.external.uri ?? ((id) => id);
631
+ if (externalId) {
632
+ return { ref: uriGenerator(externalId) };
633
+ }
624
634
 
625
635
  // otherwise, add to __shared
626
636
  const id: string = entry[1].defId ?? (entry[1].schema.id as string) ?? `schema${this.counter++}`;
627
- entry[1].defId = id;
628
- return { defId: id, ref: `${params.external.uri("__shared")}#/${defsSegment}/${id}` };
637
+ entry[1].defId = id; // set defId so it will be reused if needed
638
+ return { defId: id, ref: `${uriGenerator("__shared")}#/${defsSegment}/${id}` };
629
639
  }
630
640
 
631
641
  if (entry[1] === root) {
@@ -642,6 +652,7 @@ export class JSONSchemaGenerator {
642
652
  // stored cached version in `def` property
643
653
  // remove all properties, set $ref
644
654
  const extractToDef = (entry: [schemas.$ZodType<unknown, unknown>, Seen]): void => {
655
+ // if the schema is already a reference, do not extract it
645
656
  if (entry[1].schema.$ref) {
646
657
  return;
647
658
  }
@@ -659,15 +670,29 @@ export class JSONSchemaGenerator {
659
670
  schema.$ref = ref;
660
671
  };
661
672
 
673
+ // throw on cycles
674
+
675
+ // break cycles
676
+ if (params.cycles === "throw") {
677
+ for (const entry of this.seen.entries()) {
678
+ const seen = entry[1];
679
+ if (seen.cycle) {
680
+ throw new Error(
681
+ "Cycle detected: " +
682
+ `#/${seen.cycle?.join("/")}/<root>` +
683
+ '\n\nSet the `cycles` parameter to `"ref"` to resolve cyclical schemas with defs.'
684
+ );
685
+ }
686
+ }
687
+ }
688
+
662
689
  // extract schemas into $defs
663
690
  for (const entry of this.seen.entries()) {
664
691
  const seen = entry[1];
665
692
 
666
693
  // convert root schema to # $ref
667
- // also prevents root schema from being extracted
668
694
  if (schema === entry[0]) {
669
- // do not copy to defs...this is the root schema
670
- extractToDef(entry);
695
+ extractToDef(entry); // this has special handling for the root schema
671
696
  continue;
672
697
  }
673
698
 
@@ -684,21 +709,13 @@ export class JSONSchemaGenerator {
684
709
  const id = this.metadataRegistry.get(entry[0])?.id;
685
710
  if (id) {
686
711
  extractToDef(entry);
687
-
688
712
  continue;
689
713
  }
690
714
 
691
715
  // break cycles
692
716
  if (seen.cycle) {
693
- if (params.cycles === "throw") {
694
- throw new Error(
695
- "Cycle detected: " +
696
- `#/${seen.cycle?.join("/")}/<root>` +
697
- '\n\nSet the `cycles` parameter to `"ref"` to resolve cyclical schemas with defs.'
698
- );
699
- } else if (params.cycles === "ref") {
700
- extractToDef(entry);
701
- }
717
+ // any
718
+ extractToDef(entry);
702
719
  continue;
703
720
  }
704
721
 
@@ -763,6 +780,12 @@ export class JSONSchemaGenerator {
763
780
  console.warn(`Invalid target: ${this.target}`);
764
781
  }
765
782
 
783
+ if (params.external?.uri) {
784
+ const id = params.external.registry.get(schema)?.id;
785
+ if (!id) throw new Error("Schema is missing an `id` property");
786
+ result.$id = params.external.uri(id);
787
+ }
788
+
766
789
  Object.assign(result, root.def);
767
790
 
768
791
  // build defs object
@@ -775,11 +798,14 @@ export class JSONSchemaGenerator {
775
798
  }
776
799
 
777
800
  // set definitions in result
778
- if (!params.external && Object.keys(defs).length > 0) {
779
- if (this.target === "draft-2020-12") {
780
- result.$defs = defs;
781
- } else {
782
- result.definitions = defs;
801
+ if (params.external) {
802
+ } else {
803
+ if (Object.keys(defs).length > 0) {
804
+ if (this.target === "draft-2020-12") {
805
+ result.$defs = defs;
806
+ } else {
807
+ result.definitions = defs;
808
+ }
783
809
  }
784
810
  }
785
811
 
@@ -819,7 +845,7 @@ export function toJSONSchema(
819
845
  const schemas: Record<string, JSONSchema.BaseSchema> = {};
820
846
  const external = {
821
847
  registry: input,
822
- uri: (_params as RegistryToJSONSchemaParams)?.uri || ((id) => id),
848
+ uri: (_params as RegistryToJSONSchemaParams)?.uri,
823
849
  defs,
824
850
  };
825
851
  for (const entry of input._idmap.entries()) {
@@ -1038,10 +1038,10 @@ exports.ZodCustom = core.$constructor("ZodCustom", (inst, def) => {
1038
1038
  exports.ZodType.init(inst, def);
1039
1039
  });
1040
1040
  // custom checks
1041
- function check(fn, params) {
1041
+ function check(fn) {
1042
1042
  const ch = new core.$ZodCheck({
1043
1043
  check: "custom",
1044
- ...index_js_1.util.normalizeParams(params),
1044
+ // ...util.normalizeParams(params),
1045
1045
  });
1046
1046
  ch._zod.check = fn;
1047
1047
  return ch;
@@ -1053,7 +1053,7 @@ function refine(fn, _params = {}) {
1053
1053
  return core._refine(exports.ZodCustom, fn, _params);
1054
1054
  }
1055
1055
  // superRefine
1056
- function superRefine(fn, params) {
1056
+ function superRefine(fn) {
1057
1057
  const ch = check((payload) => {
1058
1058
  payload.addIssue = (issue) => {
1059
1059
  if (typeof issue === "string") {
@@ -1072,7 +1072,7 @@ function superRefine(fn, params) {
1072
1072
  }
1073
1073
  };
1074
1074
  return fn(payload.value, payload);
1075
- }, params);
1075
+ });
1076
1076
  return ch;
1077
1077
  }
1078
1078
  function _instanceof(cls, params = {
@@ -602,10 +602,10 @@ export declare function promise<T extends core.SomeType>(innerType: T): ZodPromi
602
602
  export interface ZodCustom<O = unknown, I = unknown> extends _ZodType<core.$ZodCustomInternals<O, I>>, core.$ZodCustom<O, I> {
603
603
  }
604
604
  export declare const ZodCustom: core.$constructor<ZodCustom>;
605
- export declare function check<O = unknown>(fn: core.CheckFn<O>, params?: string | core.$ZodCustomParams): core.$ZodCheck<O>;
605
+ export declare function check<O = unknown>(fn: core.CheckFn<O>): core.$ZodCheck<O>;
606
606
  export declare function custom<O>(fn?: (data: unknown) => unknown, _params?: string | core.$ZodCustomParams | undefined): ZodCustom<O, O>;
607
607
  export declare function refine<T>(fn: (arg: NoInfer<T>) => util.MaybeAsync<unknown>, _params?: string | core.$ZodCustomParams): core.$ZodCheck<T>;
608
- export declare function superRefine<T>(fn: (arg: T, payload: RefinementCtx<T>) => void | Promise<void>, params?: string | core.$ZodCustomParams): core.$ZodCheck<T>;
608
+ export declare function superRefine<T>(fn: (arg: T, payload: RefinementCtx<T>) => void | Promise<void>): core.$ZodCheck<T>;
609
609
  type ZodInstanceOfParams = core.Params<ZodCustom, core.$ZodIssueCustom, "type" | "check" | "checks" | "fn" | "abort" | "error" | "params" | "path">;
610
610
  declare function _instanceof<T extends typeof util.Class>(cls: T, params?: ZodInstanceOfParams): ZodCustom<InstanceType<T>, InstanceType<T>>;
611
611
  export { _instanceof as instanceof };
@@ -602,10 +602,10 @@ export declare function promise<T extends core.SomeType>(innerType: T): ZodPromi
602
602
  export interface ZodCustom<O = unknown, I = unknown> extends _ZodType<core.$ZodCustomInternals<O, I>>, core.$ZodCustom<O, I> {
603
603
  }
604
604
  export declare const ZodCustom: core.$constructor<ZodCustom>;
605
- export declare function check<O = unknown>(fn: core.CheckFn<O>, params?: string | core.$ZodCustomParams): core.$ZodCheck<O>;
605
+ export declare function check<O = unknown>(fn: core.CheckFn<O>): core.$ZodCheck<O>;
606
606
  export declare function custom<O>(fn?: (data: unknown) => unknown, _params?: string | core.$ZodCustomParams | undefined): ZodCustom<O, O>;
607
607
  export declare function refine<T>(fn: (arg: NoInfer<T>) => util.MaybeAsync<unknown>, _params?: string | core.$ZodCustomParams): core.$ZodCheck<T>;
608
- export declare function superRefine<T>(fn: (arg: T, payload: RefinementCtx<T>) => void | Promise<void>, params?: string | core.$ZodCustomParams): core.$ZodCheck<T>;
608
+ export declare function superRefine<T>(fn: (arg: T, payload: RefinementCtx<T>) => void | Promise<void>): core.$ZodCheck<T>;
609
609
  type ZodInstanceOfParams = core.Params<ZodCustom, core.$ZodIssueCustom, "type" | "check" | "checks" | "fn" | "abort" | "error" | "params" | "path">;
610
610
  declare function _instanceof<T extends typeof util.Class>(cls: T, params?: ZodInstanceOfParams): ZodCustom<InstanceType<T>, InstanceType<T>>;
611
611
  export { _instanceof as instanceof };
@@ -935,10 +935,10 @@ export const ZodCustom = /*@__PURE__*/ core.$constructor("ZodCustom", (inst, def
935
935
  ZodType.init(inst, def);
936
936
  });
937
937
  // custom checks
938
- export function check(fn, params) {
938
+ export function check(fn) {
939
939
  const ch = new core.$ZodCheck({
940
940
  check: "custom",
941
- ...util.normalizeParams(params),
941
+ // ...util.normalizeParams(params),
942
942
  });
943
943
  ch._zod.check = fn;
944
944
  return ch;
@@ -950,7 +950,7 @@ export function refine(fn, _params = {}) {
950
950
  return core._refine(ZodCustom, fn, _params);
951
951
  }
952
952
  // superRefine
953
- export function superRefine(fn, params) {
953
+ export function superRefine(fn) {
954
954
  const ch = check((payload) => {
955
955
  payload.addIssue = (issue) => {
956
956
  if (typeof issue === "string") {
@@ -969,7 +969,7 @@ export function superRefine(fn, params) {
969
969
  }
970
970
  };
971
971
  return fn(payload.value, payload);
972
- }, params);
972
+ });
973
973
  return ch;
974
974
  }
975
975
  function _instanceof(cls, params = {
@@ -248,11 +248,12 @@ exports.$ZodCheckBigIntFormat = core.$constructor("$ZodCheckBigIntFormat", (inst
248
248
  };
249
249
  });
250
250
  exports.$ZodCheckMaxSize = core.$constructor("$ZodCheckMaxSize", (inst, def) => {
251
+ var _a;
251
252
  exports.$ZodCheck.init(inst, def);
252
- inst._zod.when = (payload) => {
253
+ (_a = inst._zod.def).when ?? (_a.when = (payload) => {
253
254
  const val = payload.value;
254
255
  return !util.nullish(val) && val.size !== undefined;
255
- };
256
+ });
256
257
  inst._zod.onattach.push((inst) => {
257
258
  const curr = (inst._zod.bag.maximum ?? Number.POSITIVE_INFINITY);
258
259
  if (def.maximum < curr)
@@ -274,11 +275,12 @@ exports.$ZodCheckMaxSize = core.$constructor("$ZodCheckMaxSize", (inst, def) =>
274
275
  };
275
276
  });
276
277
  exports.$ZodCheckMinSize = core.$constructor("$ZodCheckMinSize", (inst, def) => {
278
+ var _a;
277
279
  exports.$ZodCheck.init(inst, def);
278
- inst._zod.when = (payload) => {
280
+ (_a = inst._zod.def).when ?? (_a.when = (payload) => {
279
281
  const val = payload.value;
280
282
  return !util.nullish(val) && val.size !== undefined;
281
- };
283
+ });
282
284
  inst._zod.onattach.push((inst) => {
283
285
  const curr = (inst._zod.bag.minimum ?? Number.NEGATIVE_INFINITY);
284
286
  if (def.minimum > curr)
@@ -300,11 +302,12 @@ exports.$ZodCheckMinSize = core.$constructor("$ZodCheckMinSize", (inst, def) =>
300
302
  };
301
303
  });
302
304
  exports.$ZodCheckSizeEquals = core.$constructor("$ZodCheckSizeEquals", (inst, def) => {
305
+ var _a;
303
306
  exports.$ZodCheck.init(inst, def);
304
- inst._zod.when = (payload) => {
307
+ (_a = inst._zod.def).when ?? (_a.when = (payload) => {
305
308
  const val = payload.value;
306
309
  return !util.nullish(val) && val.size !== undefined;
307
- };
310
+ });
308
311
  inst._zod.onattach.push((inst) => {
309
312
  const bag = inst._zod.bag;
310
313
  bag.minimum = def.size;
@@ -329,11 +332,12 @@ exports.$ZodCheckSizeEquals = core.$constructor("$ZodCheckSizeEquals", (inst, de
329
332
  };
330
333
  });
331
334
  exports.$ZodCheckMaxLength = core.$constructor("$ZodCheckMaxLength", (inst, def) => {
335
+ var _a;
332
336
  exports.$ZodCheck.init(inst, def);
333
- inst._zod.when = (payload) => {
337
+ (_a = inst._zod.def).when ?? (_a.when = (payload) => {
334
338
  const val = payload.value;
335
339
  return !util.nullish(val) && val.length !== undefined;
336
- };
340
+ });
337
341
  inst._zod.onattach.push((inst) => {
338
342
  const curr = (inst._zod.bag.maximum ?? Number.POSITIVE_INFINITY);
339
343
  if (def.maximum < curr)
@@ -357,11 +361,12 @@ exports.$ZodCheckMaxLength = core.$constructor("$ZodCheckMaxLength", (inst, def)
357
361
  };
358
362
  });
359
363
  exports.$ZodCheckMinLength = core.$constructor("$ZodCheckMinLength", (inst, def) => {
364
+ var _a;
360
365
  exports.$ZodCheck.init(inst, def);
361
- inst._zod.when = (payload) => {
366
+ (_a = inst._zod.def).when ?? (_a.when = (payload) => {
362
367
  const val = payload.value;
363
368
  return !util.nullish(val) && val.length !== undefined;
364
- };
369
+ });
365
370
  inst._zod.onattach.push((inst) => {
366
371
  const curr = (inst._zod.bag.minimum ?? Number.NEGATIVE_INFINITY);
367
372
  if (def.minimum > curr)
@@ -385,11 +390,12 @@ exports.$ZodCheckMinLength = core.$constructor("$ZodCheckMinLength", (inst, def)
385
390
  };
386
391
  });
387
392
  exports.$ZodCheckLengthEquals = core.$constructor("$ZodCheckLengthEquals", (inst, def) => {
393
+ var _a;
388
394
  exports.$ZodCheck.init(inst, def);
389
- inst._zod.when = (payload) => {
395
+ (_a = inst._zod.def).when ?? (_a.when = (payload) => {
390
396
  const val = payload.value;
391
397
  return !util.nullish(val) && val.length !== undefined;
392
- };
398
+ });
393
399
  inst._zod.onattach.push((inst) => {
394
400
  const bag = inst._zod.bag;
395
401
  bag.minimum = def.length;
@@ -7,6 +7,8 @@ export interface $ZodCheckDef {
7
7
  error?: errors.$ZodErrorMap<never> | undefined;
8
8
  /** If true, no later checks will be executed if this check fails. Default `false`. */
9
9
  abort?: boolean | undefined;
10
+ /** If provided, this check will only be executed if the function returns `true`. Defaults to `payload => z.util.isAborted(payload)`. */
11
+ when?: ((payload: schemas.ParsePayload) => boolean) | undefined;
10
12
  }
11
13
  export interface $ZodCheckInternals<T> {
12
14
  def: $ZodCheckDef;
@@ -14,7 +16,6 @@ export interface $ZodCheckInternals<T> {
14
16
  issc?: errors.$ZodIssueBase;
15
17
  check(payload: schemas.ParsePayload<T>): util.MaybeAsync<void>;
16
18
  onattach: ((schema: schemas.$ZodType) => void)[];
17
- when?: ((payload: schemas.ParsePayload) => boolean) | undefined;
18
19
  }
19
20
  export interface $ZodCheck<in T = never> {
20
21
  _zod: $ZodCheckInternals<T>;
@@ -7,6 +7,8 @@ export interface $ZodCheckDef {
7
7
  error?: errors.$ZodErrorMap<never> | undefined;
8
8
  /** If true, no later checks will be executed if this check fails. Default `false`. */
9
9
  abort?: boolean | undefined;
10
+ /** If provided, this check will only be executed if the function returns `true`. Defaults to `payload => z.util.isAborted(payload)`. */
11
+ when?: ((payload: schemas.ParsePayload) => boolean) | undefined;
10
12
  }
11
13
  export interface $ZodCheckInternals<T> {
12
14
  def: $ZodCheckDef;
@@ -14,7 +16,6 @@ export interface $ZodCheckInternals<T> {
14
16
  issc?: errors.$ZodIssueBase;
15
17
  check(payload: schemas.ParsePayload<T>): util.MaybeAsync<void>;
16
18
  onattach: ((schema: schemas.$ZodType) => void)[];
17
- when?: ((payload: schemas.ParsePayload) => boolean) | undefined;
18
19
  }
19
20
  export interface $ZodCheck<in T = never> {
20
21
  _zod: $ZodCheckInternals<T>;
package/v4/core/checks.js CHANGED
@@ -222,11 +222,12 @@ export const $ZodCheckBigIntFormat = /*@__PURE__*/ core.$constructor("$ZodCheckB
222
222
  };
223
223
  });
224
224
  export const $ZodCheckMaxSize = /*@__PURE__*/ core.$constructor("$ZodCheckMaxSize", (inst, def) => {
225
+ var _a;
225
226
  $ZodCheck.init(inst, def);
226
- inst._zod.when = (payload) => {
227
+ (_a = inst._zod.def).when ?? (_a.when = (payload) => {
227
228
  const val = payload.value;
228
229
  return !util.nullish(val) && val.size !== undefined;
229
- };
230
+ });
230
231
  inst._zod.onattach.push((inst) => {
231
232
  const curr = (inst._zod.bag.maximum ?? Number.POSITIVE_INFINITY);
232
233
  if (def.maximum < curr)
@@ -248,11 +249,12 @@ export const $ZodCheckMaxSize = /*@__PURE__*/ core.$constructor("$ZodCheckMaxSiz
248
249
  };
249
250
  });
250
251
  export const $ZodCheckMinSize = /*@__PURE__*/ core.$constructor("$ZodCheckMinSize", (inst, def) => {
252
+ var _a;
251
253
  $ZodCheck.init(inst, def);
252
- inst._zod.when = (payload) => {
254
+ (_a = inst._zod.def).when ?? (_a.when = (payload) => {
253
255
  const val = payload.value;
254
256
  return !util.nullish(val) && val.size !== undefined;
255
- };
257
+ });
256
258
  inst._zod.onattach.push((inst) => {
257
259
  const curr = (inst._zod.bag.minimum ?? Number.NEGATIVE_INFINITY);
258
260
  if (def.minimum > curr)
@@ -274,11 +276,12 @@ export const $ZodCheckMinSize = /*@__PURE__*/ core.$constructor("$ZodCheckMinSiz
274
276
  };
275
277
  });
276
278
  export const $ZodCheckSizeEquals = /*@__PURE__*/ core.$constructor("$ZodCheckSizeEquals", (inst, def) => {
279
+ var _a;
277
280
  $ZodCheck.init(inst, def);
278
- inst._zod.when = (payload) => {
281
+ (_a = inst._zod.def).when ?? (_a.when = (payload) => {
279
282
  const val = payload.value;
280
283
  return !util.nullish(val) && val.size !== undefined;
281
- };
284
+ });
282
285
  inst._zod.onattach.push((inst) => {
283
286
  const bag = inst._zod.bag;
284
287
  bag.minimum = def.size;
@@ -303,11 +306,12 @@ export const $ZodCheckSizeEquals = /*@__PURE__*/ core.$constructor("$ZodCheckSiz
303
306
  };
304
307
  });
305
308
  export const $ZodCheckMaxLength = /*@__PURE__*/ core.$constructor("$ZodCheckMaxLength", (inst, def) => {
309
+ var _a;
306
310
  $ZodCheck.init(inst, def);
307
- inst._zod.when = (payload) => {
311
+ (_a = inst._zod.def).when ?? (_a.when = (payload) => {
308
312
  const val = payload.value;
309
313
  return !util.nullish(val) && val.length !== undefined;
310
- };
314
+ });
311
315
  inst._zod.onattach.push((inst) => {
312
316
  const curr = (inst._zod.bag.maximum ?? Number.POSITIVE_INFINITY);
313
317
  if (def.maximum < curr)
@@ -331,11 +335,12 @@ export const $ZodCheckMaxLength = /*@__PURE__*/ core.$constructor("$ZodCheckMaxL
331
335
  };
332
336
  });
333
337
  export const $ZodCheckMinLength = /*@__PURE__*/ core.$constructor("$ZodCheckMinLength", (inst, def) => {
338
+ var _a;
334
339
  $ZodCheck.init(inst, def);
335
- inst._zod.when = (payload) => {
340
+ (_a = inst._zod.def).when ?? (_a.when = (payload) => {
336
341
  const val = payload.value;
337
342
  return !util.nullish(val) && val.length !== undefined;
338
- };
343
+ });
339
344
  inst._zod.onattach.push((inst) => {
340
345
  const curr = (inst._zod.bag.minimum ?? Number.NEGATIVE_INFINITY);
341
346
  if (def.minimum > curr)
@@ -359,11 +364,12 @@ export const $ZodCheckMinLength = /*@__PURE__*/ core.$constructor("$ZodCheckMinL
359
364
  };
360
365
  });
361
366
  export const $ZodCheckLengthEquals = /*@__PURE__*/ core.$constructor("$ZodCheckLengthEquals", (inst, def) => {
367
+ var _a;
362
368
  $ZodCheck.init(inst, def);
363
- inst._zod.when = (payload) => {
369
+ (_a = inst._zod.def).when ?? (_a.when = (payload) => {
364
370
  const val = payload.value;
365
371
  return !util.nullish(val) && val.length !== undefined;
366
- };
372
+ });
367
373
  inst._zod.onattach.push((inst) => {
368
374
  const bag = inst._zod.bag;
369
375
  bag.minimum = def.length;
@@ -48,6 +48,10 @@ const initializer = (inst, def) => {
48
48
  enumerable: true,
49
49
  // configurable: false,
50
50
  });
51
+ Object.defineProperty(inst, "toString", {
52
+ value: () => inst.message,
53
+ enumerable: false,
54
+ });
51
55
  };
52
56
  exports.$ZodError = (0, core_js_1.$constructor)("$ZodError", initializer);
53
57
  exports.$ZodRealError = (0, core_js_1.$constructor)("$ZodError", initializer, { Parent: Error });
package/v4/core/errors.js CHANGED
@@ -17,6 +17,10 @@ const initializer = (inst, def) => {
17
17
  enumerable: true,
18
18
  // configurable: false,
19
19
  });
20
+ Object.defineProperty(inst, "toString", {
21
+ value: () => inst.message,
22
+ enumerable: false,
23
+ });
20
24
  };
21
25
  export const $ZodError = $constructor("$ZodError", initializer);
22
26
  export const $ZodRealError = $constructor("$ZodError", initializer, { Parent: Error });
@@ -6,7 +6,7 @@ exports.$output = Symbol("ZodOutput");
6
6
  exports.$input = Symbol("ZodInput");
7
7
  class $ZodRegistry {
8
8
  constructor() {
9
- this._map = new WeakMap();
9
+ this._map = new Map();
10
10
  this._idmap = new Map();
11
11
  }
12
12
  add(schema, ..._meta) {
@@ -20,7 +20,16 @@ class $ZodRegistry {
20
20
  }
21
21
  return this;
22
22
  }
23
+ clear() {
24
+ this._map = new Map();
25
+ this._idmap = new Map();
26
+ return this;
27
+ }
23
28
  remove(schema) {
29
+ const meta = this._map.get(schema);
30
+ if (meta && typeof meta === "object" && "id" in meta) {
31
+ this._idmap.delete(meta.id);
32
+ }
24
33
  this._map.delete(schema);
25
34
  return this;
26
35
  }
@@ -13,9 +13,10 @@ type MetadataType = Record<string, unknown> | undefined;
13
13
  export declare class $ZodRegistry<Meta extends MetadataType = MetadataType, Schema extends $ZodType = $ZodType> {
14
14
  _meta: Meta;
15
15
  _schema: Schema;
16
- _map: WeakMap<Schema, $replace<Meta, Schema>>;
16
+ _map: Map<Schema, $replace<Meta, Schema>>;
17
17
  _idmap: Map<string, Schema>;
18
18
  add<S extends Schema>(schema: S, ..._meta: undefined extends Meta ? [$replace<Meta, S>?] : [$replace<Meta, S>]): this;
19
+ clear(): this;
19
20
  remove(schema: Schema): this;
20
21
  get<S extends Schema>(schema: S): $replace<Meta, S> | undefined;
21
22
  has(schema: Schema): boolean;
@@ -13,9 +13,10 @@ type MetadataType = Record<string, unknown> | undefined;
13
13
  export declare class $ZodRegistry<Meta extends MetadataType = MetadataType, Schema extends $ZodType = $ZodType> {
14
14
  _meta: Meta;
15
15
  _schema: Schema;
16
- _map: WeakMap<Schema, $replace<Meta, Schema>>;
16
+ _map: Map<Schema, $replace<Meta, Schema>>;
17
17
  _idmap: Map<string, Schema>;
18
18
  add<S extends Schema>(schema: S, ..._meta: undefined extends Meta ? [$replace<Meta, S>?] : [$replace<Meta, S>]): this;
19
+ clear(): this;
19
20
  remove(schema: Schema): this;
20
21
  get<S extends Schema>(schema: S): $replace<Meta, S> | undefined;
21
22
  has(schema: Schema): boolean;
@@ -2,7 +2,7 @@ export const $output = Symbol("ZodOutput");
2
2
  export const $input = Symbol("ZodInput");
3
3
  export class $ZodRegistry {
4
4
  constructor() {
5
- this._map = new WeakMap();
5
+ this._map = new Map();
6
6
  this._idmap = new Map();
7
7
  }
8
8
  add(schema, ..._meta) {
@@ -16,7 +16,16 @@ export class $ZodRegistry {
16
16
  }
17
17
  return this;
18
18
  }
19
+ clear() {
20
+ this._map = new Map();
21
+ this._idmap = new Map();
22
+ return this;
23
+ }
19
24
  remove(schema) {
25
+ const meta = this._map.get(schema);
26
+ if (meta && typeof meta === "object" && "id" in meta) {
27
+ this._idmap.delete(meta.id);
28
+ }
20
29
  this._map.delete(schema);
21
30
  return this;
22
31
  }
@@ -65,8 +65,8 @@ exports.$ZodType = core.$constructor("$ZodType", (inst, def) => {
65
65
  let isAborted = util.aborted(payload);
66
66
  let asyncResult;
67
67
  for (const ch of checks) {
68
- if (ch._zod.when) {
69
- const shouldRun = ch._zod.when(payload);
68
+ if (ch._zod.def.when) {
69
+ const shouldRun = ch._zod.def.when(payload);
70
70
  if (!shouldRun)
71
71
  continue;
72
72
  }
@@ -635,6 +635,8 @@ export interface $ZodIntersectionDef<Left extends SomeType = $ZodType, Right ext
635
635
  export interface $ZodIntersectionInternals<A extends SomeType = $ZodType, B extends SomeType = $ZodType> extends $ZodTypeInternals<core.output<A> & core.output<B>, core.input<A> & core.input<B>> {
636
636
  def: $ZodIntersectionDef<A, B>;
637
637
  isst: never;
638
+ optin: A["_zod"]["optin"] | B["_zod"]["optin"];
639
+ optout: A["_zod"]["optout"] | B["_zod"]["optout"];
638
640
  }
639
641
  export interface $ZodIntersection<A extends SomeType = $ZodType, B extends SomeType = $ZodType> extends $ZodType {
640
642
  _zod: $ZodIntersectionInternals<A, B>;
@@ -686,6 +688,8 @@ export type $InferZodRecordInput<Key extends $ZodRecordKey = $ZodRecordKey, Valu
686
688
  export interface $ZodRecordInternals<Key extends $ZodRecordKey = $ZodRecordKey, Value extends SomeType = $ZodType> extends $ZodTypeInternals<$InferZodRecordOutput<Key, Value>, $InferZodRecordInput<Key, Value>> {
687
689
  def: $ZodRecordDef<Key, Value>;
688
690
  isst: errors.$ZodIssueInvalidType | errors.$ZodIssueInvalidKey<Record<PropertyKey, unknown>>;
691
+ optin?: "optional" | undefined;
692
+ optout?: "optional" | undefined;
689
693
  }
690
694
  export type $partial = {
691
695
  "~~partial": true;
@@ -702,6 +706,8 @@ export interface $ZodMapDef<Key extends SomeType = $ZodType, Value extends SomeT
702
706
  export interface $ZodMapInternals<Key extends SomeType = $ZodType, Value extends SomeType = $ZodType> extends $ZodTypeInternals<Map<core.output<Key>, core.output<Value>>, Map<core.input<Key>, core.input<Value>>> {
703
707
  def: $ZodMapDef<Key, Value>;
704
708
  isst: errors.$ZodIssueInvalidType | errors.$ZodIssueInvalidKey | errors.$ZodIssueInvalidElement<unknown>;
709
+ optin?: "optional" | undefined;
710
+ optout?: "optional" | undefined;
705
711
  }
706
712
  export interface $ZodMap<Key extends SomeType = $ZodType, Value extends SomeType = $ZodType> extends $ZodType {
707
713
  _zod: $ZodMapInternals<Key, Value>;
@@ -714,6 +720,8 @@ export interface $ZodSetDef<T extends SomeType = $ZodType> extends $ZodTypeDef {
714
720
  export interface $ZodSetInternals<T extends SomeType = $ZodType> extends $ZodTypeInternals<Set<core.output<T>>, Set<core.input<T>>> {
715
721
  def: $ZodSetDef<T>;
716
722
  isst: errors.$ZodIssueInvalidType;
723
+ optin?: "optional" | undefined;
724
+ optout?: "optional" | undefined;
717
725
  }
718
726
  export interface $ZodSet<T extends SomeType = $ZodType> extends $ZodType {
719
727
  _zod: $ZodSetInternals<T>;
@@ -826,6 +834,7 @@ export interface $ZodDefaultDef<T extends SomeType = $ZodType> extends $ZodTypeD
826
834
  export interface $ZodDefaultInternals<T extends SomeType = $ZodType> extends $ZodTypeInternals<util.NoUndefined<core.output<T>>, core.input<T> | undefined> {
827
835
  def: $ZodDefaultDef<T>;
828
836
  optin: "optional";
837
+ optout?: "optional" | undefined;
829
838
  isst: never;
830
839
  values: T["_zod"]["values"];
831
840
  }
@@ -842,6 +851,7 @@ export interface $ZodPrefaultDef<T extends SomeType = $ZodType> extends $ZodType
842
851
  export interface $ZodPrefaultInternals<T extends SomeType = $ZodType> extends $ZodTypeInternals<util.NoUndefined<core.output<T>>, core.input<T> | undefined> {
843
852
  def: $ZodPrefaultDef<T>;
844
853
  optin: "optional";
854
+ optout?: "optional" | undefined;
845
855
  isst: never;
846
856
  values: T["_zod"]["values"];
847
857
  }
@@ -857,6 +867,8 @@ export interface $ZodNonOptionalInternals<T extends SomeType = $ZodType> extends
857
867
  def: $ZodNonOptionalDef<T>;
858
868
  isst: errors.$ZodIssueInvalidType;
859
869
  values: T["_zod"]["values"];
870
+ optin: "optional" | undefined;
871
+ optout: "optional" | undefined;
860
872
  }
861
873
  export interface $ZodNonOptional<T extends SomeType = $ZodType> extends $ZodType {
862
874
  _zod: $ZodNonOptionalInternals<T>;
@@ -869,6 +881,8 @@ export interface $ZodSuccessDef<T extends SomeType = $ZodType> extends $ZodTypeD
869
881
  export interface $ZodSuccessInternals<T extends SomeType = $ZodType> extends $ZodTypeInternals<boolean, core.input<T>> {
870
882
  def: $ZodSuccessDef<T>;
871
883
  isst: never;
884
+ optin: T["_zod"]["optin"];
885
+ optout: "optional" | undefined;
872
886
  }
873
887
  export interface $ZodSuccess<T extends SomeType = $ZodType> extends $ZodType {
874
888
  _zod: $ZodSuccessInternals<T>;