@embeddable.com/sdk-utils 0.3.2 → 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.
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Get the hash of the content string. It returns the first 5 characters of the hash
3
+ * Example: getContentHash("Hello World")
4
+ * @param contentString The content string to hash
5
+ * @returns
6
+ */
7
+ export declare const getContentHash: (contentString: string) => string;
package/lib/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { default as findFiles } from "./findFiles";
2
2
  export { default as runProcess } from "./runProcess";
3
3
  export { errorFormatter, formatErrorPath } from "./errorFormatter";
4
+ export { getContentHash } from "./fileContentHash";
package/lib/index.esm.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { readdir, lstat } from 'node:fs/promises';
2
2
  import { join } from 'node:path';
3
3
  import { fork } from 'node:child_process';
4
+ import * as crypto from 'node:crypto';
4
5
 
5
6
  var findFiles = async (initialSrcDir, regex) => {
6
7
  const filesList = [];
@@ -283,6 +284,11 @@ class ZodError extends Error {
283
284
  processError(this);
284
285
  return fieldErrors;
285
286
  }
287
+ static assert(value) {
288
+ if (!(value instanceof ZodError)) {
289
+ throw new Error(`Not a ZodError: ${value}`);
290
+ }
291
+ }
286
292
  toString() {
287
293
  return this.message;
288
294
  }
@@ -452,6 +458,13 @@ const makeIssue = (params) => {
452
458
  ...issueData,
453
459
  path: fullPath,
454
460
  };
461
+ if (issueData.message !== undefined) {
462
+ return {
463
+ ...issueData,
464
+ path: fullPath,
465
+ message: issueData.message,
466
+ };
467
+ }
455
468
  let errorMessage = "";
456
469
  const maps = errorMaps
457
470
  .filter((m) => !!m)
@@ -463,10 +476,11 @@ const makeIssue = (params) => {
463
476
  return {
464
477
  ...issueData,
465
478
  path: fullPath,
466
- message: issueData.message || errorMessage,
479
+ message: errorMessage,
467
480
  };
468
481
  };
469
482
  function addIssueToContext(ctx, issueData) {
483
+ const overrideMap = getErrorMap();
470
484
  const issue = makeIssue({
471
485
  issueData: issueData,
472
486
  data: ctx.data,
@@ -474,8 +488,8 @@ function addIssueToContext(ctx, issueData) {
474
488
  errorMaps: [
475
489
  ctx.common.contextualErrorMap,
476
490
  ctx.schemaErrorMap,
477
- getErrorMap(),
478
- errorMap, // then global default map
491
+ overrideMap,
492
+ overrideMap === errorMap ? undefined : errorMap, // then global default map
479
493
  ].filter((x) => !!x),
480
494
  });
481
495
  ctx.common.issues.push(issue);
@@ -506,9 +520,11 @@ class ParseStatus {
506
520
  static async mergeObjectAsync(status, pairs) {
507
521
  const syncPairs = [];
508
522
  for (const pair of pairs) {
523
+ const key = await pair.key;
524
+ const value = await pair.value;
509
525
  syncPairs.push({
510
- key: await pair.key,
511
- value: await pair.value,
526
+ key,
527
+ value,
512
528
  });
513
529
  }
514
530
  return ParseStatus.mergeObjectSync(status, syncPairs);
@@ -543,12 +559,46 @@ const isDirty = (x) => x.status === "dirty";
543
559
  const isValid = (x) => x.status === "valid";
544
560
  const isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
545
561
 
562
+ /******************************************************************************
563
+ Copyright (c) Microsoft Corporation.
564
+
565
+ Permission to use, copy, modify, and/or distribute this software for any
566
+ purpose with or without fee is hereby granted.
567
+
568
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
569
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
570
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
571
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
572
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
573
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
574
+ PERFORMANCE OF THIS SOFTWARE.
575
+ ***************************************************************************** */
576
+
577
+ function __classPrivateFieldGet(receiver, state, kind, f) {
578
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
579
+ 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");
580
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
581
+ }
582
+
583
+ function __classPrivateFieldSet(receiver, state, value, kind, f) {
584
+ if (kind === "m") throw new TypeError("Private method is not writable");
585
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
586
+ 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");
587
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
588
+ }
589
+
590
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
591
+ var e = new Error(message);
592
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
593
+ };
594
+
546
595
  var errorUtil;
547
596
  (function (errorUtil) {
548
597
  errorUtil.errToObj = (message) => typeof message === "string" ? { message } : message || {};
549
598
  errorUtil.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
550
599
  })(errorUtil || (errorUtil = {}));
551
600
 
601
+ var _ZodEnum_cache, _ZodNativeEnum_cache;
552
602
  class ParseInputLazyPath {
553
603
  constructor(parent, value, path, key) {
554
604
  this._cachedPath = [];
@@ -599,12 +649,17 @@ function processCreateParams(params) {
599
649
  if (errorMap)
600
650
  return { errorMap: errorMap, description };
601
651
  const customMap = (iss, ctx) => {
602
- if (iss.code !== "invalid_type")
603
- return { message: ctx.defaultError };
652
+ var _a, _b;
653
+ const { message } = params;
654
+ if (iss.code === "invalid_enum_value") {
655
+ return { message: message !== null && message !== void 0 ? message : ctx.defaultError };
656
+ }
604
657
  if (typeof ctx.data === "undefined") {
605
- return { message: required_error !== null && required_error !== void 0 ? required_error : ctx.defaultError };
658
+ return { message: (_a = message !== null && message !== void 0 ? message : required_error) !== null && _a !== void 0 ? _a : ctx.defaultError };
606
659
  }
607
- return { message: invalid_type_error !== null && invalid_type_error !== void 0 ? invalid_type_error : ctx.defaultError };
660
+ if (iss.code !== "invalid_type")
661
+ return { message: ctx.defaultError };
662
+ return { message: (_b = message !== null && message !== void 0 ? message : invalid_type_error) !== null && _b !== void 0 ? _b : ctx.defaultError };
608
663
  };
609
664
  return { errorMap: customMap, description };
610
665
  }
@@ -862,11 +917,13 @@ class ZodType {
862
917
  }
863
918
  }
864
919
  const cuidRegex = /^c[^\s-]{8,}$/i;
865
- const cuid2Regex = /^[a-z][a-z0-9]*$/;
920
+ const cuid2Regex = /^[0-9a-z]+$/;
866
921
  const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/;
867
922
  // const uuidRegex =
868
923
  // /^([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;
869
924
  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;
925
+ const nanoidRegex = /^[a-z0-9_-]{21}$/i;
926
+ 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)?)??$/;
870
927
  // from https://stackoverflow.com/a/46181/1550155
871
928
  // old version: too slow, didn't support unicode
872
929
  // 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;
@@ -879,41 +936,48 @@ const uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-
879
936
  // /^[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])?)*$/;
880
937
  // const emailRegex =
881
938
  // /^(?:[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;
882
- const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_+-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
939
+ const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
883
940
  // const emailRegex =
884
941
  // /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;
885
942
  // from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
886
943
  const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
887
944
  let emojiRegex;
888
- 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}))$/;
945
+ // faster, simpler, safer
946
+ 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])$/;
889
947
  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})))$/;
