@embeddable.com/sdk-utils 0.3.3 → 0.4.0

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.js CHANGED
@@ -3,6 +3,26 @@
3
3
  var promises = require('node:fs/promises');
4
4
  var node_path = require('node:path');
5
5
  var node_child_process = require('node:child_process');
6
+ var crypto = require('node:crypto');
7
+
8
+ function _interopNamespaceDefault(e) {
9
+ var n = Object.create(null);
10
+ if (e) {
11
+ Object.keys(e).forEach(function (k) {
12
+ if (k !== 'default') {
13
+ var d = Object.getOwnPropertyDescriptor(e, k);
14
+ Object.defineProperty(n, k, d.get ? d : {
15
+ enumerable: true,
16
+ get: function () { return e[k]; }
17
+ });
18
+ }
19
+ });
20
+ }
21
+ n.default = e;
22
+ return Object.freeze(n);
23
+ }
24
+
25
+ var crypto__namespace = /*#__PURE__*/_interopNamespaceDefault(crypto);
6
26
 
7
27
  var findFiles = async (initialSrcDir, regex) => {
8
28
  const filesList = [];
@@ -285,6 +305,11 @@ class ZodError extends Error {
285
305
  processError(this);
286
306
  return fieldErrors;
287
307
  }
308
+ static assert(value) {
309
+ if (!(value instanceof ZodError)) {
310
+ throw new Error(`Not a ZodError: ${value}`);
311
+ }
312
+ }
288
313
  toString() {
289
314
  return this.message;
290
315
  }
@@ -454,6 +479,13 @@ const makeIssue = (params) => {
454
479
  ...issueData,
455
480
  path: fullPath,
456
481
  };
482
+ if (issueData.message !== undefined) {
483
+ return {
484
+ ...issueData,
485
+ path: fullPath,
486
+ message: issueData.message,
487
+ };
488
+ }
457
489
  let errorMessage = "";
458
490
  const maps = errorMaps
459
491
  .filter((m) => !!m)
@@ -465,10 +497,11 @@ const makeIssue = (params) => {
465
497
  return {
466
498
  ...issueData,
467
499
  path: fullPath,
468
- message: issueData.message || errorMessage,
500
+ message: errorMessage,
469
501
  };
470
502
  };
471
503
  function addIssueToContext(ctx, issueData) {
504
+ const overrideMap = getErrorMap();
472
505
  const issue = makeIssue({
473
506
  issueData: issueData,
474
507
  data: ctx.data,
@@ -476,8 +509,8 @@ function addIssueToContext(ctx, issueData) {
476
509
  errorMaps: [
477
510
  ctx.common.contextualErrorMap,
478
511
  ctx.schemaErrorMap,
479
- getErrorMap(),
480
- errorMap, // then global default map
512
+ overrideMap,
513
+ overrideMap === errorMap ? undefined : errorMap, // then global default map
481
514
  ].filter((x) => !!x),
482
515
  });
483
516
  ctx.common.issues.push(issue);
@@ -508,9 +541,11 @@ class ParseStatus {
508
541
  static async mergeObjectAsync(status, pairs) {
509
542
  const syncPairs = [];
510
543
  for (const pair of pairs) {
544
+ const key = await pair.key;
545
+ const value = await pair.value;
511
546
  syncPairs.push({
512
- key: await pair.key,
513
- value: await pair.value,
547
+ key,
548
+ value,
514
549
  });
515
550
  }
516
551
  return ParseStatus.mergeObjectSync(status, syncPairs);
@@ -545,12 +580,46 @@ const isDirty = (x) => x.status === "dirty";
545
580
  const isValid = (x) => x.status === "valid";
546
581
  const isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
547
582
 
583
+ /******************************************************************************
584
+ Copyright (c) Microsoft Corporation.
585
+
586
+ Permission to use, copy, modify, and/or distribute this software for any
587
+ purpose with or without fee is hereby granted.
588
+
589
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
590
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
591
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
592
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
593
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
594
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
595
+ PERFORMANCE OF THIS SOFTWARE.
596
+ ***************************************************************************** */
597
+
598
+ function __classPrivateFieldGet(receiver, state, kind, f) {
599
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
600
+ 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");
601
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
602
+ }
603
+
604
+ function __classPrivateFieldSet(receiver, state, value, kind, f) {
605
+ if (kind === "m") throw new TypeError("Private method is not writable");
606
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
607
+ 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");
608
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
609
+ }
610
+
611
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
612
+ var e = new Error(message);
613
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
614
+ };
615
+
548
616
  var errorUtil;
549
617
  (function (errorUtil) {
550
618
  errorUtil.errToObj = (message) => typeof message === "string" ? { message } : message || {};
551
619
  errorUtil.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
552
620
  })(errorUtil || (errorUtil = {}));
553
621
 
622
+ var _ZodEnum_cache, _ZodNativeEnum_cache;
554
623
  class ParseInputLazyPath {
555
624
  constructor(parent, value, path, key) {
556
625
  this._cachedPath = [];
@@ -601,12 +670,17 @@ function processCreateParams(params) {
601
670
  if (errorMap)
602
671
  return { errorMap: errorMap, description };
603
672
  const customMap = (iss, ctx) => {
604
- if (iss.code !== "invalid_type")
605
- return { message: ctx.defaultError };
673
+ var _a, _b;
674
+ const { message } = params;
675
+ if (iss.code === "invalid_enum_value") {
676
+ return { message: message !== null && message !== void 0 ? message : ctx.defaultError };
677
+ }
606
678
  if (typeof ctx.data === "undefined") {
607
- return { message: required_error !== null && required_error !== void 0 ? required_error : ctx.defaultError };
679
+ return { message: (_a = message !== null && message !== void 0 ? message : required_error) !== null && _a !== void 0 ? _a : ctx.defaultError };
608
680
  }
609
- return { message: invalid_type_error !== null && invalid_type_error !== void 0 ? invalid_type_error : ctx.defaultError };
681
+ if (iss.code !== "invalid_type")
682
+ return { message: ctx.defaultError };
683
+ return { message: (_b = message !== null && message !== void 0 ? message : invalid_type_error) !== null && _b !== void 0 ? _b : ctx.defaultError };
610
684
  };
611
685
  return { errorMap: customMap, description };
612
686
  }
@@ -864,11 +938,13 @@ class ZodType {
864
938
  }
865
939
  }
866
940
  const cuidRegex = /^c[^\s-]{8,}$/i;
867
- const cuid2Regex = /^[a-z][a-z0-9]*$/;
941
+ const cuid2Regex = /^[0-9a-z]+$/;
868
942
  const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/;
869
943
  // const uuidRegex =
870
944
  // /^([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;
871
945
  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;
946
+ const nanoidRegex = /^[a-z0-9_-]{21}$/i;
947
+ 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)?)??$/;
872
948
  // from https://stackoverflow.com/a/46181/1550155
873
949
  // old version: too slow, didn't support unicode
874
950
  // 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;
@@ -881,41 +957,48 @@ const uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-
881
957
  // /^[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])?)*$/;
882
958
  // const emailRegex =
883
959
  // /^(?:[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;
884
- const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_+-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
960
+ const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
885
961
  // const emailRegex =
886
962
  // /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;
887
963
  // from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
888
964
  const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
889
965
  let emojiRegex;
890
- 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}))$/;
966
+ // faster, simpler, safer
967
+ 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])$/;
891
968
  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})))$/;
