zod 3.22.4 → 3.23.8

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.umd.js CHANGED
@@ -238,6 +238,11 @@
238
238
  processError(this);
239
239
  return fieldErrors;
240
240
  }
241
+ static assert(value) {
242
+ if (!(value instanceof ZodError)) {
243
+ throw new Error(`Not a ZodError: ${value}`);
244
+ }
245
+ }
241
246
  toString() {
242
247
  return this.message;
243
248
  }
@@ -410,6 +415,13 @@
410
415
  ...issueData,
411
416
  path: fullPath,
412
417
  };
418
+ if (issueData.message !== undefined) {
419
+ return {
420
+ ...issueData,
421
+ path: fullPath,
422
+ message: issueData.message,
423
+ };
424
+ }
413
425
  let errorMessage = "";
414
426
  const maps = errorMaps
415
427
  .filter((m) => !!m)
@@ -421,11 +433,12 @@
421
433
  return {
422
434
  ...issueData,
423
435
  path: fullPath,
424
- message: issueData.message || errorMessage,
436
+ message: errorMessage,
425
437
  };
426
438
  };
427
439
  const EMPTY_PATH = [];
428
440
  function addIssueToContext(ctx, issueData) {
441
+ const overrideMap = getErrorMap();
429
442
  const issue = makeIssue({
430
443
  issueData: issueData,
431
444
  data: ctx.data,
@@ -433,8 +446,8 @@
433
446
  errorMaps: [
434
447
  ctx.common.contextualErrorMap,
435
448
  ctx.schemaErrorMap,
436
- getErrorMap(),
437
- errorMap, // then global default map
449
+ overrideMap,
450
+ overrideMap === errorMap ? undefined : errorMap, // then global default map
438
451
  ].filter((x) => !!x),
439
452
  });
440
453
  ctx.common.issues.push(issue);
@@ -465,9 +478,11 @@
465
478
  static async mergeObjectAsync(status, pairs) {
466
479
  const syncPairs = [];
467
480
  for (const pair of pairs) {
481
+ const key = await pair.key;
482
+ const value = await pair.value;
468
483
  syncPairs.push({
469
- key: await pair.key,
470
- value: await pair.value,
484
+ key,
485
+ value,
471
486
  });
472
487
  }
473
488
  return ParseStatus.mergeObjectSync(status, syncPairs);
@@ -502,12 +517,46 @@
502
517
  const isValid = (x) => x.status === "valid";
503
518
  const isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
504
519
 
520
+ /******************************************************************************
521
+ Copyright (c) Microsoft Corporation.
522
+
523
+ Permission to use, copy, modify, and/or distribute this software for any
524
+ purpose with or without fee is hereby granted.
525
+
526
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
527
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
528
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
529
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
530
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
531
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
532
+ PERFORMANCE OF THIS SOFTWARE.
533
+ ***************************************************************************** */
534
+
535
+ function __classPrivateFieldGet(receiver, state, kind, f) {
536
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
537
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
538
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
539
+ }
540
+
541
+ function __classPrivateFieldSet(receiver, state, value, kind, f) {
542
+ if (kind === "m") throw new TypeError("Private method is not writable");
543
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
544
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
545
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
546
+ }
547
+
548
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
549
+ var e = new Error(message);
550
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
551
+ };
552
+
505
553
  var errorUtil;
506
554
  (function (errorUtil) {
507
555
  errorUtil.errToObj = (message) => typeof message === "string" ? { message } : message || {};
508
556
  errorUtil.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
509
557
  })(errorUtil || (errorUtil = {}));
510
558
 
559
+ var _ZodEnum_cache, _ZodNativeEnum_cache;
511
560
  class ParseInputLazyPath {
512
561
  constructor(parent, value, path, key) {
513
562
  this._cachedPath = [];
@@ -558,12 +607,17 @@
558
607
  if (errorMap)
559
608
  return { errorMap: errorMap, description };
560
609
  const customMap = (iss, ctx) => {
561
- if (iss.code !== "invalid_type")
562
- return { message: ctx.defaultError };
610
+ var _a, _b;
611
+ const { message } = params;
612
+ if (iss.code === "invalid_enum_value") {
613
+ return { message: message !== null && message !== void 0 ? message : ctx.defaultError };
614
+ }
563
615
  if (typeof ctx.data === "undefined") {
564
- return { message: required_error !== null && required_error !== void 0 ? required_error : ctx.defaultError };
616
+ return { message: (_a = message !== null && message !== void 0 ? message : required_error) !== null && _a !== void 0 ? _a : ctx.defaultError };
565
617
  }
566
- return { message: invalid_type_error !== null && invalid_type_error !== void 0 ? invalid_type_error : ctx.defaultError };
618
+ if (iss.code !== "invalid_type")
619
+ return { message: ctx.defaultError };
620
+ return { message: (_b = message !== null && message !== void 0 ? message : invalid_type_error) !== null && _b !== void 0 ? _b : ctx.defaultError };
567
621
  };
568
622
  return { errorMap: customMap, description };
569
623
  }
@@ -821,11 +875,13 @@
821
875
  }
822
876
  }
