@embeddable.com/sdk-utils 0.3.3 → 0.4.1
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/errorFormatter.test.d.ts +1 -0
- package/lib/fileContentHash.d.ts +7 -0
- package/lib/fileContentHash.test.d.ts +1 -0
- package/lib/findFiles.test.d.ts +1 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.esm.js +297 -67
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +316 -66
- package/lib/index.js.map +1 -1
- package/package.json +7 -5
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:
|
|
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
|
-
|
|
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
|
|
513
|
-
value
|
|
547
|
+
key,
|
|
548
|
+
value,
|
|
514
549
|
});
|
|
515
550
|
}
|
|
516
551
|
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
@@ -545,12 +580,43 @@ 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 (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");
|
|
600
|
+
return state.get(receiver);
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function __classPrivateFieldSet(receiver, state, value, kind, f) {
|
|
604
|
+
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");
|
|
605
|
+
return (state.set(receiver, value)), value;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
609
|
+
var e = new Error(message);
|
|
610
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
611
|
+
};
|
|
612
|
+
|
|
548
613
|
var errorUtil;
|
|
549
614
|
(function (errorUtil) {
|
|
550
615
|
errorUtil.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
551
616
|
errorUtil.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
|
|
552
617
|
})(errorUtil || (errorUtil = {}));
|
|
553
618
|
|
|
619
|
+
var _ZodEnum_cache, _ZodNativeEnum_cache;
|
|
554
620
|
class ParseInputLazyPath {
|
|
555
621
|
constructor(parent, value, path, key) {
|
|
556
622
|
this._cachedPath = [];
|
|
@@ -601,12 +667,17 @@ function processCreateParams(params) {
|
|
|
601
667
|
if (errorMap)
|
|
602
668
|
return { errorMap: errorMap, description };
|
|
603
669
|
const customMap = (iss, ctx) => {
|
|
604
|
-
|
|
605
|
-
|
|
670
|
+
var _a, _b;
|
|
671
|
+
const { message } = params;
|
|
672
|
+
if (iss.code === "invalid_enum_value") {
|
|
673
|
+
return { message: message !== null && message !== void 0 ? message : ctx.defaultError };
|
|
674
|
+
}
|
|
606
675
|
if (typeof ctx.data === "undefined") {
|
|
607
|
-
return { message:
|
|
676
|
+
return { message: (_a = message !== null && message !== void 0 ? message : required_error) !== null && _a !== void 0 ? _a : ctx.defaultError };
|
|
608
677
|
}
|
|
609
|
-
|
|
678
|
+
if (iss.code !== "invalid_type")
|
|
679
|
+
return { message: ctx.defaultError };
|
|
680
|
+
return { message: (_b = message !== null && message !== void 0 ? message : invalid_type_error) !== null && _b !== void 0 ? _b : ctx.defaultError };
|
|
610
681
|
};
|
|
611
682
|
return { errorMap: customMap, description };
|
|
612
683
|
}
|
|
@@ -864,11 +935,13 @@ class ZodType {
|
|
|
864
935
|
}
|
|
865
936
|
}
|
|
866
937
|
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
867
|
-
const cuid2Regex = /^[
|
|
938
|
+
const cuid2Regex = /^[0-9a-z]+$/;
|
|
868
939
|
const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
869
940
|
// const uuidRegex =
|
|
870
941
|
// /^([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
942
|
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;
|
|
943
|
+
const nanoidRegex = /^[a-z0-9_-]{21}$/i;
|
|
944
|
+
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
945
|
// from https://stackoverflow.com/a/46181/1550155
|
|
873
946
|
// old version: too slow, didn't support unicode
|
|
874
947
|
// 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 +954,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
954
|
// /^[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
955
|
// const emailRegex =
|
|
883
956
|
// /^(?:[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_
|
|
957
|
+
const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
885
958
|
// const emailRegex =
|
|
886
959
|
// /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;
|
|
887
960
|
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
|
|
888
961
|
const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
889
962
|
let emojiRegex;
|
|
890
|
-
|
|
963
|
+
// faster, simpler, safer
|
|
964
|
+
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
965
|
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
|
-
//
|
|
893
|
-
const
|
|
966
|
+
// https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
|
|
967
|
+
const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
968
|
+
// simple
|
|
969
|
+
// const dateRegexSource = `\\d{4}-\\d{2}-\\d{2}`;
|
|
970
|
+
// no leap year validation
|
|
971
|
+
// 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))`;
|
|
972
|
+
// with leap year validation
|
|
973
|
+
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])))`;
|
|
974
|
+
const dateRegex = new RegExp(`^${dateRegexSource}$`);
|
|
975
|
+
function timeRegexSource(args) {
|
|
976
|
+
// let regex = `\\d{2}:\\d{2}:\\d{2}`;
|
|
977
|
+
let regex = `([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d`;
|
|
894
978
|
if (args.precision) {
|
|
895
|
-
|
|
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
|
-
}
|
|
979
|
+
regex = `${regex}\\.\\d{${args.precision}}`;
|
|
901
980
|
}
|
|
902
|
-
else if (args.precision
|
|
903
|
-
|
|
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
|
-
}
|
|
981
|
+
else if (args.precision == null) {
|
|
982
|
+
regex = `${regex}(\\.\\d+)?`;
|
|
909
983
|
}
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
}
|
|
918
|
-
|
|
984
|
+
return regex;
|
|
985
|
+
}
|
|
986
|
+
function timeRegex(args) {
|
|
987
|
+
return new RegExp(`^${timeRegexSource(args)}$`);
|
|
988
|
+
}
|
|
989
|
+
// Adapted from https://stackoverflow.com/a/3143231
|
|
990
|
+
function datetimeRegex(args) {
|
|
991
|
+
let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
|
|
992
|
+
const opts = [];
|
|
993
|
+
opts.push(args.local ? `Z?` : `Z`);
|
|
994
|
+
if (args.offset)
|
|
995
|
+
opts.push(`([+-]\\d{2}:?\\d{2})`);
|
|
996
|
+
regex = `${regex}(${opts.join("|")})`;
|
|
997
|
+
return new RegExp(`^${regex}$`);
|
|
998
|
+
}
|
|
919
999
|
function isValidIP(ip, version) {
|
|
920
1000
|
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
|
|
921
1001
|
return true;
|
|
@@ -937,9 +1017,7 @@ class ZodString extends ZodType {
|
|
|
937
1017
|
code: ZodIssueCode.invalid_type,
|
|
938
1018
|
expected: ZodParsedType.string,
|
|
939
1019
|
received: ctx.parsedType,
|
|
940
|
-
}
|
|
941
|
-
//
|
|
942
|
-
);
|
|
1020
|
+
});
|
|
943
1021
|
return INVALID;
|
|
944
1022
|
}
|
|
945
1023
|
const status = new ParseStatus();
|
|
@@ -1037,6 +1115,17 @@ class ZodString extends ZodType {
|
|
|
1037
1115
|
status.dirty();
|
|
1038
1116
|
}
|
|
1039
1117
|
}
|
|
1118
|
+
else if (check.kind === "nanoid") {
|
|
1119
|
+
if (!nanoidRegex.test(input.data)) {
|
|
1120
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1121
|
+
addIssueToContext(ctx, {
|
|
1122
|
+
validation: "nanoid",
|
|
1123
|
+
code: ZodIssueCode.invalid_string,
|
|
1124
|
+
message: check.message,
|
|
1125
|
+
});
|
|
1126
|
+
status.dirty();
|
|
1127
|
+
}
|
|
1128
|
+
}
|
|
1040
1129
|
else if (check.kind === "cuid") {
|
|
1041
1130
|
if (!cuidRegex.test(input.data)) {
|
|
1042
1131
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
@@ -1151,6 +1240,41 @@ class ZodString extends ZodType {
|
|
|
1151
1240
|
status.dirty();
|
|
1152
1241
|
}
|
|
1153
1242
|
}
|
|
1243
|
+
else if (check.kind === "date") {
|
|
1244
|
+
const regex = dateRegex;
|
|
1245
|
+
if (!regex.test(input.data)) {
|
|
1246
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1247
|
+
addIssueToContext(ctx, {
|
|
1248
|
+
code: ZodIssueCode.invalid_string,
|
|
1249
|
+
validation: "date",
|
|
1250
|
+
message: check.message,
|
|
1251
|
+
});
|
|
1252
|
+
status.dirty();
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
else if (check.kind === "time") {
|
|
1256
|
+
const regex = timeRegex(check);
|
|
1257
|
+
if (!regex.test(input.data)) {
|
|
1258
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1259
|
+
addIssueToContext(ctx, {
|
|
1260
|
+
code: ZodIssueCode.invalid_string,
|
|
1261
|
+
validation: "time",
|
|
1262
|
+
message: check.message,
|
|
1263
|
+
});
|
|
1264
|
+
status.dirty();
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
else if (check.kind === "duration") {
|
|
1268
|
+
if (!durationRegex.test(input.data)) {
|
|
1269
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1270
|
+
addIssueToContext(ctx, {
|
|
1271
|
+
validation: "duration",
|
|
1272
|
+
code: ZodIssueCode.invalid_string,
|
|
1273
|
+
message: check.message,
|
|
1274
|
+
});
|
|
1275
|
+
status.dirty();
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1154
1278
|
else if (check.kind === "ip") {
|
|
1155
1279
|
if (!isValidIP(input.data, check.version)) {
|
|
1156
1280
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
@@ -1162,6 +1286,17 @@ class ZodString extends ZodType {
|
|
|
1162
1286
|
status.dirty();
|
|
1163
1287
|
}
|
|
1164
1288
|
}
|
|
1289
|
+
else if (check.kind === "base64") {
|
|
1290
|
+
if (!base64Regex.test(input.data)) {
|
|
1291
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1292
|
+
addIssueToContext(ctx, {
|
|
1293
|
+
validation: "base64",
|
|
1294
|
+
code: ZodIssueCode.invalid_string,
|
|
1295
|
+
message: check.message,
|
|
1296
|
+
});
|
|
1297
|
+
status.dirty();
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1165
1300
|
else {
|
|
1166
1301
|
util.assertNever(check);
|
|
1167
1302
|
}
|
|
@@ -1193,6 +1328,9 @@ class ZodString extends ZodType {
|
|
|
1193
1328
|
uuid(message) {
|
|
1194
1329
|
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
1195
1330
|
}
|
|
1331
|
+
nanoid(message) {
|
|
1332
|
+
return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
|
|
1333
|
+
}
|
|
1196
1334
|
cuid(message) {
|
|
1197
1335
|
return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
|
|
1198
1336
|
}
|
|
@@ -1202,16 +1340,20 @@ class ZodString extends ZodType {
|
|
|
1202
1340
|
ulid(message) {
|
|
1203
1341
|
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
|
|
1204
1342
|
}
|
|
1343
|
+
base64(message) {
|
|
1344
|
+
return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
|
|
1345
|
+
}
|
|
1205
1346
|
ip(options) {
|
|
1206
1347
|
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
|
|
1207
1348
|
}
|
|
1208
1349
|
datetime(options) {
|
|
1209
|
-
var _a;
|
|
1350
|
+
var _a, _b;
|
|
1210
1351
|
if (typeof options === "string") {
|
|
1211
1352
|
return this._addCheck({
|
|
1212
1353
|
kind: "datetime",
|
|
1213
1354
|
precision: null,
|
|
1214
1355
|
offset: false,
|
|
1356
|
+
local: false,
|
|
1215
1357
|
message: options,
|
|
1216
1358
|
});
|
|
1217
1359
|
}
|
|
@@ -1219,9 +1361,30 @@ class ZodString extends ZodType {
|
|
|
1219
1361
|
kind: "datetime",
|
|
1220
1362
|
precision: typeof (options === null || options === void 0 ? void 0 : options.precision) === "undefined" ? null : options === null || options === void 0 ? void 0 : options.precision,
|
|
1221
1363
|
offset: (_a = options === null || options === void 0 ? void 0 : options.offset) !== null && _a !== void 0 ? _a : false,
|
|
1364
|
+
local: (_b = options === null || options === void 0 ? void 0 : options.local) !== null && _b !== void 0 ? _b : false,
|
|
1222
1365
|
...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
|
|
1223
1366
|
});
|
|
1224
1367
|
}
|
|
1368
|
+
date(message) {
|
|
1369
|
+
return this._addCheck({ kind: "date", message });
|
|
1370
|
+
}
|
|
1371
|
+
time(options) {
|
|
1372
|
+
if (typeof options === "string") {
|
|
1373
|
+
return this._addCheck({
|
|
1374
|
+
kind: "time",
|
|
1375
|
+
precision: null,
|
|
1376
|
+
message: options,
|
|
1377
|
+
});
|
|
1378
|
+
}
|
|
1379
|
+
return this._addCheck({
|
|
1380
|
+
kind: "time",
|
|
1381
|
+
precision: typeof (options === null || options === void 0 ? void 0 : options.precision) === "undefined" ? null : options === null || options === void 0 ? void 0 : options.precision,
|
|
1382
|
+
...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
|
|
1383
|
+
});
|
|
1384
|
+
}
|
|
1385
|
+
duration(message) {
|
|
1386
|
+
return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
|
|
1387
|
+
}
|
|
1225
1388
|
regex(regex, message) {
|
|
1226
1389
|
return this._addCheck({
|
|
1227
1390
|
kind: "regex",
|
|
@@ -1300,6 +1463,15 @@ class ZodString extends ZodType {
|
|
|
1300
1463
|
get isDatetime() {
|
|
1301
1464
|
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
1302
1465
|
}
|
|
1466
|
+
get isDate() {
|
|
1467
|
+
return !!this._def.checks.find((ch) => ch.kind === "date");
|
|
1468
|
+
}
|
|
1469
|
+
get isTime() {
|
|
1470
|
+
return !!this._def.checks.find((ch) => ch.kind === "time");
|
|
1471
|
+
}
|
|
1472
|
+
get isDuration() {
|
|
1473
|
+
return !!this._def.checks.find((ch) => ch.kind === "duration");
|
|
1474
|
+
}
|
|
1303
1475
|
get isEmail() {
|
|
1304
1476
|
return !!this._def.checks.find((ch) => ch.kind === "email");
|
|
1305
1477
|
}
|
|
@@ -1312,6 +1484,9 @@ class ZodString extends ZodType {
|
|
|
1312
1484
|
get isUUID() {
|
|
1313
1485
|
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
1314
1486
|
}
|
|
1487
|
+
get isNANOID() {
|
|
1488
|
+
return !!this._def.checks.find((ch) => ch.kind === "nanoid");
|
|
1489
|
+
}
|
|
1315
1490
|
get isCUID() {
|
|
1316
1491
|
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
1317
1492
|
}
|
|
@@ -1324,6 +1499,9 @@ class ZodString extends ZodType {
|
|
|
1324
1499
|
get isIP() {
|
|
1325
1500
|
return !!this._def.checks.find((ch) => ch.kind === "ip");
|
|
1326
1501
|
}
|
|
1502
|
+
get isBase64() {
|
|
1503
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64");
|
|
1504
|
+
}
|
|
1327
1505
|
get minLength() {
|
|
1328
1506
|
let min = null;
|
|
1329
1507
|
for (const ch of this._def.checks) {
|
|
@@ -2311,9 +2489,10 @@ class ZodObject extends ZodType {
|
|
|
2311
2489
|
const syncPairs = [];
|
|
2312
2490
|
for (const pair of pairs) {
|
|
2313
2491
|
const key = await pair.key;
|
|
2492
|
+
const value = await pair.value;
|
|
2314
2493
|
syncPairs.push({
|
|
2315
2494
|
key,
|
|
2316
|
-
value
|
|
2495
|
+
value,
|
|
2317
2496
|
alwaysSet: pair.alwaysSet,
|
|
2318
2497
|
});
|
|
2319
2498
|
}
|
|
@@ -2687,7 +2866,7 @@ const getDiscriminator = (type) => {
|
|
|
2687
2866
|
}
|
|
2688
2867
|
else if (type instanceof ZodNativeEnum) {
|
|
2689
2868
|
// eslint-disable-next-line ban/ban
|
|
2690
|
-
return
|
|
2869
|
+
return util.objectValues(type.enum);
|
|
2691
2870
|
}
|
|
2692
2871
|
else if (type instanceof ZodDefault) {
|
|
2693
2872
|
return getDiscriminator(type._def.innerType);
|
|
@@ -2698,8 +2877,23 @@ const getDiscriminator = (type) => {
|
|
|
2698
2877
|
else if (type instanceof ZodNull) {
|
|
2699
2878
|
return [null];
|
|
2700
2879
|
}
|
|
2880
|
+
else if (type instanceof ZodOptional) {
|
|
2881
|
+
return [undefined, ...getDiscriminator(type.unwrap())];
|
|
2882
|
+
}
|
|
2883
|
+
else if (type instanceof ZodNullable) {
|
|
2884
|
+
return [null, ...getDiscriminator(type.unwrap())];
|
|
2885
|
+
}
|
|
2886
|
+
else if (type instanceof ZodBranded) {
|
|
2887
|
+
return getDiscriminator(type.unwrap());
|
|
2888
|
+
}
|
|
2889
|
+
else if (type instanceof ZodReadonly) {
|
|
2890
|
+
return getDiscriminator(type.unwrap());
|
|
2891
|
+
}
|
|
2892
|
+
else if (type instanceof ZodCatch) {
|
|
2893
|
+
return getDiscriminator(type._def.innerType);
|
|
2894
|
+
}
|
|
2701
2895
|
else {
|
|
2702
|
-
return
|
|
2896
|
+
return [];
|
|
2703
2897
|
}
|
|
2704
2898
|
};
|
|
2705
2899
|
class ZodDiscriminatedUnion extends ZodType {
|
|
@@ -2762,7 +2956,7 @@ class ZodDiscriminatedUnion extends ZodType {
|
|
|
2762
2956
|
// try {
|
|
2763
2957
|
for (const type of options) {
|
|
2764
2958
|
const discriminatorValues = getDiscriminator(type.shape[discriminator]);
|
|
2765
|
-
if (!discriminatorValues) {
|
|
2959
|
+
if (!discriminatorValues.length) {
|
|
2766
2960
|
throw new Error(`A discriminator value for key \`${discriminator}\` could not be extracted from all schema options`);
|
|
2767
2961
|
}
|
|
2768
2962
|
for (const value of discriminatorValues) {
|
|
@@ -2975,6 +3169,7 @@ class ZodRecord extends ZodType {
|
|
|
2975
3169
|
pairs.push({
|
|
2976
3170
|
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
|
|
2977
3171
|
value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
|
|
3172
|
+
alwaysSet: key in ctx.data,
|
|
2978
3173
|
});
|
|
2979
3174
|
}
|
|
2980
3175
|
if (ctx.common.async) {
|
|
@@ -3334,6 +3529,10 @@ function createZodEnum(values, params) {
|
|
|
3334
3529
|
});
|
|
3335
3530
|
}
|
|
3336
3531
|
class ZodEnum extends ZodType {
|
|
3532
|
+
constructor() {
|
|
3533
|
+
super(...arguments);
|
|
3534
|
+
_ZodEnum_cache.set(this, void 0);
|
|
3535
|
+
}
|
|
3337
3536
|
_parse(input) {
|
|
3338
3537
|
if (typeof input.data !== "string") {
|
|
3339
3538
|
const ctx = this._getOrReturnCtx(input);
|
|
@@ -3345,7 +3544,10 @@ class ZodEnum extends ZodType {
|
|
|
3345
3544
|
});
|
|
3346
3545
|
return INVALID;
|
|
3347
3546
|
}
|
|
3348
|
-
if (this
|
|
3547
|
+
if (!__classPrivateFieldGet(this, _ZodEnum_cache)) {
|
|
3548
|
+
__classPrivateFieldSet(this, _ZodEnum_cache, new Set(this._def.values));
|
|
3549
|
+
}
|
|
3550
|
+
if (!__classPrivateFieldGet(this, _ZodEnum_cache).has(input.data)) {
|
|
3349
3551
|
const ctx = this._getOrReturnCtx(input);
|
|
3350
3552
|
const expectedValues = this._def.values;
|
|
3351
3553
|
addIssueToContext(ctx, {
|
|
@@ -3381,15 +3583,26 @@ class ZodEnum extends ZodType {
|
|
|
3381
3583
|
}
|
|
3382
3584
|
return enumValues;
|
|
3383
3585
|
}
|
|
3384
|
-
extract(values) {
|
|
3385
|
-
return ZodEnum.create(values
|
|
3586
|
+
extract(values, newDef = this._def) {
|
|
3587
|
+
return ZodEnum.create(values, {
|
|
3588
|
+
...this._def,
|
|
3589
|
+
...newDef,
|
|
3590
|
+
});
|
|
3386
3591
|
}
|
|
3387
|
-
exclude(values) {
|
|
3388
|
-
return ZodEnum.create(this.options.filter((opt) => !values.includes(opt))
|
|
3592
|
+
exclude(values, newDef = this._def) {
|
|
3593
|
+
return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
|
|
3594
|
+
...this._def,
|
|
3595
|
+
...newDef,
|
|
3596
|
+
});
|
|
3389
3597
|
}
|
|
3390
3598
|
}
|
|
3599
|
+
_ZodEnum_cache = new WeakMap();
|
|
3391
3600
|
ZodEnum.create = createZodEnum;
|
|
3392
3601
|
class ZodNativeEnum extends ZodType {
|
|
3602
|
+
constructor() {
|
|
3603
|
+
super(...arguments);
|
|
3604
|
+
_ZodNativeEnum_cache.set(this, void 0);
|
|
3605
|
+
}
|
|
3393
3606
|
_parse(input) {
|
|
3394
3607
|
const nativeEnumValues = util.getValidEnumValues(this._def.values);
|
|
3395
3608
|
const ctx = this._getOrReturnCtx(input);
|
|
@@ -3403,7 +3616,10 @@ class ZodNativeEnum extends ZodType {
|
|
|
3403
3616
|
});
|
|
3404
3617
|
return INVALID;
|
|
3405
3618
|
}
|
|
3406
|
-
if (
|
|
3619
|
+
if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache)) {
|
|
3620
|
+
__classPrivateFieldSet(this, _ZodNativeEnum_cache, new Set(util.getValidEnumValues(this._def.values)));
|
|
3621
|
+
}
|
|
3622
|
+
if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache).has(input.data)) {
|
|
3407
3623
|
const expectedValues = util.objectValues(nativeEnumValues);
|
|
3408
3624
|
addIssueToContext(ctx, {
|
|
3409
3625
|
received: ctx.data,
|
|
@@ -3418,6 +3634,7 @@ class ZodNativeEnum extends ZodType {
|
|
|
3418
3634
|
return this._def.values;
|
|
3419
3635
|
}
|
|
3420
3636
|
}
|
|
3637
|
+
_ZodNativeEnum_cache = new WeakMap();
|
|
3421
3638
|
ZodNativeEnum.create = (values, params) => {
|
|
3422
3639
|
return new ZodNativeEnum({
|
|
3423
3640
|
values: values,
|
|
@@ -3487,33 +3704,43 @@ class ZodEffects extends ZodType {
|
|
|
3487
3704
|
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
3488
3705
|
if (effect.type === "preprocess") {
|
|
3489
3706
|
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
3707
|
if (ctx.common.async) {
|
|
3497
|
-
return Promise.resolve(processed).then((processed) => {
|
|
3498
|
-
|
|
3708
|
+
return Promise.resolve(processed).then(async (processed) => {
|
|
3709
|
+
if (status.value === "aborted")
|
|
3710
|
+
return INVALID;
|
|
3711
|
+
const result = await this._def.schema._parseAsync({
|
|
3499
3712
|
data: processed,
|
|
3500
3713
|
path: ctx.path,
|
|
3501
3714
|
parent: ctx,
|
|
3502
3715
|
});
|
|
3716
|
+
if (result.status === "aborted")
|
|
3717
|
+
return INVALID;
|
|
3718
|
+
if (result.status === "dirty")
|
|
3719
|
+
return DIRTY(result.value);
|
|
3720
|
+
if (status.value === "dirty")
|
|
3721
|
+
return DIRTY(result.value);
|
|
3722
|
+
return result;
|
|
3503
3723
|
});
|
|
3504
3724
|
}
|
|
3505
3725
|
else {
|
|
3506
|
-
|
|
3726
|
+
if (status.value === "aborted")
|
|
3727
|
+
return INVALID;
|
|
3728
|
+
const result = this._def.schema._parseSync({
|
|
3507
3729
|
data: processed,
|
|
3508
3730
|
path: ctx.path,
|
|
3509
3731
|
parent: ctx,
|
|
3510
3732
|
});
|
|
3733
|
+
if (result.status === "aborted")
|
|
3734
|
+
return INVALID;
|
|
3735
|
+
if (result.status === "dirty")
|
|
3736
|
+
return DIRTY(result.value);
|
|
3737
|
+
if (status.value === "dirty")
|
|
3738
|
+
return DIRTY(result.value);
|
|
3739
|
+
return result;
|
|
3511
3740
|
}
|
|
3512
3741
|
}
|
|
3513
3742
|
if (effect.type === "refinement") {
|
|
3514
|
-
const executeRefinement = (acc
|
|
3515
|
-
// effect: RefinementEffect<any>
|
|
3516
|
-
) => {
|
|
3743
|
+
const executeRefinement = (acc) => {
|
|
3517
3744
|
const result = effect.refinement(acc, checkCtx);
|
|
3518
3745
|
if (ctx.common.async) {
|
|
3519
3746
|
return Promise.resolve(result);
|
|
@@ -3815,10 +4042,18 @@ class ZodPipeline extends ZodType {
|
|
|
3815
4042
|
class ZodReadonly extends ZodType {
|
|
3816
4043
|
_parse(input) {
|
|
3817
4044
|
const result = this._def.innerType._parse(input);
|
|
3818
|
-
|
|
3819
|
-
|
|
3820
|
-
|
|
3821
|
-
|
|
4045
|
+
const freeze = (data) => {
|
|
4046
|
+
if (isValid(data)) {
|
|
4047
|
+
data.value = Object.freeze(data.value);
|
|
4048
|
+
}
|
|
4049
|
+
return data;
|
|
4050
|
+
};
|
|
4051
|
+
return isAsync(result)
|
|
4052
|
+
? result.then((data) => freeze(data))
|
|
4053
|
+
: freeze(result);
|
|
4054
|
+
}
|
|
4055
|
+
unwrap() {
|
|
4056
|
+
return this._def.innerType;
|
|
3822
4057
|
}
|
|
3823
4058
|
}
|
|
3824
4059
|
ZodReadonly.create = (type, params) => {
|
|
@@ -3934,8 +4169,23 @@ const formatErrorPath = (path) => {
|
|
|
3934
4169
|
return formatted;
|
|
3935
4170
|
};
|
|
3936
4171
|
|
|
4172
|
+
/**
|
|
4173
|
+
* Get the hash of the content string. It returns the first 5 characters of the hash
|
|
4174
|
+
* Example: getContentHash("Hello World")
|
|
4175
|
+
* @param contentString The content string to hash
|
|
4176
|
+
* @returns
|
|
4177
|
+
*/
|
|
4178
|
+
const getContentHash = (contentString) => {
|
|
4179
|
+
return crypto__namespace
|
|
4180
|
+
.createHash("md5")
|
|
4181
|
+
.update(contentString)
|
|
4182
|
+
.digest("hex")
|
|
4183
|
+
.substring(0, 5);
|
|
4184
|
+
};
|
|
4185
|
+
|
|
3937
4186
|
exports.errorFormatter = errorFormatter;
|
|
3938
4187
|
exports.findFiles = findFiles;
|
|
3939
4188
|
exports.formatErrorPath = formatErrorPath;
|
|
4189
|
+
exports.getContentHash = getContentHash;
|
|
3940
4190
|
exports.runProcess = runProcess;
|
|
3941
4191
|
//# sourceMappingURL=index.js.map
|