890
- // Adapted from https://stackoverflow.com/a/3143231
891
- const datetimeRegex = (args) => {
948
+ // https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
949
+ const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
950
+ // simple
951
+ // const dateRegexSource = `\\d{4}-\\d{2}-\\d{2}`;
952
+ // no leap year validation
953
+ // 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))`;
954
+ // with leap year validation
955
+ 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])))`;
956
+ const dateRegex = new RegExp(`^${dateRegexSource}$`);
957
+ function timeRegexSource(args) {
958
+ // let regex = `\\d{2}:\\d{2}:\\d{2}`;
959
+ let regex = `([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d`;
892
960
  if (args.precision) {
893
- if (args.offset) {
894
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${args.precision}}(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
895
- }
896
- else {
897
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${args.precision}}Z$`);
898
- }
961
+ regex = `${regex}\\.\\d{${args.precision}}`;
899
962
  }
900
- else if (args.precision === 0) {
901
- if (args.offset) {
902
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
903
- }
904
- else {
905
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$`);
906
- }
907
- }
908
- else {
909
- if (args.offset) {
910
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
911
- }
912
- else {
913
- return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?Z$`);
914
- }
963
+ else if (args.precision == null) {
964
+ regex = `${regex}(\\.\\d+)?`;
915
965
  }