823
877
  const cuidRegex = /^c[^\s-]{8,}$/i;
824
- const cuid2Regex = /^[a-z][a-z0-9]*$/;
878
+ const cuid2Regex = /^[0-9a-z]+$/;
825
879
  const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/;
826
880
  // const uuidRegex =
827
881
  // /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
828
882
  const uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
883
+ const nanoidRegex = /^[a-z0-9_-]{21}$/i;
884
+ const durationRegex = /^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
829
885
  // from https://stackoverflow.com/a/46181/1550155
830
886
  // old version: too slow, didn't support unicode
831
887
  // const emailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
@@ -838,41 +894,48 @@
838
894
  // /^[a-zA-Z0-9\.\!\#\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
839
895
  // const emailRegex =
840
896
  // /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i;
841
- const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_+-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
897
+ const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
842
898
  // const emailRegex =
843
899
  // /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;
844
900
  // from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
845
901
  const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
846
902
  let emojiRegex;
847
- const ipv4Regex = /^(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))$/;
903
+ // faster, simpler, safer
904
+ const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
848
905
  const ipv6Regex = /^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/;
849
- // Adapted from https://stackoverflow.com/a/3143231
850
- const datetimeRegex = (args) => {
906
+ // https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
907
+ const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
908
+ // simple
909
+ // const dateRegexSource = `\\d{4}-\\d{2}-\\d{2}`;
910
+ // no leap year validation
911
+ // const dateRegexSource = `\\d{4}-((0[13578]|10|12)-31|(0[13-9]|1[0-2])-30|(0[1-9]|1[0-2])-(0[1-9]|1\\d|2\\d))`;
912
+ // with leap year validation
913
+ const dateRegexSource = `((\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01])|(0[469]|11)-(0[1-9]|[12]\\d|30)|(02)-(0[1-9]|1\\d|2[0-8])))`;
914
+ const dateRegex = new RegExp(`^${dateRegexSource}$`);
915
+ function timeRegexSource(args) {
916
+ // let regex = `\\d{2}:\\d{2}:\\d{2}`;
917
+ let regex = `([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d`;
851
918
  if (args.precision) {
852
- if (args.offset) {
853
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${args.precision}}(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
854
- }
855
- else {
856
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${args.precision}}Z$`);
857
- }
919
+ regex = `${regex}\\.\\d{${args.precision}}`;
858
920
  }
859
- else if (args.precision === 0) {
860
- if (args.offset) {
861
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
862
- }
863
- else {
864
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$`);
865
- }
866
- }
867
- else {
868
- if (args.offset) {
869
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
870
- }
871
- else {
872
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?Z$`);
873
- }
921
+ else if (args.precision == null) {
922
+ regex = `${regex}(\\.\\d+)?`;
874
923
  }
875
- };
924
+ return regex;
925
+ }
926
+ function timeRegex(args) {
927
+ return new RegExp(`^${timeRegexSource(args)}$`);
928
+ }
929
+ // Adapted from https://stackoverflow.com/a/3143231
930
+ function datetimeRegex(args) {
931
+ let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
932
+ const opts = [];
933
+ opts.push(args.local ? `Z?` : `Z`);
934
+ if (args.offset)
935
+ opts.push(`([+-]\\d{2}:?\\d{2})`);
936
+ regex = `${regex}(${opts.join("|")})`;
937
+ return new RegExp(`^${regex}$`);
938
+ }
876
939
  function isValidIP(ip, version) {
877
940
  if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
878
941
  return true;
@@ -894,9 +957,7 @@
894
957
  code: ZodIssueCode.invalid_type,
895
958
  expected: ZodParsedType.string,
896
959
  received: ctx.parsedType,
897
- }
898
- //
899
- );
960
+ });
900
961
  return INVALID;
901
962
  }
902
963
  const status = new ParseStatus();
@@ -994,6 +1055,17 @@
994
1055
  status.dirty();
995
1056
  }
996
1057
  }
1058
+ else if (check.kind === "nanoid") {
1059
+ if (!nanoidRegex.test(input.data)) {
1060
+ ctx = this._getOrReturnCtx(input, ctx);
1061
+ addIssueToContext(ctx, {
1062
+ validation: "nanoid",
1063
+ code: ZodIssueCode.invalid_string,
1064
+ message: check.message,
1065
+ });
1066
+ status.dirty();
1067
+ }
1068
+ }
997
1069
  else if (check.kind === "cuid") {
998
1070
  if (!cuidRegex.test(input.data)) {
999
1071
  ctx = this._getOrReturnCtx(input, ctx);
@@ -1108,6 +1180,41 @@
1108
1180
  status.dirty();
1109
1181
  }
1110
1182
  }
1183
+ else if (check.kind === "date") {
1184
+ const regex = dateRegex;
1185
+ if (!regex.test(input.data)) {
1186
+ ctx = this._getOrReturnCtx(input, ctx);
1187
+ addIssueToContext(ctx, {
1188
+ code: ZodIssueCode.invalid_string,
1189
+ validation: "date",
1190
+ message: check.message,
1191
+ });
1192
+ status.dirty();
1193
+ }
1194
+ }
1195
+ else if (check.kind === "time") {
1196
+ const regex = timeRegex(check);
1197
+ if (!regex.test(input.data)) {
1198
+ ctx = this._getOrReturnCtx(input, ctx);
1199
+ addIssueToContext(ctx, {
1200
+ code: ZodIssueCode.invalid_string,
1201
+ validation: "time",
1202
+ message: check.message,
1203
+ });
1204
+ status.dirty();
1205
+ }
1206
+ }
1207
+ else if (check.kind === "duration") {
1208
+ if (!durationRegex.test(input.data)) {
1209
+ ctx = this._getOrReturnCtx(input, ctx);
1210
+ addIssueToContext(ctx, {
1211
+ validation: "duration",
1212
+ code: ZodIssueCode.invalid_string,
1213
+ message: check.message,
1214
+ });
1215
+ status.dirty();
1216
+ }
1217
+ }
1111
1218
  else if (check.kind === "ip") {
1112
1219
  if (!isValidIP(input.data, check.version)) {
1113
1220
  ctx = this._getOrReturnCtx(input, ctx);
@@ -1119,6 +1226,17 @@
1119
1226
  status.dirty();
1120
1227
  }
1121
1228
  }
1229
+ else if (check.kind === "base64") {
1230
+ if (!base64Regex.test(input.data)) {
1231
+ ctx = this._getOrReturnCtx(input, ctx);
1232
+ addIssueToContext(ctx, {
1233
+ validation: "base64",
1234
+ code: ZodIssueCode.invalid_string,
1235
+ message: check.message,
1236
+ });
1237
+ status.dirty();
1238
+ }
1239
+ }
1122
1240
  else {
1123
1241
  exports.util.assertNever(check);
1124
1242
  }
@@ -1150,6 +1268,9 @@
1150
1268
  uuid(message) {
1151
1269
  return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
1152
1270
  }
1271
+ nanoid(message) {
1272
+ return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
1273
+ }
1153
1274
  cuid(message) {
1154
1275
  return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
1155
1276
  }
@@ -1159,16 +1280,20 @@
1159
1280
  ulid(message) {
1160
1281
  return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
1161
1282
  }
1283
+ base64(message) {
1284
+ return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
1285
+ }
1162
1286
  ip(options) {
1163
1287
  return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
1164
1288
  }
1165
1289
  datetime(options) {
1166
- var _a;
1290
+ var _a, _b;
1167
1291
  if (typeof options === "string") {
1168
1292
  return this._addCheck({
1169
1293
  kind: "datetime",
1170
1294
  precision: null,
1171
1295
  offset: false,
1296
+ local: false,
1172
1297
  message: options,
1173
1298
  });
1174
1299
  }
@@ -1176,9 +1301,30 @@
1176
1301
  kind: "datetime",
1177
1302
  precision: typeof (options === null || options === void 0 ? void 0 : options.precision) === "undefined" ? null : options === null || options === void 0 ? void 0 : options.precision,
1178
1303
  offset: (_a = options === null || options === void 0 ? void 0 : options.offset) !== null && _a !== void 0 ? _a : false,
1304
+ local: (_b = options === null || options === void 0 ? void 0 : options.local) !== null && _b !== void 0 ? _b : false,
1179
1305
  ...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
1180
1306
  });
1181
1307
  }
1308
+ date(message) {
1309
+ return this._addCheck({ kind: "date", message });
1310
+ }
1311
+ time(options) {
1312
+ if (typeof options === "string") {
1313
+ return this._addCheck({
1314
+ kind: "time",
1315
+ precision: null,
1316
+ message: options,
1317
+ });
1318
+ }
1319
+ return this._addCheck({
1320
+ kind: "time",
1321
+ precision: typeof (options === null || options === void 0 ? void 0 : options.precision) === "undefined" ? null : options === null || options === void 0 ? void 0 : options.precision,
1322
+ ...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
1323
+ });
1324
+ }
1325
+ duration(message) {
1326
+ return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
1327
+ }
1182
1328
  regex(regex, message) {
1183
1329
  return this._addCheck({
1184
1330
  kind: "regex",
@@ -1257,6 +1403,15 @@
1257
1403
  get isDatetime() {
1258
1404
  return !!this._def.checks.find((ch) => ch.kind === "datetime");
1259
1405
  }
1406
+ get isDate() {
1407
+ return !!this._def.checks.find((ch) => ch.kind === "date");
1408
+ }
1409
+ get isTime() {
1410
+ return !!this._def.checks.find((ch) => ch.kind === "time");
1411
+ }
1412
+ get isDuration() {
1413
+ return !!this._def.checks.find((ch) => ch.kind === "duration");
1414
+ }
1260
1415
  get isEmail() {
1261
1416
  return !!this._def.checks.find((ch) => ch.kind === "email");
1262
1417
  }
@@ -1269,6 +1424,9 @@
1269
1424
  get isUUID() {
1270
1425
  return !!this._def.checks.find((ch) => ch.kind === "uuid");
1271
1426
  }
1427
+ get isNANOID() {
1428
+ return !!this._def.checks.find((ch) => ch.kind === "nanoid");
1429
+ }
1272
1430
  get isCUID() {
1273
1431
  return !!this._def.checks.find((ch) => ch.kind === "cuid");
1274
1432
  }
@@ -1281,6 +1439,9 @@
1281
1439
  get isIP() {
1282
1440
  return !!this._def.checks.find((ch) => ch.kind === "ip");
1283
1441
  }
1442
+ get isBase64() {
1443
+ return !!this._def.checks.find((ch) => ch.kind === "base64");
1444
+ }
1284
1445
  get minLength() {
1285
1446
  let min = null;
1286
1447
  for (const ch of this._def.checks) {
@@ -2268,9 +2429,10 @@
2268
2429
  const syncPairs = [];
2269
2430
  for (const pair of pairs) {
2270
2431
  const key = await pair.key;
2432
+ const value = await pair.value;
2271
2433
  syncPairs.push({
2272
2434
  key,
2273
- value: await pair.value,
2435
+ value,
2274
2436
  alwaysSet: pair.alwaysSet,
2275
2437
  });
2276
2438
  }
@@ -2644,7 +2806,7 @@
2644
2806
  }
2645
2807
  else if (type instanceof ZodNativeEnum) {
2646
2808
  // eslint-disable-next-line ban/ban
2647
- return Object.keys(type.enum);
2809
+ return exports.util.objectValues(type.enum);
2648
2810
  }
2649
2811
  else if (type instanceof ZodDefault) {
2650
2812
  return getDiscriminator(type._def.innerType);
@@ -2655,8 +2817,23 @@
2655
2817
  else if (type instanceof ZodNull) {
2656
2818
  return [null];
2657
2819
  }
2820
+ else if (type instanceof ZodOptional) {
2821
+ return [undefined, ...getDiscriminator(type.unwrap())];
2822
+ }
2823
+ else if (type instanceof ZodNullable) {
2824
+ return [null, ...getDiscriminator(type.unwrap())];
2825
+ }
2826
+ else if (type instanceof ZodBranded) {
2827
+ return getDiscriminator(type.unwrap());
2828
+ }
2829
+ else if (type instanceof ZodReadonly) {
2830
+ return getDiscriminator(type.unwrap());
2831
+ }
2832
+ else if (type instanceof ZodCatch) {
2833
+ return getDiscriminator(type._def.innerType);
2834
+ }
2658
2835
  else {
2659
- return null;
2836
+ return [];
2660
2837
  }
2661
2838
  };
2662
2839
  class ZodDiscriminatedUnion extends ZodType {
@@ -2719,7 +2896,7 @@
2719
2896
  // try {
2720
2897
  for (const type of options) {
2721
2898
  const discriminatorValues = getDiscriminator(type.shape[discriminator]);
2722
- if (!discriminatorValues) {
2899
+ if (!discriminatorValues.length) {
2723
2900
  throw new Error(`A discriminator value for key \`${discriminator}\` could not be extracted from all schema options`);
2724
2901
  }