892
- // Adapted from https://stackoverflow.com/a/3143231
893
- const datetimeRegex = (args) => {
969
+ // https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
970
+ const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
971
+ // simple
972
+ // const dateRegexSource = `\\d{4}-\\d{2}-\\d{2}`;
973
+ // no leap year validation
974
+ // 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))`;
975
+ // with leap year validation
976
+ 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])))`;
977
+ const dateRegex = new RegExp(`^${dateRegexSource}$`);
978
+ function timeRegexSource(args) {
979
+ // let regex = `\\d{2}:\\d{2}:\\d{2}`;
980
+ let regex = `([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d`;
894
981
  if (args.precision) {
895
- if (args.offset) {
896
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${args.precision}}(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
897
- }
898
- else {
899
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${args.precision}}Z$`);
900
- }
982
+ regex = `${regex}\\.\\d{${args.precision}}`;
901
983
  }
902
- else if (args.precision === 0) {
903
- if (args.offset) {
904
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
905
- }
906
- else {
907
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$`);
908
- }
984
+ else if (args.precision == null) {
985
+ regex = `${regex}(\\.\\d+)?`;
909
986
  }
910
- else {
911
- if (args.offset) {
912
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
913
- }
914
- else {
915
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?Z$`);
916
- }
917
- }
918
- };
987
+ return regex;
988
+ }
989
+ function timeRegex(args) {
990
+ return new RegExp(`^${timeRegexSource(args)}$`);
991
+ }
992
+ // Adapted from https://stackoverflow.com/a/3143231
993
+ function datetimeRegex(args) {
994
+ let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
995
+ const opts = [];
996
+ opts.push(args.local ? `Z?` : `Z`);
997
+ if (args.offset)
998
+ opts.push(`([+-]\\d{2}:?\\d{2})`);
999
+ regex = `${regex}(${opts.join("|")})`;
1000
+ return new RegExp(`^${regex}$`);
1001
+ }
919
1002
  function isValidIP(ip, version) {
920
1003
  if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
921
1004
  return true;
@@ -937,9 +1020,7 @@ class ZodString extends ZodType {
937
1020
  code: ZodIssueCode.invalid_type,
938
1021
  expected: ZodParsedType.string,
939
1022
  received: ctx.parsedType,
940
- }
941
- //
942
- );
1023
+ });
943
1024
  return INVALID;