916
- };
966
+ return regex;
967
+ }
968
+ function timeRegex(args) {
969
+ return new RegExp(`^${timeRegexSource(args)}$`);
970
+ }
971
+ // Adapted from https://stackoverflow.com/a/3143231
972
+ function datetimeRegex(args) {
973
+ let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
974
+ const opts = [];
975
+ opts.push(args.local ? `Z?` : `Z`);
976
+ if (args.offset)
977
+ opts.push(`([+-]\\d{2}:?\\d{2})`);
978
+ regex = `${regex}(${opts.join("|")})`;
979
+ return new RegExp(`^${regex}$`);
980
+ }
917
981
  function isValidIP(ip, version) {
918
982
  if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
919
983
  return true;
@@ -935,9 +999,7 @@ class ZodString extends ZodType {
935
999
  code: ZodIssueCode.invalid_type,
936
1000
  expected: ZodParsedType.string,
937
1001
  received: ctx.parsedType,
938
- }
939
- //
940
- );
1002
+ });
941
1003
  return INVALID;
942
1004
  }
943
1005
  const status = new ParseStatus();
@@ -1035,6 +1097,17 @@ class ZodString extends ZodType {
1035
1097
  status.dirty();
1036
1098
  }
1037
1099
  }
1100
+ else if (check.kind === "nanoid") {
1101
+ if (!nanoidRegex.test(input.data)) {
1102
+ ctx = this._getOrReturnCtx(input, ctx);
1103
+ addIssueToContext(ctx, {
1104
+ validation: "nanoid",
1105
+ code: ZodIssueCode.invalid_string,
1106
+ message: check.message,
1107
+ });
1108
+ status.dirty();
1109
+ }
1110
+ }
1038
1111
  else if (check.kind === "cuid") {
1039
1112
  if (!cuidRegex.test(input.data)) {
1040
1113
  ctx = this._getOrReturnCtx(input, ctx);
@@ -1149,6 +1222,41 @@ class ZodString extends ZodType {
1149
1222
  status.dirty();
1150
1223
  }
1151
1224
  }
1225
+ else if (check.kind === "date") {
1226
+ const regex = dateRegex;
1227
+ if (!regex.test(input.data)) {
1228
+ ctx = this._getOrReturnCtx(input, ctx);
1229
+ addIssueToContext(ctx, {
1230
+ code: ZodIssueCode.invalid_string,
1231
+ validation: "date",
1232
+ message: check.message,
1233
+ });
1234
+ status.dirty();
1235
+ }
1236
+ }
1237
+ else if (check.kind === "time") {
1238
+ const regex = timeRegex(check);
1239
+ if (!regex.test(input.data)) {
1240
+ ctx = this._getOrReturnCtx(input, ctx);
1241
+ addIssueToContext(ctx, {
1242
+ code: ZodIssueCode.invalid_string,
1243
+ validation: "time",
1244
+ message: check.message,
1245
+ });
1246
+ status.dirty();
1247
+ }
1248
+ }
1249
+ else if (check.kind === "duration") {
1250
+ if (!durationRegex.test(input.data)) {
1251
+ ctx = this._getOrReturnCtx(input, ctx);
1252
+ addIssueToContext(ctx, {
1253
+ validation: "duration",
1254
+ code: ZodIssueCode.invalid_string,
1255
+ message: check.message,
1256
+ });
1257
+ status.dirty();
1258
+ }
1259
+ }
1152
1260
  else if (check.kind === "ip") {
1153
1261
  if (!isValidIP(input.data, check.version)) {
1154
1262
  ctx = this._getOrReturnCtx(input, ctx);
@@ -1160,6 +1268,17 @@ class ZodString extends ZodType {
1160
1268
  status.dirty();
1161
1269
  }
1162
1270
  }
1271
+ else if (check.kind === "base64") {
1272
+ if (!base64Regex.test(input.data)) {
1273
+ ctx = this._getOrReturnCtx(input, ctx);
1274
+ addIssueToContext(ctx, {
1275
+ validation: "base64",
1276
+ code: ZodIssueCode.invalid_string,
1277
+ message: check.message,
1278
+ });
1279
+ status.dirty();
1280
+ }
1281
+ }
1163
1282
  else {
1164
1283
  util.assertNever(check);
1165
1284
  }
@@ -1191,6 +1310,9 @@ class ZodString extends ZodType {
1191
1310
  uuid(message) {
1192
1311
  return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
1193
1312
  }
1313
+ nanoid(message) {
1314
+ return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
1315
+ }
1194
1316
  cuid(message) {
1195
1317
  return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
1196
1318
  }
@@ -1200,16 +1322,20 @@ class ZodString extends ZodType {
1200
1322
  ulid(message) {
1201
1323
  return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
1202
1324
  }
1325
+ base64(message) {
1326
+ return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
1327
+ }
1203
1328
  ip(options) {
1204
1329
  return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
1205
1330
  }
1206
1331
  datetime(options) {
1207
- var _a;
1332
+ var _a, _b;
1208
1333
  if (typeof options === "string") {
1209
1334
  return this._addCheck({
1210
1335
  kind: "datetime",
1211
1336
  precision: null,
1212
1337
  offset: false,
1338
+ local: false,
1213
1339
  message: options,
1214
1340
  });
1215
1341
  }
@@ -1217,9 +1343,30 @@ class ZodString extends ZodType {
1217
1343
  kind: "datetime",
1218
1344
  precision: typeof (options === null || options === void 0 ? void 0 : options.precision) === "undefined" ? null : options === null || options === void 0 ? void 0 : options.precision,
1219
1345
  offset: (_a = options === null || options === void 0 ? void 0 : options.offset) !== null && _a !== void 0 ? _a : false,
1346
+ local: (_b = options === null || options === void 0 ? void 0 : options.local) !== null && _b !== void 0 ? _b : false,
1220
1347
  ...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
1221
1348
  });
1222
1349
  }
1350
+ date(message) {
1351
+ return this._addCheck({ kind: "date", message });
1352
+ }
1353
+ time(options) {
1354
+ if (typeof options === "string") {
1355
+ return this._addCheck({
1356
+ kind: "time",
1357
+ precision: null,
1358
+ message: options,
1359
+ });
1360
+ }
1361
+ return this._addCheck({
1362
+ kind: "time",
1363
+ precision: typeof (options === null || options === void 0 ? void 0 : options.precision) === "undefined" ? null : options === null || options === void 0 ? void 0 : options.precision,
1364
+ ...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
1365
+ });
1366
+ }
1367
+ duration(message) {
1368
+ return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
1369
+ }
1223
1370
  regex(regex, message) {
1224
1371
  return this._addCheck({
1225
1372
  kind: "regex",
@@ -1298,6 +1445,15 @@ class ZodString extends ZodType {
1298
1445
  get isDatetime() {
1299
1446
  return !!this._def.checks.find((ch) => ch.kind === "datetime");
1300
1447
  }
1448
+ get isDate() {
1449
+ return !!this._def.checks.find((ch) => ch.kind === "date");
1450
+ }
1451
+ get isTime() {
1452
+ return !!this._def.checks.find((ch) => ch.kind === "time");
1453
+ }
1454
+ get isDuration() {
1455
+ return !!this._def.checks.find((ch) => ch.kind === "duration");
1456
+ }
1301
1457
  get isEmail() {
1302
1458
  return !!this._def.checks.find((ch) => ch.kind === "email");
1303
1459
  }
@@ -1310,6 +1466,9 @@ class ZodString extends ZodType {
1310
1466
  get isUUID() {
1311
1467
  return !!this._def.checks.find((ch) => ch.kind === "uuid");
1312
1468
  }
1469
+ get isNANOID() {
1470
+ return !!this._def.checks.find((ch) => ch.kind === "nanoid");
1471
+ }
1313
1472
  get isCUID() {
1314
1473
  return !!this._def.checks.find((ch) => ch.kind === "cuid");
1315
1474
  }
@@ -1322,6 +1481,9 @@ class ZodString extends ZodType {
1322
1481
  get isIP() {
1323
1482
  return !!this._def.checks.find((ch) => ch.kind === "ip");
1324
1483
  }
1484
+ get isBase64() {
1485
+ return !!this._def.checks.find((ch) => ch.kind === "base64");
1486
+ }
1325
1487
  get minLength() {
1326
1488
  let min = null;
1327
1489
  for (const ch of this._def.checks) {
@@ -2309,9 +2471,10 @@ class ZodObject extends ZodType {
2309
2471
  const syncPairs = [];
2310
2472
  for (const pair of pairs) {
2311
2473
  const key = await pair.key;
2474
+ const value = await pair.value;
2312
2475
  syncPairs.push({
2313
2476
  key,
2314
- value: await pair.value,
2477
+ value,
2315
2478
  alwaysSet: pair.alwaysSet,
2316
2479
  });
2317
2480
  }
@@ -2685,7 +2848,7 @@ const getDiscriminator = (type) => {
2685
2848
  }
2686
2849
  else if (type instanceof ZodNativeEnum) {
2687
2850
  // eslint-disable-next-line ban/ban
2688
- return Object.keys(type.enum);
2851
+ return util.objectValues(type.enum);
2689
2852
  }
2690
2853
  else if (type instanceof ZodDefault) {
2691
2854
  return getDiscriminator(type._def.innerType);
@@ -2696,8 +2859,23 @@ const getDiscriminator = (type) => {
2696
2859
  else if (type instanceof ZodNull) {
2697
2860
  return [null];
2698
2861
  }
2862
+ else if (type instanceof ZodOptional) {
2863
+ return [undefined, ...getDiscriminator(type.unwrap())];
2864
+ }
2865
+ else if (type instanceof ZodNullable) {
2866
+ return [null, ...getDiscriminator(type.unwrap())];
2867
+ }
2868
+ else if (type instanceof ZodBranded) {
2869
+ return getDiscriminator(type.unwrap());
2870
+ }
2871
+ else if (type instanceof ZodReadonly) {
2872
+ return getDiscriminator(type.unwrap());
2873
+ }
2874
+ else if (type instanceof ZodCatch) {
2875
+ return getDiscriminator(type._def.innerType);
2876
+ }
2699
2877
  else {
2700
- return null;
2878
+ return [];
2701
2879
  }
2702
2880
  };
2703
2881
  class ZodDiscriminatedUnion extends ZodType {
@@ -2760,7 +2938,7 @@ class ZodDiscriminatedUnion extends ZodType {
2760
2938
  // try {
2761
2939
  for (const type of options) {
2762
2940
  const discriminatorValues = getDiscriminator(type.shape[discriminator]);
2763
- if (!discriminatorValues) {
2941
+ if (!discriminatorValues.length) {
2764
2942
  throw new Error(`A discriminator value for key \`${discriminator}\` could not be extracted from all schema options`);
2765
2943
  }
2766
2944
  for (const value of discriminatorValues) {
@@ -2973,6 +3151,7 @@ class ZodRecord extends ZodType {
2973
3151
  pairs.push({
2974
3152
  key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
2975
3153
  value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
3154
+ alwaysSet: key in ctx.data,
2976
3155
  });
2977
3156
  }
2978
3157
  if (ctx.common.async) {
@@ -3332,6 +3511,10 @@ function createZodEnum(values, params) {
3332
3511
  });
3333
3512
  }
3334
3513
  class ZodEnum extends ZodType {
3514
+ constructor() {
3515
+ super(...arguments);
3516
+ _ZodEnum_cache.set(this, void 0);
3517
+ }
3335
3518
  _parse(input) {
3336
3519
  if (typeof input.data !== "string") {
3337
3520
  const ctx = this._getOrReturnCtx(input);
@@ -3343,7 +3526,10 @@ class ZodEnum extends ZodType {
3343
3526
  });
3344
3527
  return INVALID;
3345
3528
  }
3346
- if (this._def.values.indexOf(input.data) === -1) {
3529
+ if (!__classPrivateFieldGet(this, _ZodEnum_cache, "f")) {
3530
+ __classPrivateFieldSet(this, _ZodEnum_cache, new Set(this._def.values), "f");
3531
+ }
3532
+ if (!__classPrivateFieldGet(this, _ZodEnum_cache, "f").has(input.data)) {
3347
3533
  const ctx = this._getOrReturnCtx(input);
3348
3534
  const expectedValues = this._def.values;
3349
3535
  addIssueToContext(ctx, {
@@ -3379,15 +3565,26 @@ class ZodEnum extends ZodType {
3379
3565
  }
3380
3566
  return enumValues;
3381
3567
  }
3382
- extract(values) {
3383
- return ZodEnum.create(values);
3568
+ extract(values, newDef = this._def) {
3569
+ return ZodEnum.create(values, {
3570
+ ...this._def,
3571
+ ...newDef,
3572
+ });
3384
3573
  }
3385
- exclude(values) {
3386
- return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)));
3574
+ exclude(values, newDef = this._def) {
3575
+ return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
3576
+ ...this._def,
3577
+ ...newDef,
3578
+ });
3387
3579
  }
3388
3580
  }
3581
+ _ZodEnum_cache = new WeakMap();
3389
3582
  ZodEnum.create = createZodEnum;
3390
3583
  class ZodNativeEnum extends ZodType {
3584
+ constructor() {
3585
+ super(...arguments);
3586
+ _ZodNativeEnum_cache.set(this, void 0);
3587
+ }
3391
3588
  _parse(input) {
3392
3589
  const nativeEnumValues = util.getValidEnumValues(this._def.values);
3393
3590
  const ctx = this._getOrReturnCtx(input);
@@ -3401,7 +3598,10 @@ class ZodNativeEnum extends ZodType {
3401
3598
  });
3402
3599
  return INVALID;
3403
3600
  }
3404
- if (nativeEnumValues.indexOf(input.data) === -1) {
3601
+ if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache, "f")) {
3602
+ __classPrivateFieldSet(this, _ZodNativeEnum_cache, new Set(util.getValidEnumValues(this._def.values)), "f");
3603
+ }
3604
+ if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache, "f").has(input.data)) {
3405
3605
  const expectedValues = util.objectValues(nativeEnumValues);
3406
3606
  addIssueToContext(ctx, {
3407
3607
  received: ctx.data,
@@ -3416,6 +3616,7 @@ class ZodNativeEnum extends ZodType {
3416
3616
  return this._def.values;
3417
3617
  }
3418
3618
  }
3619
+ _ZodNativeEnum_cache = new WeakMap();
3419
3620
  ZodNativeEnum.create = (values, params) => {
3420
3621
  return new ZodNativeEnum({
3421
3622
  values: values,
@@ -3485,33 +3686,43 @@ class ZodEffects extends ZodType {
3485
3686
  checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
3486
3687
  if (effect.type === "preprocess") {
3487
3688
  const processed = effect.transform(ctx.data, checkCtx);
3488
- if (ctx.common.issues.length) {
3489
- return {
3490
- status: "dirty",
3491
- value: ctx.data,
3492
- };
3493
- }
3494
3689
  if (ctx.common.async) {
3495
- return Promise.resolve(processed).then((processed) => {
3496
- return this._def.schema._parseAsync({
3690
+ return Promise.resolve(processed).then(async (processed) => {
3691
+ if (status.value === "aborted")
3692
+ return INVALID;
3693
+ const result = await this._def.schema._parseAsync({
3497
3694
  data: processed,
3498
3695
  path: ctx.path,
3499
3696
  parent: ctx,
3500
3697
  });
3698
+ if (result.status === "aborted")
3699
+ return INVALID;
3700
+ if (result.status === "dirty")
3701
+ return DIRTY(result.value);
3702
+ if (status.value === "dirty")
3703
+ return DIRTY(result.value);
3704
+ return result;
3501
3705
  });
3502
3706
  }
3503
3707
  else {
3504
- return this._def.schema._parseSync({
3708
+ if (status.value === "aborted")
3709
+ return INVALID;
3710
+ const result = this._def.schema._parseSync({
3505
3711
  data: processed,
3506
3712
  path: ctx.path,
3507
3713
  parent: ctx,
3508
3714
  });
3715
+ if (result.status === "aborted")
3716
+ return INVALID;
3717
+ if (result.status === "dirty")
3718
+ return DIRTY(result.value);
3719
+ if (status.value === "dirty")
3720
+ return DIRTY(result.value);
3721
+ return result;
3509
3722
  }
3510
3723
  }
3511
3724
  if (effect.type === "refinement") {
3512
- const executeRefinement = (acc
3513
- // effect: RefinementEffect<any>
3514
- ) => {
3725
+ const executeRefinement = (acc) => {
3515
3726
  const result = effect.refinement(acc, checkCtx);
3516
3727
  if (ctx.common.async) {
3517
3728
  return Promise.resolve(result);
@@ -3813,10 +4024,18 @@ class ZodPipeline extends ZodType {
3813
4024
  class ZodReadonly extends ZodType {
3814
4025
  _parse(input) {
3815
4026
  const result = this._def.innerType._parse(input);
3816
- if (isValid(result)) {
3817
- result.value = Object.freeze(result.value);
3818
- }
3819
- return result;
4027
+ const freeze = (data) => {
4028
+ if (isValid(data)) {
4029
+ data.value = Object.freeze(data.value);
4030
+ }
4031
+ return data;
4032
+ };
4033
+ return isAsync(result)
4034
+ ? result.then((data) => freeze(data))
4035
+ : freeze(result);
4036
+ }
4037
+ unwrap() {
4038
+ return this._def.innerType;
3820
4039
  }
3821
4040
  }
3822
4041
  ZodReadonly.create = (type, params) => {
@@ -3932,5 +4151,19 @@ const formatErrorPath = (path) => {
3932
4151
  return formatted;
3933
4152
  };
3934
4153
 
3935
- export { errorFormatter, findFiles, formatErrorPath, runProcess };
4154
+ /**
4155
+ * Get the hash of the content string. It returns the first 5 characters of the hash
4156
+ * Example: getContentHash("Hello World")
4157
+ * @param contentString The content string to hash
4158
+ * @returns
4159
+ */
4160
+ const getContentHash = (contentString) => {
4161
+ return crypto
4162
+ .createHash("md5")
4163
+ .update(contentString)
4164
+ .digest("hex")
4165
+ .substring(0, 5);
4166
+ };
4167
+
4168
+ export { errorFormatter, findFiles, formatErrorPath, getContentHash, runProcess };
3936
4169
  //# sourceMappingURL=index.esm.js.map