@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
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -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;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/index.d.ts
CHANGED
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:
|
|
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
|
-
|
|
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
|
|
511
|
-
value
|
|
526
|
+
key,
|
|
527
|
+
value,
|
|
512
528
|
});
|
|
513
529
|
}
|
|
514
530
|
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
@@ -543,12 +559,43 @@ 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 (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");
|
|
579
|
+
return state.get(receiver);
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
function __classPrivateFieldSet(receiver, state, value, kind, f) {
|
|
583
|
+
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");
|
|
584
|
+
return (state.set(receiver, value)), value;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
588
|
+
var e = new Error(message);
|
|
589
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
590
|
+
};
|
|
591
|
+
|
|
546
592
|
var errorUtil;
|
|
547
593
|
(function (errorUtil) {
|
|
548
594
|
errorUtil.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
549
595
|
errorUtil.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
|
|
550
596
|
})(errorUtil || (errorUtil = {}));
|
|
551
597
|
|
|
598
|
+
var _ZodEnum_cache, _ZodNativeEnum_cache;
|
|
552
599
|
class ParseInputLazyPath {
|
|
553
600
|
constructor(parent, value, path, key) {
|
|
554
601
|
this._cachedPath = [];
|
|
@@ -599,12 +646,17 @@ function processCreateParams(params) {
|
|
|
599
646
|
if (errorMap)
|
|
600
647
|
return { errorMap: errorMap, description };
|
|
601
648
|
const customMap = (iss, ctx) => {
|
|
602
|
-
|
|
603
|
-
|
|
649
|
+
var _a, _b;
|
|
650
|
+
const { message } = params;
|
|
651
|
+
if (iss.code === "invalid_enum_value") {
|
|
652
|
+
return { message: message !== null && message !== void 0 ? message : ctx.defaultError };
|
|
653
|
+
}
|
|
604
654
|
if (typeof ctx.data === "undefined") {
|
|
605
|
-
return { message:
|
|
655
|
+
return { message: (_a = message !== null && message !== void 0 ? message : required_error) !== null && _a !== void 0 ? _a : ctx.defaultError };
|
|
606
656
|
}
|
|
607
|
-
|
|
657
|
+
if (iss.code !== "invalid_type")
|
|
658
|
+
return { message: ctx.defaultError };
|
|
659
|
+
return { message: (_b = message !== null && message !== void 0 ? message : invalid_type_error) !== null && _b !== void 0 ? _b : ctx.defaultError };
|
|
608
660
|
};
|
|
609
661
|
return { errorMap: customMap, description };
|
|
610
662
|
}
|
|
@@ -862,11 +914,13 @@ class ZodType {
|
|
|
862
914
|
}
|
|
863
915
|
}
|
|
864
916
|
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
865
|
-
const cuid2Regex = /^[
|
|
917
|
+
const cuid2Regex = /^[0-9a-z]+$/;
|
|
866
918
|
const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
867
919
|
// const uuidRegex =
|
|
868
920
|
// /^([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
921
|
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;
|
|
922
|
+
const nanoidRegex = /^[a-z0-9_-]{21}$/i;
|
|
923
|
+
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
924
|
// from https://stackoverflow.com/a/46181/1550155
|
|
871
925
|
// old version: too slow, didn't support unicode
|
|
872
926
|
// 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 +933,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
933
|
// /^[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
934
|
// const emailRegex =
|
|
881
935
|
// /^(?:[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_
|
|
936
|
+
const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
883
937
|
// const emailRegex =
|
|
884
938
|
// /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;
|
|
885
939
|
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
|
|
886
940
|
const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
887
941
|
let emojiRegex;
|
|
888
|
-
|
|
942
|
+
// faster, simpler, safer
|
|
943
|
+
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
944
|
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
|
-
//
|
|
891
|
-
const
|
|
945
|
+
// https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
|
|
946
|
+
const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
947
|
+
// simple
|
|
948
|
+
// const dateRegexSource = `\\d{4}-\\d{2}-\\d{2}`;
|
|
949
|
+
// no leap year validation
|
|
950
|
+
// 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))`;
|
|
951
|
+
// with leap year validation
|
|
952
|
+
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])))`;
|
|
953
|
+
const dateRegex = new RegExp(`^${dateRegexSource}$`);
|
|
954
|
+
function timeRegexSource(args) {
|
|
955
|
+
// let regex = `\\d{2}:\\d{2}:\\d{2}`;
|
|
956
|
+
let regex = `([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d`;
|
|
892
957
|
if (args.precision) {
|
|
893
|
-
|
|
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
|
-
}
|
|
958
|
+
regex = `${regex}\\.\\d{${args.precision}}`;
|
|
899
959
|
}
|
|
900
|
-
else if (args.precision
|
|
901
|
-
|
|
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
|
-
}
|
|
960
|
+
else if (args.precision == null) {
|
|
961
|
+
regex = `${regex}(\\.\\d+)?`;
|
|
915
962
|
}
|
|
916
|
-
|
|
963
|
+
return regex;
|
|
964
|
+
}
|
|
965
|
+
function timeRegex(args) {
|
|
966
|
+
return new RegExp(`^${timeRegexSource(args)}$`);
|
|
967
|
+
}
|
|
968
|
+
// Adapted from https://stackoverflow.com/a/3143231
|
|
969
|
+
function datetimeRegex(args) {
|
|
970
|
+
let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
|
|
971
|
+
const opts = [];
|
|
972
|
+
opts.push(args.local ? `Z?` : `Z`);
|
|
973
|
+
if (args.offset)
|
|
974
|
+
opts.push(`([+-]\\d{2}:?\\d{2})`);
|
|
975
|
+
regex = `${regex}(${opts.join("|")})`;
|
|
976
|
+
return new RegExp(`^${regex}$`);
|
|
977
|
+
}
|
|
917
978
|
function isValidIP(ip, version) {
|
|
918
979
|
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
|
|
919
980
|
return true;
|
|
@@ -935,9 +996,7 @@ class ZodString extends ZodType {
|
|
|
935
996
|
code: ZodIssueCode.invalid_type,
|
|
936
997
|
expected: ZodParsedType.string,
|
|
937
998
|
received: ctx.parsedType,
|
|
938
|
-
}
|
|
939
|
-
//
|
|
940
|
-
);
|
|
999
|
+
});
|
|
941
1000
|
return INVALID;
|
|
942
1001
|
}
|
|
943
1002
|
const status = new ParseStatus();
|
|
@@ -1035,6 +1094,17 @@ class ZodString extends ZodType {
|
|
|
1035
1094
|
status.dirty();
|
|
1036
1095
|
}
|
|
1037
1096
|
}
|
|
1097
|
+
else if (check.kind === "nanoid") {
|
|
1098
|
+
if (!nanoidRegex.test(input.data)) {
|
|
1099
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1100
|
+
addIssueToContext(ctx, {
|
|
1101
|
+
validation: "nanoid",
|
|
1102
|
+
code: ZodIssueCode.invalid_string,
|
|
1103
|
+
message: check.message,
|
|
1104
|
+
});
|
|
1105
|
+
status.dirty();
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1038
1108
|
else if (check.kind === "cuid") {
|
|
1039
1109
|
if (!cuidRegex.test(input.data)) {
|
|
1040
1110
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
@@ -1149,6 +1219,41 @@ class ZodString extends ZodType {
|
|
|
1149
1219
|
status.dirty();
|
|
1150
1220
|
}
|
|
1151
1221
|
}
|
|
1222
|
+
else if (check.kind === "date") {
|
|
1223
|
+
const regex = dateRegex;
|
|
1224
|
+
if (!regex.test(input.data)) {
|
|
1225
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1226
|
+
addIssueToContext(ctx, {
|
|
1227
|
+
code: ZodIssueCode.invalid_string,
|
|
1228
|
+
validation: "date",
|
|
1229
|
+
message: check.message,
|
|
1230
|
+
});
|
|
1231
|
+
status.dirty();
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
else if (check.kind === "time") {
|
|
1235
|
+
const regex = timeRegex(check);
|
|
1236
|
+
if (!regex.test(input.data)) {
|
|
1237
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1238
|
+
addIssueToContext(ctx, {
|
|
1239
|
+
code: ZodIssueCode.invalid_string,
|
|
1240
|
+
validation: "time",
|
|
1241
|
+
message: check.message,
|
|
1242
|
+
});
|
|
1243
|
+
status.dirty();
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
else if (check.kind === "duration") {
|
|
1247
|
+
if (!durationRegex.test(input.data)) {
|
|
1248
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1249
|
+
addIssueToContext(ctx, {
|
|
1250
|
+
validation: "duration",
|
|
1251
|
+
code: ZodIssueCode.invalid_string,
|
|
1252
|
+
message: check.message,
|
|
1253
|
+
});
|
|
1254
|
+
status.dirty();
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1152
1257
|
else if (check.kind === "ip") {
|
|
1153
1258
|
if (!isValidIP(input.data, check.version)) {
|
|
1154
1259
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
@@ -1160,6 +1265,17 @@ class ZodString extends ZodType {
|
|
|
1160
1265
|
status.dirty();
|
|
1161
1266
|
}
|
|
1162
1267
|
}
|
|
1268
|
+
else if (check.kind === "base64") {
|
|
1269
|
+
if (!base64Regex.test(input.data)) {
|
|
1270
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1271
|
+
addIssueToContext(ctx, {
|
|
1272
|
+
validation: "base64",
|
|
1273
|
+
code: ZodIssueCode.invalid_string,
|
|
1274
|
+
message: check.message,
|
|
1275
|
+
});
|
|
1276
|
+
status.dirty();
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1163
1279
|
else {
|
|
1164
1280
|
util.assertNever(check);
|
|
1165
1281
|
}
|
|
@@ -1191,6 +1307,9 @@ class ZodString extends ZodType {
|
|
|
1191
1307
|
uuid(message) {
|
|
1192
1308
|
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
1193
1309
|
}
|
|
1310
|
+
nanoid(message) {
|
|
1311
|
+
return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
|
|
1312
|
+
}
|
|
1194
1313
|
cuid(message) {
|
|
1195
1314
|
return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
|
|
1196
1315
|
}
|
|
@@ -1200,16 +1319,20 @@ class ZodString extends ZodType {
|
|
|
1200
1319
|
ulid(message) {
|
|
1201
1320
|
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
|
|
1202
1321
|
}
|
|
1322
|
+
base64(message) {
|
|
1323
|
+
return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
|
|
1324
|
+
}
|
|
1203
1325
|
ip(options) {
|
|
1204
1326
|
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
|
|
1205
1327
|
}
|
|
1206
1328
|
datetime(options) {
|
|
1207
|
-
var _a;
|
|
1329
|
+
var _a, _b;
|
|
1208
1330
|
if (typeof options === "string") {
|
|
1209
1331
|
return this._addCheck({
|
|
1210
1332
|
kind: "datetime",
|
|
1211
1333
|
precision: null,
|
|
1212
1334
|
offset: false,
|
|
1335
|
+
local: false,
|
|
1213
1336
|
message: options,
|
|
1214
1337
|
});
|
|
1215
1338
|
}
|
|
@@ -1217,9 +1340,30 @@ class ZodString extends ZodType {
|
|
|
1217
1340
|
kind: "datetime",
|
|
1218
1341
|
precision: typeof (options === null || options === void 0 ? void 0 : options.precision) === "undefined" ? null : options === null || options === void 0 ? void 0 : options.precision,
|
|
1219
1342
|
offset: (_a = options === null || options === void 0 ? void 0 : options.offset) !== null && _a !== void 0 ? _a : false,
|
|
1343
|
+
local: (_b = options === null || options === void 0 ? void 0 : options.local) !== null && _b !== void 0 ? _b : false,
|
|
1220
1344
|
...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
|
|
1221
1345
|
});
|
|
1222
1346
|
}
|
|
1347
|
+
date(message) {
|
|
1348
|
+
return this._addCheck({ kind: "date", message });
|
|
1349
|
+
}
|
|
1350
|
+
time(options) {
|
|
1351
|
+
if (typeof options === "string") {
|
|
1352
|
+
return this._addCheck({
|
|
1353
|
+
kind: "time",
|
|
1354
|
+
precision: null,
|
|
1355
|
+
message: options,
|
|
1356
|
+
});
|
|
1357
|
+
}
|
|
1358
|
+
return this._addCheck({
|
|
1359
|
+
kind: "time",
|
|
1360
|
+
precision: typeof (options === null || options === void 0 ? void 0 : options.precision) === "undefined" ? null : options === null || options === void 0 ? void 0 : options.precision,
|
|
1361
|
+
...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
|
|
1362
|
+
});
|
|
1363
|
+
}
|
|
1364
|
+
duration(message) {
|
|
1365
|
+
return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
|
|
1366
|
+
}
|
|
1223
1367
|
regex(regex, message) {
|
|
1224
1368
|
return this._addCheck({
|
|
1225
1369
|
kind: "regex",
|
|
@@ -1298,6 +1442,15 @@ class ZodString extends ZodType {
|
|
|
1298
1442
|
get isDatetime() {
|
|
1299
1443
|
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
1300
1444
|
}
|
|
1445
|
+
get isDate() {
|
|
1446
|
+
return !!this._def.checks.find((ch) => ch.kind === "date");
|
|
1447
|
+
}
|
|
1448
|
+
get isTime() {
|
|
1449
|
+
return !!this._def.checks.find((ch) => ch.kind === "time");
|
|
1450
|
+
}
|
|
1451
|
+
get isDuration() {
|
|
1452
|
+
return !!this._def.checks.find((ch) => ch.kind === "duration");
|
|
1453
|
+
}
|
|
1301
1454
|
get isEmail() {
|
|
1302
1455
|
return !!this._def.checks.find((ch) => ch.kind === "email");
|
|
1303
1456
|
}
|
|
@@ -1310,6 +1463,9 @@ class ZodString extends ZodType {
|
|
|
1310
1463
|
get isUUID() {
|
|
1311
1464
|
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
1312
1465
|
}
|
|
1466
|
+
get isNANOID() {
|
|
1467
|
+
return !!this._def.checks.find((ch) => ch.kind === "nanoid");
|
|
1468
|
+
}
|
|
1313
1469
|
get isCUID() {
|
|
1314
1470
|
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
1315
1471
|
}
|
|
@@ -1322,6 +1478,9 @@ class ZodString extends ZodType {
|
|
|
1322
1478
|
get isIP() {
|
|
1323
1479
|
return !!this._def.checks.find((ch) => ch.kind === "ip");
|
|
1324
1480
|
}
|
|
1481
|
+
get isBase64() {
|
|
1482
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64");
|
|
1483
|
+
}
|
|
1325
1484
|
get minLength() {
|
|
1326
1485
|
let min = null;
|
|
1327
1486
|
for (const ch of this._def.checks) {
|
|
@@ -2309,9 +2468,10 @@ class ZodObject extends ZodType {
|
|
|
2309
2468
|
const syncPairs = [];
|
|
2310
2469
|
for (const pair of pairs) {
|
|
2311
2470
|
const key = await pair.key;
|
|
2471
|
+
const value = await pair.value;
|
|
2312
2472
|
syncPairs.push({
|
|
2313
2473
|
key,
|
|
2314
|
-
value
|
|
2474
|
+
value,
|
|
2315
2475
|
alwaysSet: pair.alwaysSet,
|
|
2316
2476
|
});
|
|
2317
2477
|
}
|
|
@@ -2685,7 +2845,7 @@ const getDiscriminator = (type) => {
|
|
|
2685
2845
|
}
|
|
2686
2846
|
else if (type instanceof ZodNativeEnum) {
|
|
2687
2847
|
// eslint-disable-next-line ban/ban
|
|
2688
|
-
return
|
|
2848
|
+
return util.objectValues(type.enum);
|
|
2689
2849
|
}
|
|
2690
2850
|
else if (type instanceof ZodDefault) {
|
|
2691
2851
|
return getDiscriminator(type._def.innerType);
|
|
@@ -2696,8 +2856,23 @@ const getDiscriminator = (type) => {
|
|
|
2696
2856
|
else if (type instanceof ZodNull) {
|
|
2697
2857
|
return [null];
|
|
2698
2858
|
}
|
|
2859
|
+
else if (type instanceof ZodOptional) {
|
|
2860
|
+
return [undefined, ...getDiscriminator(type.unwrap())];
|
|
2861
|
+
}
|
|
2862
|
+
else if (type instanceof ZodNullable) {
|
|
2863
|
+
return [null, ...getDiscriminator(type.unwrap())];
|
|
2864
|
+
}
|
|
2865
|
+
else if (type instanceof ZodBranded) {
|
|
2866
|
+
return getDiscriminator(type.unwrap());
|
|
2867
|
+
}
|
|
2868
|
+
else if (type instanceof ZodReadonly) {
|
|
2869
|
+
return getDiscriminator(type.unwrap());
|
|
2870
|
+
}
|
|
2871
|
+
else if (type instanceof ZodCatch) {
|
|
2872
|
+
return getDiscriminator(type._def.innerType);
|
|
2873
|
+
}
|
|
2699
2874
|
else {
|
|
2700
|
-
return
|
|
2875
|
+
return [];
|
|
2701
2876
|
}
|
|
2702
2877
|
};
|
|
2703
2878
|
class ZodDiscriminatedUnion extends ZodType {
|
|
@@ -2760,7 +2935,7 @@ class ZodDiscriminatedUnion extends ZodType {
|
|
|
2760
2935
|
// try {
|
|
2761
2936
|
for (const type of options) {
|
|
2762
2937
|
const discriminatorValues = getDiscriminator(type.shape[discriminator]);
|
|
2763
|
-
if (!discriminatorValues) {
|
|
2938
|
+
if (!discriminatorValues.length) {
|
|
2764
2939
|
throw new Error(`A discriminator value for key \`${discriminator}\` could not be extracted from all schema options`);
|
|
2765
2940
|
}
|
|
2766
2941
|
for (const value of discriminatorValues) {
|
|
@@ -2973,6 +3148,7 @@ class ZodRecord extends ZodType {
|
|
|
2973
3148
|
pairs.push({
|
|
2974
3149
|
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
|
|
2975
3150
|
value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
|
|
3151
|
+
alwaysSet: key in ctx.data,
|
|
2976
3152
|
});
|
|
2977
3153
|
}
|
|
2978
3154
|
if (ctx.common.async) {
|
|
@@ -3332,6 +3508,10 @@ function createZodEnum(values, params) {
|
|
|
3332
3508
|
});
|
|
3333
3509
|
}
|
|
3334
3510
|
class ZodEnum extends ZodType {
|
|
3511
|
+
constructor() {
|
|
3512
|
+
super(...arguments);
|
|
3513
|
+
_ZodEnum_cache.set(this, void 0);
|
|
3514
|
+
}
|
|
3335
3515
|
_parse(input) {
|
|
3336
3516
|
if (typeof input.data !== "string") {
|
|
3337
3517
|
const ctx = this._getOrReturnCtx(input);
|
|
@@ -3343,7 +3523,10 @@ class ZodEnum extends ZodType {
|
|
|
3343
3523
|
});
|
|
3344
3524
|
return INVALID;
|
|
3345
3525
|
}
|
|
3346
|
-
if (this
|
|
3526
|
+
if (!__classPrivateFieldGet(this, _ZodEnum_cache)) {
|
|
3527
|
+
__classPrivateFieldSet(this, _ZodEnum_cache, new Set(this._def.values));
|
|
3528
|
+
}
|
|
3529
|
+
if (!__classPrivateFieldGet(this, _ZodEnum_cache).has(input.data)) {
|
|
3347
3530
|
const ctx = this._getOrReturnCtx(input);
|
|
3348
3531
|
const expectedValues = this._def.values;
|
|
3349
3532
|
addIssueToContext(ctx, {
|
|
@@ -3379,15 +3562,26 @@ class ZodEnum extends ZodType {
|
|
|
3379
3562
|
}
|
|
3380
3563
|
return enumValues;
|
|
3381
3564
|
}
|
|
3382
|
-
extract(values) {
|
|
3383
|
-
return ZodEnum.create(values
|
|
3565
|
+
extract(values, newDef = this._def) {
|
|
3566
|
+
return ZodEnum.create(values, {
|
|
3567
|
+
...this._def,
|
|
3568
|
+
...newDef,
|
|
3569
|
+
});
|
|
3384
3570
|
}
|
|
3385
|
-
exclude(values) {
|
|
3386
|
-
return ZodEnum.create(this.options.filter((opt) => !values.includes(opt))
|
|
3571
|
+
exclude(values, newDef = this._def) {
|
|
3572
|
+
return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
|
|
3573
|
+
...this._def,
|
|
3574
|
+
...newDef,
|
|
3575
|
+
});
|
|
3387
3576
|
}
|
|
3388
3577
|
}
|
|
3578
|
+
_ZodEnum_cache = new WeakMap();
|
|
3389
3579
|
ZodEnum.create = createZodEnum;
|
|
3390
3580
|
class ZodNativeEnum extends ZodType {
|
|
3581
|
+
constructor() {
|
|
3582
|
+
super(...arguments);
|
|
3583
|
+
_ZodNativeEnum_cache.set(this, void 0);
|
|
3584
|
+
}
|
|
3391
3585
|
_parse(input) {
|
|
3392
3586
|
const nativeEnumValues = util.getValidEnumValues(this._def.values);
|
|
3393
3587
|
const ctx = this._getOrReturnCtx(input);
|
|
@@ -3401,7 +3595,10 @@ class ZodNativeEnum extends ZodType {
|
|
|
3401
3595
|
});
|
|
3402
3596
|
return INVALID;
|
|
3403
3597
|
}
|
|
3404
|
-
if (
|
|
3598
|
+
if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache)) {
|
|
3599
|
+
__classPrivateFieldSet(this, _ZodNativeEnum_cache, new Set(util.getValidEnumValues(this._def.values)));
|
|
3600
|
+
}
|
|
3601
|
+
if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache).has(input.data)) {
|
|
3405
3602
|
const expectedValues = util.objectValues(nativeEnumValues);
|
|
3406
3603
|
addIssueToContext(ctx, {
|
|
3407
3604
|
received: ctx.data,
|
|
@@ -3416,6 +3613,7 @@ class ZodNativeEnum extends ZodType {
|
|
|
3416
3613
|
return this._def.values;
|
|
3417
3614
|
}
|
|
3418
3615
|
}
|
|
3616
|
+
_ZodNativeEnum_cache = new WeakMap();
|
|
3419
3617
|
ZodNativeEnum.create = (values, params) => {
|
|
3420
3618
|
return new ZodNativeEnum({
|
|
3421
3619
|
values: values,
|
|
@@ -3485,33 +3683,43 @@ class ZodEffects extends ZodType {
|
|
|
3485
3683
|
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
3486
3684
|
if (effect.type === "preprocess") {
|
|
3487
3685
|
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
3686
|
if (ctx.common.async) {
|
|
3495
|
-
return Promise.resolve(processed).then((processed) => {
|
|
3496
|
-
|
|
3687
|
+
return Promise.resolve(processed).then(async (processed) => {
|
|
3688
|
+
if (status.value === "aborted")
|
|
3689
|
+
return INVALID;
|
|
3690
|
+
const result = await this._def.schema._parseAsync({
|
|
3497
3691
|
data: processed,
|
|
3498
3692
|
path: ctx.path,
|
|
3499
3693
|
parent: ctx,
|
|
3500
3694
|
});
|
|
3695
|
+
if (result.status === "aborted")
|
|
3696
|
+
return INVALID;
|
|
3697
|
+
if (result.status === "dirty")
|
|
3698
|
+
return DIRTY(result.value);
|
|
3699
|
+
if (status.value === "dirty")
|
|
3700
|
+
return DIRTY(result.value);
|
|
3701
|
+
return result;
|
|
3501
3702
|
});
|
|
3502
3703
|
}
|
|
3503
3704
|
else {
|
|
3504
|
-
|
|
3705
|
+
if (status.value === "aborted")
|
|
3706
|
+
return INVALID;
|
|
3707
|
+
const result = this._def.schema._parseSync({
|
|
3505
3708
|
data: processed,
|
|
3506
3709
|
path: ctx.path,
|
|
3507
3710
|
parent: ctx,
|
|
3508
3711
|
});
|
|
3712
|
+
if (result.status === "aborted")
|
|
3713
|
+
return INVALID;
|
|
3714
|
+
if (result.status === "dirty")
|
|
3715
|
+
return DIRTY(result.value);
|
|
3716
|
+
if (status.value === "dirty")
|
|
3717
|
+
return DIRTY(result.value);
|
|
3718
|
+
return result;
|
|
3509
3719
|
}
|
|
3510
3720
|
}
|
|
3511
3721
|
if (effect.type === "refinement") {
|
|
3512
|
-
const executeRefinement = (acc
|
|
3513
|
-
// effect: RefinementEffect<any>
|
|
3514
|
-
) => {
|
|
3722
|
+
const executeRefinement = (acc) => {
|
|
3515
3723
|
const result = effect.refinement(acc, checkCtx);
|
|
3516
3724
|
if (ctx.common.async) {
|
|
3517
3725
|
return Promise.resolve(result);
|
|
@@ -3813,10 +4021,18 @@ class ZodPipeline extends ZodType {
|
|
|
3813
4021
|
class ZodReadonly extends ZodType {
|
|
3814
4022
|
_parse(input) {
|
|
3815
4023
|
const result = this._def.innerType._parse(input);
|
|
3816
|
-
|
|
3817
|
-
|
|
3818
|
-
|
|
3819
|
-
|
|
4024
|
+
const freeze = (data) => {
|
|
4025
|
+
if (isValid(data)) {
|
|
4026
|
+
data.value = Object.freeze(data.value);
|
|
4027
|
+
}
|
|
4028
|
+
return data;
|
|
4029
|
+
};
|
|
4030
|
+
return isAsync(result)
|
|
4031
|
+
? result.then((data) => freeze(data))
|
|
4032
|
+
: freeze(result);
|
|
4033
|
+
}
|
|
4034
|
+
unwrap() {
|
|
4035
|
+
return this._def.innerType;
|
|
3820
4036
|
}
|
|
3821
4037
|
}
|
|
3822
4038
|
ZodReadonly.create = (type, params) => {
|
|
@@ -3932,5 +4148,19 @@ const formatErrorPath = (path) => {
|
|
|
3932
4148
|
return formatted;
|
|
3933
4149
|
};
|
|
3934
4150
|
|
|
3935
|
-
|
|
4151
|
+
/**
|
|
4152
|
+
* Get the hash of the content string. It returns the first 5 characters of the hash
|
|
4153
|
+
* Example: getContentHash("Hello World")
|
|
4154
|
+
* @param contentString The content string to hash
|
|
4155
|
+
* @returns
|
|
4156
|
+
*/
|
|
4157
|
+
const getContentHash = (contentString) => {
|
|
4158
|
+
return crypto
|
|
4159
|
+
.createHash("md5")
|
|
4160
|
+
.update(contentString)
|
|
4161
|
+
.digest("hex")
|
|
4162
|
+
.substring(0, 5);
|
|
4163
|
+
};
|
|
4164
|
+
|
|
4165
|
+
export { errorFormatter, findFiles, formatErrorPath, getContentHash, runProcess };
|
|
3936
4166
|
//# sourceMappingURL=index.esm.js.map
|