2725
2902
  for (const value of discriminatorValues) {
@@ -2932,6 +3109,7 @@
2932
3109
  pairs.push({
2933
3110
  key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
2934
3111
  value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
3112
+ alwaysSet: key in ctx.data,
2935
3113
  });
2936
3114
  }
2937
3115
  if (ctx.common.async) {
@@ -3291,6 +3469,10 @@
3291
3469
  });
3292
3470
  }
3293
3471
  class ZodEnum extends ZodType {
3472
+ constructor() {
3473
+ super(...arguments);
3474
+ _ZodEnum_cache.set(this, void 0);
3475
+ }
3294
3476
  _parse(input) {
3295
3477
  if (typeof input.data !== "string") {
3296
3478
  const ctx = this._getOrReturnCtx(input);
@@ -3302,7 +3484,10 @@
3302
3484
  });
3303
3485
  return INVALID;
3304
3486
  }
3305
- if (this._def.values.indexOf(input.data) === -1) {
3487
+ if (!__classPrivateFieldGet(this, _ZodEnum_cache, "f")) {
3488
+ __classPrivateFieldSet(this, _ZodEnum_cache, new Set(this._def.values), "f");
3489
+ }
3490
+ if (!__classPrivateFieldGet(this, _ZodEnum_cache, "f").has(input.data)) {
3306
3491
  const ctx = this._getOrReturnCtx(input);
3307
3492
  const expectedValues = this._def.values;
3308
3493
  addIssueToContext(ctx, {
@@ -3338,15 +3523,26 @@
3338
3523
  }
3339
3524
  return enumValues;
3340
3525
  }
3341
- extract(values) {
3342
- return ZodEnum.create(values);
3526
+ extract(values, newDef = this._def) {
3527
+ return ZodEnum.create(values, {
3528
+ ...this._def,
3529
+ ...newDef,
3530
+ });
3343
3531
  }
3344
- exclude(values) {
3345
- return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)));
3532
+ exclude(values, newDef = this._def) {
3533
+ return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
3534
+ ...this._def,
3535
+ ...newDef,
3536
+ });
3346
3537
  }
