zod 3.24.0 → 3.24.2

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/lib/index.mjs CHANGED
@@ -160,6 +160,9 @@ const quotelessJson = (obj) => {
160
160
  return json.replace(/"([^"]+)":/g, "$1:");
161
161
  };
162
162
  class ZodError extends Error {
163
+ get errors() {
164
+ return this.issues;
165
+ }
163
166
  constructor(issues) {
164
167
  super();
165
168
  this.issues = [];
@@ -180,9 +183,6 @@ class ZodError extends Error {
180
183
  this.name = "ZodError";
181
184
  this.issues = issues;
182
185
  }
183
- get errors() {
184
- return this.issues;
185
- }
186
186
  format(_mapper) {
187
187
  const mapper = _mapper ||
188
188
  function (issue) {
@@ -438,9 +438,9 @@ function addIssueToContext(ctx, issueData) {
438
438
  data: ctx.data,
439
439
  path: ctx.path,
440
440
  errorMaps: [
441
- ctx.common.contextualErrorMap,
442
- ctx.schemaErrorMap,
443
- overrideMap,
441
+ ctx.common.contextualErrorMap, // contextual error map is first priority
442
+ ctx.schemaErrorMap, // then schema-bound map if available
443
+ overrideMap, // then global override map
444
444
  overrideMap === errorMap ? undefined : errorMap, // then global default map
445
445
  ].filter((x) => !!x),
446
446
  });
@@ -616,40 +616,6 @@ function processCreateParams(params) {
616
616
  return { errorMap: customMap, description };
617
617
  }
618
618
  class ZodType {
619
- constructor(def) {
620
- /** Alias of safeParseAsync */
621
- this.spa = this.safeParseAsync;
622
- this._def = def;
623
- this.parse = this.parse.bind(this);
624
- this.safeParse = this.safeParse.bind(this);
625
- this.parseAsync = this.parseAsync.bind(this);
626
- this.safeParseAsync = this.safeParseAsync.bind(this);
627
- this.spa = this.spa.bind(this);
628
- this.refine = this.refine.bind(this);
629
- this.refinement = this.refinement.bind(this);
630
- this.superRefine = this.superRefine.bind(this);
631
- this.optional = this.optional.bind(this);
632
- this.nullable = this.nullable.bind(this);
633
- this.nullish = this.nullish.bind(this);
634
- this.array = this.array.bind(this);
635
- this.promise = this.promise.bind(this);
636
- this.or = this.or.bind(this);
637
- this.and = this.and.bind(this);
638
- this.transform = this.transform.bind(this);
639
- this.brand = this.brand.bind(this);
640
- this.default = this.default.bind(this);
641
- this.catch = this.catch.bind(this);
642
- this.describe = this.describe.bind(this);
643
- this.pipe = this.pipe.bind(this);
644
- this.readonly = this.readonly.bind(this);
645
- this.isNullable = this.isNullable.bind(this);
646
- this.isOptional = this.isOptional.bind(this);
647
- this["~standard"] = {
648
- version: 1,
649
- vendor: "zod",
650
- validate: (data) => this["~validate"](data),
651
- };
652
- }
653
619
  get description() {
654
620
  return this._def.description;
655
621
  }
@@ -714,7 +680,7 @@ class ZodType {
714
680
  return handleResult(ctx, result);
715
681
  }
716
682
  "~validate"(data) {
717
- var _a, _b, _c;
683
+ var _a, _b;
718
684
  const ctx = {
719
685
  common: {
720
686
  issues: [],
@@ -738,7 +704,7 @@ class ZodType {
738
704
  };
739
705
  }
740
706
  catch (err) {
741
- if ((_c = (_b = (_a = err) === null || _a === void 0 ? void 0 : _a.message) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === null || _c === void 0 ? void 0 : _c.includes("encountered")) {
707
+ if ((_b = (_a = err === null || err === void 0 ? void 0 : err.message) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === null || _b === void 0 ? void 0 : _b.includes("encountered")) {
742
708
  this["~standard"].async = true;
743
709
  }
744
710
  ctx.common = {
@@ -841,6 +807,40 @@ class ZodType {
841
807
  superRefine(refinement) {
842
808
  return this._refinement(refinement);
843
809
  }
810
+ constructor(def) {
811
+ /** Alias of safeParseAsync */
812
+ this.spa = this.safeParseAsync;
813
+ this._def = def;
814
+ this.parse = this.parse.bind(this);
815
+ this.safeParse = this.safeParse.bind(this);
816
+ this.parseAsync = this.parseAsync.bind(this);
817
+ this.safeParseAsync = this.safeParseAsync.bind(this);
818
+ this.spa = this.spa.bind(this);
819
+ this.refine = this.refine.bind(this);
820
+ this.refinement = this.refinement.bind(this);
821
+ this.superRefine = this.superRefine.bind(this);
822
+ this.optional = this.optional.bind(this);
823
+ this.nullable = this.nullable.bind(this);
824
+ this.nullish = this.nullish.bind(this);
825
+ this.array = this.array.bind(this);
826
+ this.promise = this.promise.bind(this);
827
+ this.or = this.or.bind(this);
828
+ this.and = this.and.bind(this);
829
+ this.transform = this.transform.bind(this);
830
+ this.brand = this.brand.bind(this);
831
+ this.default = this.default.bind(this);
832
+ this.catch = this.catch.bind(this);
833
+ this.describe = this.describe.bind(this);
834
+ this.pipe = this.pipe.bind(this);
835
+ this.readonly = this.readonly.bind(this);
836
+ this.isNullable = this.isNullable.bind(this);
837
+ this.isOptional = this.isOptional.bind(this);
838
+ this["~standard"] = {
839
+ version: 1,
840
+ vendor: "zod",
841
+ validate: (data) => this["~validate"](data),
842
+ };
843
+ }
844
844
  optional() {
845
845
  return ZodOptional.create(this, this._def);
846
846
  }
@@ -1398,7 +1398,10 @@ class ZodString extends ZodType {
1398
1398
  }
1399
1399
  base64url(message) {
1400
1400
  // base64url encoding is a modification of base64 that can safely be used in URLs and filenames
1401
- return this._addCheck({ kind: "base64url", ...errorUtil.errToObj(message) });
1401
+ return this._addCheck({
1402
+ kind: "base64url",
1403
+ ...errorUtil.errToObj(message),
1404
+ });
1402
1405
  }
1403
1406
  jwt(options) {
1404
1407
  return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(options) });
@@ -1499,8 +1502,7 @@ class ZodString extends ZodType {
1499
1502
  });
1500
1503
  }
1501
1504
  /**
1502
- * @deprecated Use z.string().min(1) instead.
1503
- * @see {@link ZodString.min}
1505
+ * Equivalent to `.min(1)`
1504
1506
  */
1505
1507
  nonempty(message) {
1506
1508
  return this.min(1, errorUtil.errToObj(message));
@@ -4142,7 +4144,23 @@ ZodReadonly.create = (type, params) => {
4142
4144
  ...processCreateParams(params),
4143
4145
  });
4144
4146
  };
4145
- function custom(check, params = {},
4147
+ ////////////////////////////////////////
4148
+ ////////////////////////////////////////
4149
+ ////////// //////////
4150
+ ////////// z.custom //////////
4151
+ ////////// //////////
4152
+ ////////////////////////////////////////
4153
+ ////////////////////////////////////////
4154
+ function cleanParams(params, data) {
4155
+ const p = typeof params === "function"
4156
+ ? params(data)
4157
+ : typeof params === "string"
4158
+ ? { message: params }
4159
+ : params;
4160
+ const p2 = typeof p === "string" ? { message: p } : p;
4161
+ return p2;
4162
+ }
4163
+ function custom(check, _params = {},
4146
4164
  /**
4147
4165
  * @deprecated
4148
4166
  *
@@ -4157,16 +4175,23 @@ fatal) {
4157
4175
  if (check)
4158
4176
  return ZodAny.create().superRefine((data, ctx) => {
4159
4177
  var _a, _b;
4160
- if (!check(data)) {
4161
- const p = typeof params === "function"
4162
- ? params(data)
4163
- : typeof params === "string"
4164
- ? { message: params }
4165
- : params;
4166
- const _fatal = (_b = (_a = p.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
4167
- const p2 = typeof p === "string" ? { message: p } : p;
4168
- ctx.addIssue({ code: "custom", ...p2, fatal: _fatal });
4178
+ const r = check(data);
4179
+ if (r instanceof Promise) {
4180
+ return r.then((r) => {
4181
+ var _a, _b;
4182
+ if (!r) {
4183
+ const params = cleanParams(_params, data);
4184
+ const _fatal = (_b = (_a = params.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
4185
+ ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
4186
+ }
4187
+ });
4188
+ }
4189
+ if (!r) {
4190
+ const params = cleanParams(_params, data);
4191
+ const _fatal = (_b = (_a = params.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
4192
+ ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
4169
4193
  }
4194
+ return;
4170
4195
  });
4171
4196
  return ZodAny.create();
4172
4197
  }
package/lib/index.umd.js CHANGED
@@ -166,6 +166,9 @@
166
166
  return json.replace(/"([^"]+)":/g, "$1:");
167
167
  };
168
168
  class ZodError extends Error {
169
+ get errors() {
170
+ return this.issues;
171
+ }
169
172
  constructor(issues) {
170
173
  super();
171
174
  this.issues = [];
@@ -186,9 +189,6 @@
186
189
  this.name = "ZodError";
187
190
  this.issues = issues;
188
191
  }
189
- get errors() {
190
- return this.issues;
191
- }
192
192
  format(_mapper) {
193
193
  const mapper = _mapper ||
194
194
  function (issue) {
@@ -444,9 +444,9 @@
444
444
  data: ctx.data,
445
445
  path: ctx.path,
446
446
  errorMaps: [
447
- ctx.common.contextualErrorMap,
448
- ctx.schemaErrorMap,
449
- overrideMap,
447
+ ctx.common.contextualErrorMap, // contextual error map is first priority
448
+ ctx.schemaErrorMap, // then schema-bound map if available
449
+ overrideMap, // then global override map
450
450
  overrideMap === errorMap ? undefined : errorMap, // then global default map
451
451
  ].filter((x) => !!x),
452
452
  });
@@ -622,40 +622,6 @@
622
622
  return { errorMap: customMap, description };
623
623
  }
624
624
  class ZodType {
625
- constructor(def) {
626
- /** Alias of safeParseAsync */
627
- this.spa = this.safeParseAsync;
628
- this._def = def;
629
- this.parse = this.parse.bind(this);
630
- this.safeParse = this.safeParse.bind(this);
631
- this.parseAsync = this.parseAsync.bind(this);
632
- this.safeParseAsync = this.safeParseAsync.bind(this);
633
- this.spa = this.spa.bind(this);
634
- this.refine = this.refine.bind(this);
635
- this.refinement = this.refinement.bind(this);
636
- this.superRefine = this.superRefine.bind(this);
637
- this.optional = this.optional.bind(this);
638
- this.nullable = this.nullable.bind(this);
639
- this.nullish = this.nullish.bind(this);
640
- this.array = this.array.bind(this);
641
- this.promise = this.promise.bind(this);
642
- this.or = this.or.bind(this);
643
- this.and = this.and.bind(this);
644
- this.transform = this.transform.bind(this);
645
- this.brand = this.brand.bind(this);
646
- this.default = this.default.bind(this);
647
- this.catch = this.catch.bind(this);
648
- this.describe = this.describe.bind(this);
649
- this.pipe = this.pipe.bind(this);
650
- this.readonly = this.readonly.bind(this);
651
- this.isNullable = this.isNullable.bind(this);
652
- this.isOptional = this.isOptional.bind(this);
653
- this["~standard"] = {
654
- version: 1,
655
- vendor: "zod",
656
- validate: (data) => this["~validate"](data),
657
- };
658
- }
659
625
  get description() {
660
626
  return this._def.description;
661
627
  }
@@ -720,7 +686,7 @@
720
686
  return handleResult(ctx, result);
721
687
  }
722
688
  "~validate"(data) {
723
- var _a, _b, _c;
689
+ var _a, _b;
724
690
  const ctx = {
725
691
  common: {
726
692
  issues: [],
@@ -744,7 +710,7 @@
744
710
  };
745
711
  }
746
712
  catch (err) {
747
- if ((_c = (_b = (_a = err) === null || _a === void 0 ? void 0 : _a.message) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === null || _c === void 0 ? void 0 : _c.includes("encountered")) {
713
+ if ((_b = (_a = err === null || err === void 0 ? void 0 : err.message) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === null || _b === void 0 ? void 0 : _b.includes("encountered")) {
748
714
  this["~standard"].async = true;
749
715
  }
750
716
  ctx.common = {
@@ -847,6 +813,40 @@
847
813
  superRefine(refinement) {
848
814
  return this._refinement(refinement);
849
815
  }
816
+ constructor(def) {
817
+ /** Alias of safeParseAsync */
818
+ this.spa = this.safeParseAsync;
819
+ this._def = def;
820
+ this.parse = this.parse.bind(this);
821
+ this.safeParse = this.safeParse.bind(this);
822
+ this.parseAsync = this.parseAsync.bind(this);
823
+ this.safeParseAsync = this.safeParseAsync.bind(this);
824
+ this.spa = this.spa.bind(this);
825
+ this.refine = this.refine.bind(this);
826
+ this.refinement = this.refinement.bind(this);
827
+ this.superRefine = this.superRefine.bind(this);
828
+ this.optional = this.optional.bind(this);
829
+ this.nullable = this.nullable.bind(this);
830
+ this.nullish = this.nullish.bind(this);
831
+ this.array = this.array.bind(this);
832
+ this.promise = this.promise.bind(this);
833
+ this.or = this.or.bind(this);
834
+ this.and = this.and.bind(this);
835
+ this.transform = this.transform.bind(this);
836
+ this.brand = this.brand.bind(this);
837
+ this.default = this.default.bind(this);
838
+ this.catch = this.catch.bind(this);
839
+ this.describe = this.describe.bind(this);
840
+ this.pipe = this.pipe.bind(this);
841
+ this.readonly = this.readonly.bind(this);
842
+ this.isNullable = this.isNullable.bind(this);
843
+ this.isOptional = this.isOptional.bind(this);
844
+ this["~standard"] = {
845
+ version: 1,
846
+ vendor: "zod",
847
+ validate: (data) => this["~validate"](data),
848
+ };
849
+ }
850
850
  optional() {
851
851
  return ZodOptional.create(this, this._def);
852
852
  }
@@ -1404,7 +1404,10 @@
1404
1404
  }
1405
1405
  base64url(message) {
1406
1406
  // base64url encoding is a modification of base64 that can safely be used in URLs and filenames
1407
- return this._addCheck({ kind: "base64url", ...errorUtil.errToObj(message) });
1407
+ return this._addCheck({
1408
+ kind: "base64url",
1409
+ ...errorUtil.errToObj(message),
1410
+ });
1408
1411
  }
1409
1412
  jwt(options) {
1410
1413
  return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(options) });
@@ -1505,8 +1508,7 @@
1505
1508
  });
1506
1509
  }
1507
1510
  /**
1508
- * @deprecated Use z.string().min(1) instead.
1509
- * @see {@link ZodString.min}
1511
+ * Equivalent to `.min(1)`
1510
1512
  */
1511
1513
  nonempty(message) {
1512
1514
  return this.min(1, errorUtil.errToObj(message));
@@ -4148,7 +4150,23 @@
4148
4150
  ...processCreateParams(params),
4149
4151
  });
4150
4152
  };
4151
- function custom(check, params = {},
4153
+ ////////////////////////////////////////
4154
+ ////////////////////////////////////////
4155
+ ////////// //////////
4156
+ ////////// z.custom //////////
4157
+ ////////// //////////
4158
+ ////////////////////////////////////////
4159
+ ////////////////////////////////////////
4160
+ function cleanParams(params, data) {
4161
+ const p = typeof params === "function"
4162
+ ? params(data)
4163
+ : typeof params === "string"
4164
+ ? { message: params }
4165
+ : params;
4166
+ const p2 = typeof p === "string" ? { message: p } : p;
4167
+ return p2;
4168
+ }
4169
+ function custom(check, _params = {},
4152
4170
  /**
4153
4171
  * @deprecated
4154
4172
  *
@@ -4163,16 +4181,23 @@
4163
4181
  if (check)
4164
4182
  return ZodAny.create().superRefine((data, ctx) => {
4165
4183
  var _a, _b;
4166
- if (!check(data)) {
4167
- const p = typeof params === "function"
4168
- ? params(data)
4169
- : typeof params === "string"
4170
- ? { message: params }
4171
- : params;
4172
- const _fatal = (_b = (_a = p.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
4173
- const p2 = typeof p === "string" ? { message: p } : p;
4174
- ctx.addIssue({ code: "custom", ...p2, fatal: _fatal });
4184
+ const r = check(data);
4185
+ if (r instanceof Promise) {
4186
+ return r.then((r) => {
4187
+ var _a, _b;
4188
+ if (!r) {
4189
+ const params = cleanParams(_params, data);
4190
+ const _fatal = (_b = (_a = params.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
4191
+ ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
4192
+ }
4193
+ });
4194
+ }
4195
+ if (!r) {
4196
+ const params = cleanParams(_params, data);
4197
+ const _fatal = (_b = (_a = params.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
4198
+ ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
4175
4199
  }
4200
+ return;
4176
4201
  });
4177
4202
  return ZodAny.create();
4178
4203
  }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * The Standard Schema interface.
3
3
  */
4
- export declare type StandardSchemaV1<Input = unknown, Output = Input> = {
4
+ export type StandardSchemaV1<Input = unknown, Output = Input> = {
5
5
  /**
6
6
  * The Standard Schema properties.
7
7
  */