944
1025
  }
945
1026
  const status = new ParseStatus();
@@ -1037,6 +1118,17 @@ class ZodString extends ZodType {
1037
1118
  status.dirty();
1038
1119
  }
1039
1120
  }
1121
+ else if (check.kind === "nanoid") {
1122
+ if (!nanoidRegex.test(input.data)) {
1123
+ ctx = this._getOrReturnCtx(input, ctx);
1124
+ addIssueToContext(ctx, {
1125
+ validation: "nanoid",
1126
+ code: ZodIssueCode.invalid_string,
1127
+ message: check.message,
1128
+ });
1129
+ status.dirty();
1130
+ }
1131
+ }
1040
1132
  else if (check.kind === "cuid") {
1041
1133
  if (!cuidRegex.test(input.data)) {
1042
1134
  ctx = this._getOrReturnCtx(input, ctx);
@@ -1151,6 +1243,41 @@ class ZodString extends ZodType {
1151
1243
  status.dirty();
1152
1244
  }
1153
1245
  }
1246
+ else if (check.kind === "date") {
1247
+ const regex = dateRegex;
1248
+ if (!regex.test(input.data)) {
1249
+ ctx = this._getOrReturnCtx(input, ctx);
1250
+ addIssueToContext(ctx, {
1251
+ code: ZodIssueCode.invalid_string,
1252
+ validation: "date",
1253
+ message: check.message,
1254
+ });
1255
+ status.dirty();
1256
+ }
1257
+ }
1258
+ else if (check.kind === "time") {
1259
+ const regex = timeRegex(check);
1260
+ if (!regex.test(input.data)) {
1261
+ ctx = this._getOrReturnCtx(input, ctx);
1262
+ addIssueToContext(ctx, {
1263
+ code: ZodIssueCode.invalid_string,
1264
+ validation: "time",
1265
+ message: check.message,
1266
+ });
1267
+ status.dirty();
1268
+ }
1269
+ }
1270
+ else if (check.kind === "duration") {
1271
+ if (!durationRegex.test(input.data)) {
1272
+ ctx = this._getOrReturnCtx(input, ctx);
1273
+ addIssueToContext(ctx, {
1274
+ validation: "duration",
1275
+ code: ZodIssueCode.invalid_string,
1276
+ message: check.message,
1277
+ });
1278
+ status.dirty();
1279
+ }
1280
+ }
1154
1281
  else if (check.kind === "ip") {
1155
1282
  if (!isValidIP(input.data, check.version)) {
1156
1283
  ctx = this._getOrReturnCtx(input, ctx);
@@ -1162,6 +1289,17 @@ class ZodString extends ZodType {
1162
1289
  status.dirty();
1163
1290
  }
1164
1291
  }
1292
+ else if (check.kind === "base64") {
1293
+ if (!base64Regex.test(input.data)) {
1294
+ ctx = this._getOrReturnCtx(input, ctx);
1295
+ addIssueToContext(ctx, {
1296
+ validation: "base64",
1297
+ code: ZodIssueCode.invalid_string,
1298
+ message: check.message,
1299
+ });
1300
+ status.dirty();
1301
+ }
1302
+ }
1165
1303
  else {
1166
1304
  util.assertNever(check);
1167
1305
  }
@@ -1193,6 +1331,9 @@ class ZodString extends ZodType {
1193
1331
  uuid(message) {
1194
1332
  return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
1195
1333
  }
1334
+ nanoid(message) {
1335
+ return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
1336
+ }
1196
1337
  cuid(message) {
1197
1338
  return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
1198
1339
  }
@@ -1202,16 +1343,20 @@ class ZodString extends ZodType {
1202
1343
  ulid(message) {
1203
1344
  return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
1204
1345
  }
1346
+ base64(message) {
1347
+ return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
1348
+ }
1205
1349
  ip(options) {
1206
1350
  return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
1207
1351
  }
1208
1352
  datetime(options) {
1209
- var _a;
1353
+ var _a, _b;
1210
1354
  if (typeof options === "string") {
1211
1355
  return this._addCheck({
1212
1356
  kind: "datetime",
1213
1357
  precision: null,
1214
1358
  offset: false,
1359
+ local: false,
1215
1360
  message: options,
1216
1361
  });
1217
1362
  }
@@ -1219,9 +1364,30 @@ class ZodString extends ZodType {
1219
1364
  kind: "datetime",
1220
1365
  precision: typeof (options === null || options === void 0 ? void 0 : options.precision) === "undefined" ? null : options === null || options === void 0 ? void 0 : options.precision,
1221
1366
  offset: (_a = options === null || options === void 0 ? void 0 : options.offset) !== null && _a !== void 0 ? _a : false,
1367
+ local: (_b = options === null || options === void 0 ? void 0 : options.local) !== null && _b !== void 0 ? _b : false,
1222
1368
  ...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
1223
1369
  });
1224
1370
  }
1371
+ date(message) {
1372
+ return this._addCheck({ kind: "date", message });
1373
+ }
1374
+ time(options) {
1375
+ if (typeof options === "string") {
1376
+ return this._addCheck({
1377
+ kind: "time",
1378
+ precision: null,
1379
+ message: options,
1380
+ });
1381
+ }
1382
+ return this._addCheck({
1383
+ kind: "time",
1384
+ precision: typeof (options === null || options === void 0 ? void 0 : options.precision) === "undefined" ? null : options === null || options === void 0 ? void 0 : options.precision,
1385
+ ...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
1386
+ });
1387
+ }
1388
+ duration(message) {
1389
+ return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
1390
+ }
1225
1391
  regex(regex, message) {
1226
1392
  return this._addCheck({
1227
1393
  kind: "regex",
@@ -1300,6 +1466,15 @@ class ZodString extends ZodType {
1300
1466
  get isDatetime() {
1301
1467
  return !!this._def.checks.find((ch) => ch.kind === "datetime");
1302
1468
  }
1469
+ get isDate() {
1470
+ return !!this._def.checks.find((ch) => ch.kind === "date");
1471
+ }
1472
+ get isTime() {
1473
+ return !!this._def.checks.find((ch) => ch.kind === "time");
1474
+ }
1475
+ get isDuration() {
1476
+ return !!this._def.checks.find((ch) => ch.kind === "duration");
1477
+ }
1303
1478
  get isEmail() {
1304
1479
  return !!this._def.checks.find((ch) => ch.kind === "email");
1305
1480
  }
@@ -1312,6 +1487,9 @@ class ZodString extends ZodType {
1312
1487
  get isUUID() {
1313
1488
  return !!this._def.checks.find((ch) => ch.kind === "uuid");
1314
1489
  }
1490
+ get isNANOID() {
1491
+ return !!this._def.checks.find((ch) => ch.kind === "nanoid");
1492
+ }
1315
1493
  get isCUID() {
1316
1494
  return !!this._def.checks.find((ch) => ch.kind === "cuid");
1317
1495
  }
@@ -1324,6 +1502,9 @@ class ZodString extends ZodType {
1324
1502
  get isIP() {
1325
1503
  return !!this._def.checks.find((ch) => ch.kind === "ip");
1326
1504
  }
1505
+ get isBase64() {
1506
+ return !!this._def.checks.find((ch) => ch.kind === "base64");
1507
+ }
1327
1508
  get minLength() {
1328
1509
  let min = null;
1329
1510
  for (const ch of this._def.checks) {
@@ -2311,9 +2492,10 @@ class ZodObject extends ZodType {
2311
2492
  const syncPairs = [];
2312
2493
  for (const pair of pairs) {
2313
2494
  const key = await pair.key;
2495
+ const value = await pair.value;
2314
2496
  syncPairs.push({
2315
2497
  key,
2316
- value: await pair.value,
2498
+ value,
2317
2499
  alwaysSet: pair.alwaysSet,
2318
2500
  });
2319
2501
  }
@@ -2687,7 +2869,7 @@ const getDiscriminator = (type) => {
2687
2869
  }
2688
2870
  else if (type instanceof ZodNativeEnum) {
2689
2871
  // eslint-disable-next-line ban/ban
2690
- return Object.keys(type.enum);
2872
+ return util.objectValues(type.enum);
2691
2873
  }
2692
2874
  else if (type instanceof ZodDefault) {
2693
2875
  return getDiscriminator(type._def.innerType);
@@ -2698,8 +2880,23 @@ const getDiscriminator = (type) => {
2698
2880
  else if (type instanceof ZodNull) {
2699
2881
  return [null];
2700
2882
  }
2883
+ else if (type instanceof ZodOptional) {
2884
+ return [undefined, ...getDiscriminator(type.unwrap())];
2885
+ }
2886
+ else if (type instanceof ZodNullable) {
2887
+ return [null, ...getDiscriminator(type.unwrap())];
2888
+ }
2889
+ else if (type instanceof ZodBranded) {
2890
+ return getDiscriminator(type.unwrap());
2891
+ }
2892
+ else if (type instanceof ZodReadonly) {
2893
+ return getDiscriminator(type.unwrap());
2894
+ }
2895
+ else if (type instanceof ZodCatch) {
2896
+ return getDiscriminator(type._def.innerType);
2897
+ }
2701
2898
  else {
2702
- return null;
2899
+ return [];
2703
2900
  }
2704
2901
  };
2705
2902
  class ZodDiscriminatedUnion extends ZodType {
@@ -2762,7 +2959,7 @@ class ZodDiscriminatedUnion extends ZodType {
2762
2959
  // try {
2763
2960
  for (const type of options) {
2764
2961
  const discriminatorValues = getDiscriminator(type.shape[discriminator]);
2765
- if (!discriminatorValues) {
2962
+ if (!discriminatorValues.length) {
2766
2963
  throw new Error(`A discriminator value for key \`${discriminator}\` could not be extracted from all schema options`);
2767
2964
  }
2768
2965
  for (const value of discriminatorValues) {
@@ -2975,6 +3172,7 @@ class ZodRecord extends ZodType {
2975
3172
  pairs.push({
2976
3173
  key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
2977
3174
  value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
3175
+ alwaysSet: key in ctx.data,
2978
3176
  });
2979
3177
  }
2980
3178
  if (ctx.common.async) {
@@ -3334,6 +3532,10 @@ function createZodEnum(values, params) {
3334
3532
  });
3335
3533
  }
3336
3534
  class ZodEnum extends ZodType {
3535
+ constructor() {
3536
+ super(...arguments);
3537
+ _ZodEnum_cache.set(this, void 0);
3538
+ }
3337
3539
  _parse(input) {
3338
3540
  if (typeof input.data !== "string") {
3339
3541
  const ctx = this._getOrReturnCtx(input);
@@ -3345,7 +3547,10 @@ class ZodEnum extends ZodType {
3345
3547
  });
3346
3548
  return INVALID;
3347
3549
  }
3348
- if (this._def.values.indexOf(input.data) === -1) {
3550
+ if (!__classPrivateFieldGet(this, _ZodEnum_cache, "f")) {
3551
+ __classPrivateFieldSet(this, _ZodEnum_cache, new Set(this._def.values), "f");
3552
+ }
3553
+ if (!__classPrivateFieldGet(this, _ZodEnum_cache, "f").has(input.data)) {
3349
3554
  const ctx = this._getOrReturnCtx(input);
3350
3555
  const expectedValues = this._def.values;
3351
3556
  addIssueToContext(ctx, {
@@ -3381,15 +3586,26 @@ class ZodEnum extends ZodType {
3381
3586
  }
3382
3587
  return enumValues;
3383
3588
  }
3384
- extract(values) {
3385
- return ZodEnum.create(values);
3589
+ extract(values, newDef = this._def) {
3590
+ return ZodEnum.create(values, {
3591
+ ...this._def,
3592
+ ...newDef,
3593
+ });
3386
3594
  }
3387
- exclude(values) {
3388
- return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)));
3595
+ exclude(values, newDef = this._def) {
3596
+ return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
3597
+ ...this._def,
3598
+ ...newDef,
3599
+ });
3389
3600
  }
3390
3601
  }
3602
+ _ZodEnum_cache = new WeakMap();
3391
3603
  ZodEnum.create = createZodEnum;
3392
3604
  class ZodNativeEnum extends ZodType {
3605
+ constructor() {
3606
+ super(...arguments);
3607
+ _ZodNativeEnum_cache.set(this, void 0);
3608
+ }
3393
3609
  _parse(input) {
3394
3610
  const nativeEnumValues = util.getValidEnumValues(this._def.values);
3395
3611
  const ctx = this._getOrReturnCtx(input);
@@ -3403,7 +3619,10 @@ class ZodNativeEnum extends ZodType {
3403
3619
  });
3404
3620
  return INVALID;
3405
3621
  }
3406
- if (nativeEnumValues.indexOf(input.data) === -1) {
3622
+ if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache, "f")) {
3623
+ __classPrivateFieldSet(this, _ZodNativeEnum_cache, new Set(util.getValidEnumValues(this._def.values)), "f");
3624
+ }
3625
+ if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache, "f").has(input.data)) {
3407
3626
  const expectedValues = util.objectValues(nativeEnumValues);
3408
3627
  addIssueToContext(ctx, {
3409
3628
  received: ctx.data,
@@ -3418,6 +3637,7 @@ class ZodNativeEnum extends ZodType {
3418
3637
  return this._def.values;
3419
3638
  }
3420
3639
  }
3640
+ _ZodNativeEnum_cache = new WeakMap();
3421
3641
  ZodNativeEnum.create = (values, params) => {
3422
3642
  return new ZodNativeEnum({
3423
3643
  values: values,
@@ -3487,33 +3707,43 @@ class ZodEffects extends ZodType {
3487
3707
  checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
3488
3708
  if (effect.type === "preprocess") {
3489
3709
  const processed = effect.transform(ctx.data, checkCtx);
3490
- if (ctx.common.issues.length) {
3491
- return {
3492
- status: "dirty",
3493
- value: ctx.data,
3494
- };
3495
- }
3496
3710
  if (ctx.common.async) {
3497
- return Promise.resolve(processed).then((processed) => {
3498
- return this._def.schema._parseAsync({
3711
+ return Promise.resolve(processed).then(async (processed) => {
3712
+ if (status.value === "aborted")
3713
+ return INVALID;
3714
+ const result = await this._def.schema._parseAsync({
3499
3715
  data: processed,
3500
3716
  path: ctx.path,
3501
3717
  parent: ctx,
3502
3718
  });
3719
+ if (result.status === "aborted")
3720
+ return INVALID;
3721
+ if (result.status === "dirty")
3722
+ return DIRTY(result.value);
3723
+ if (status.value === "dirty")
3724
+ return DIRTY(result.value);
3725
+ return result;
3503
3726
  });
3504
3727
  }
3505
3728
  else {
3506
- return this._def.schema._parseSync({
3729
+ if (status.value === "aborted")
3730
+ return INVALID;
3731
+ const result = this._def.schema._parseSync({
3507
3732
  data: processed,
3508
3733
  path: ctx.path,
3509
3734
  parent: ctx,
3510
3735
  });
3736
+ if (result.status === "aborted")
3737
+ return INVALID;
3738
+ if (result.status === "dirty")
3739
+ return DIRTY(result.value);
3740
+ if (status.value === "dirty")
3741
+ return DIRTY(result.value);
3742
+ return result;
3511
3743
  }
3512
3744
  }
3513
3745
  if (effect.type === "refinement") {
3514
- const executeRefinement = (acc
3515
- // effect: RefinementEffect<any>
3516
- ) => {
3746
+ const executeRefinement = (acc) => {
3517
3747
  const result = effect.refinement(acc, checkCtx);
3518
3748
  if (ctx.common.async) {
3519
3749
  return Promise.resolve(result);
@@ -3815,10 +4045,18 @@ class ZodPipeline extends ZodType {
3815
4045
  class ZodReadonly extends ZodType {
3816
4046
  _parse(input) {
3817
4047
  const result = this._def.innerType._parse(input);
3818
- if (isValid(result)) {
3819
- result.value = Object.freeze(result.value);
3820
- }
3821
- return result;
4048
+ const freeze = (data) => {
4049
+ if (isValid(data)) {
4050
+ data.value = Object.freeze(data.value);
4051
+ }
4052
+ return data;
4053
+ };
4054
+ return isAsync(result)
4055
+ ? result.then((data) => freeze(data))
4056
+ : freeze(result);
4057
+ }
4058
+ unwrap() {
4059
+ return this._def.innerType;
3822
4060
  }
3823
4061
  }
3824
4062
  ZodReadonly.create = (type, params) => {
@@ -3934,8 +4172,23 @@ const formatErrorPath = (path) => {
3934
4172
  return formatted;
3935
4173
  };
3936
4174
 
4175
+ /**
4176
+ * Get the hash of the content string. It returns the first 5 characters of the hash
4177
+ * Example: getContentHash("Hello World")
4178
+ * @param contentString The content string to hash
4179
+ * @returns
4180
+ */
4181
+ const getContentHash = (contentString) => {
4182
+ return crypto__namespace
4183
+ .createHash("md5")
4184
+ .update(contentString)
4185
+ .digest("hex")
4186
+ .substring(0, 5);
4187
+ };
4188
+
3937
4189
  exports.errorFormatter = errorFormatter;
3938
4190
  exports.findFiles = findFiles;
3939
4191
  exports.formatErrorPath = formatErrorPath;
4192
+ exports.getContentHash = getContentHash;
3940
4193
  exports.runProcess = runProcess;
3941
4194
  //# sourceMappingURL=index.js.map