3347
3538
  }
3539
+ _ZodEnum_cache = new WeakMap();
3348
3540
  ZodEnum.create = createZodEnum;
3349
3541
  class ZodNativeEnum extends ZodType {
3542
+ constructor() {
3543
+ super(...arguments);
3544
+ _ZodNativeEnum_cache.set(this, void 0);
3545
+ }
3350
3546
  _parse(input) {
3351
3547
  const nativeEnumValues = exports.util.getValidEnumValues(this._def.values);
3352
3548
  const ctx = this._getOrReturnCtx(input);
@@ -3360,7 +3556,10 @@
3360
3556
  });
3361
3557
  return INVALID;
3362
3558
  }
3363
- if (nativeEnumValues.indexOf(input.data) === -1) {
3559
+ if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache, "f")) {
3560
+ __classPrivateFieldSet(this, _ZodNativeEnum_cache, new Set(exports.util.getValidEnumValues(this._def.values)), "f");
3561
+ }
3562
+ if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache, "f").has(input.data)) {
3364
3563
  const expectedValues = exports.util.objectValues(nativeEnumValues);
3365
3564
  addIssueToContext(ctx, {
3366
3565
  received: ctx.data,
@@ -3375,6 +3574,7 @@
3375
3574
  return this._def.values;
3376
3575
  }
3377
3576
  }
3577
+ _ZodNativeEnum_cache = new WeakMap();
3378
3578
  ZodNativeEnum.create = (values, params) => {
3379
3579
  return new ZodNativeEnum({
3380
3580
  values: values,
@@ -3444,33 +3644,43 @@
3444
3644
  checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
3445
3645
  if (effect.type === "preprocess") {
3446
3646
  const processed = effect.transform(ctx.data, checkCtx);
3447
- if (ctx.common.issues.length) {
3448
- return {
3449
- status: "dirty",
3450
- value: ctx.data,
3451
- };
3452
- }
3453
3647
  if (ctx.common.async) {
3454
- return Promise.resolve(processed).then((processed) => {
3455
- return this._def.schema._parseAsync({
3648
+ return Promise.resolve(processed).then(async (processed) => {
3649
+ if (status.value === "aborted")
3650
+ return INVALID;
3651
+ const result = await this._def.schema._parseAsync({
3456
3652
  data: processed,
3457
3653
  path: ctx.path,
3458
3654
  parent: ctx,
3459
3655
  });
3656
+ if (result.status === "aborted")
3657
+ return INVALID;
3658
+ if (result.status === "dirty")
3659
+ return DIRTY(result.value);
3660
+ if (status.value === "dirty")
3661
+ return DIRTY(result.value);
3662
+ return result;
3460
3663
  });
3461
3664
  }
3462
3665
  else {
3463
- return this._def.schema._parseSync({
3666
+ if (status.value === "aborted")
3667
+ return INVALID;
3668
+ const result = this._def.schema._parseSync({
3464
3669
  data: processed,
3465
3670
  path: ctx.path,
3466
3671
  parent: ctx,
3467
3672
  });
3673
+ if (result.status === "aborted")
3674
+ return INVALID;
3675
+ if (result.status === "dirty")
3676
+ return DIRTY(result.value);
3677
+ if (status.value === "dirty")
3678
+ return DIRTY(result.value);
3679
+ return result;
3468
3680
  }
3469
3681
  }
3470
3682
  if (effect.type === "refinement") {
3471
- const executeRefinement = (acc
3472
- // effect: RefinementEffect<any>
3473
- ) => {
3683
+ const executeRefinement = (acc) => {
3474
3684
  const result = effect.refinement(acc, checkCtx);
3475
3685
  if (ctx.common.async) {
3476
3686
  return Promise.resolve(result);
@@ -3773,10 +3983,18 @@
3773
3983
  class ZodReadonly extends ZodType {
3774
3984
  _parse(input) {
3775
3985
  const result = this._def.innerType._parse(input);
3776
- if (isValid(result)) {
3777
- result.value = Object.freeze(result.value);
3778
- }
3779
- return result;
3986
+ const freeze = (data) => {
3987
+ if (isValid(data)) {
3988
+ data.value = Object.freeze(data.value);
3989
+ }
3990
+ return data;
3991
+ };
3992
+ return isAsync(result)
3993
+ ? result.then((data) => freeze(data))
3994
+ : freeze(result);
3995
+ }
3996
+ unwrap() {
3997
+ return this._def.innerType;
3780
3998
  }
3781
3999
  }
3782
4000
  ZodReadonly.create = (type, params) => {
@@ -3786,7 +4004,7 @@
3786
4004
  ...processCreateParams(params),
3787
4005
  });
3788
4006
  };
3789
- const custom = (check, params = {},
4007
+ function custom(check, params = {},
3790
4008
  /**
3791
4009
  * @deprecated
3792
4010
  *
@@ -3797,7 +4015,7 @@
3797
4015
  * ```
3798
4016
  *
3799
4017
  */
3800
- fatal) => {
4018
+ fatal) {
3801
4019
  if (check)
3802
4020
  return ZodAny.create().superRefine((data, ctx) => {
3803
4021
  var _a, _b;
@@ -3813,7 +4031,7 @@
3813
4031
  }
3814
4032
  });
3815
4033
  return ZodAny.create();
3816
- };
4034
+ }
3817
4035
  const late = {
3818
4036
  object: ZodObject.lazycreate,
3819
4037
  };
@@ -3931,6 +4149,7 @@
3931
4149
  ZodParsedType: ZodParsedType,
3932
4150
  getParsedType: getParsedType,
3933
4151
  ZodType: ZodType,
4152
+ datetimeRegex: datetimeRegex,
3934
4153
  ZodString: ZodString,
3935
4154
  ZodNumber: ZodNumber,
3936
4155
  ZodBigInt: ZodBigInt,
@@ -4078,6 +4297,7 @@
4078
4297
  exports.coerce = coerce;
4079
4298
  exports.custom = custom;
4080
4299
  exports.date = dateType;
4300
+ exports.datetimeRegex = datetimeRegex;
4081
4301
  exports["default"] = z;
4082
4302
  exports.defaultErrorMap = errorMap;
4083
4303
  exports.discriminatedUnion = discriminatedUnionType;