zod 3.22.4 → 3.23.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +279 -237
- package/lib/ZodError.d.ts +2 -1
- package/lib/ZodError.js +5 -0
- package/lib/benchmarks/datetime.d.ts +5 -0
- package/lib/benchmarks/datetime.js +54 -0
- package/lib/benchmarks/index.js +15 -2
- package/lib/benchmarks/ipv4.d.ts +5 -0
- package/lib/benchmarks/ipv4.js +54 -0
- package/lib/benchmarks/primitives.js +34 -0
- package/lib/helpers/parseUtil.d.ts +1 -1
- package/lib/helpers/parseUtil.js +15 -5
- package/lib/helpers/util.d.ts +16 -2
- package/lib/index.mjs +289 -70
- package/lib/index.umd.js +289 -69
- package/lib/types.d.ts +113 -43
- package/lib/types.js +249 -67
- package/package.json +6 -6
package/lib/types.js
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
4
|
+
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");
|
|
5
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
8
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
9
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
10
|
+
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");
|
|
11
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
12
|
+
};
|
|
13
|
+
var _ZodEnum_cache, _ZodNativeEnum_cache;
|
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
exports.NEVER = exports.void = exports.unknown = exports.union = exports.undefined = exports.tuple = exports.transformer = exports.symbol = exports.string = exports.strictObject = exports.set = exports.record = exports.promise = exports.preprocess = exports.pipeline = exports.ostring = exports.optional = exports.onumber = exports.oboolean = exports.object = exports.number = exports.nullable = exports.null = exports.never = exports.nativeEnum = exports.nan = exports.map = exports.literal = exports.lazy = exports.intersection = exports.instanceof = exports.function = exports.enum = exports.effect = exports.discriminatedUnion = void 0;
|
|
15
|
+
exports.boolean = exports.bigint = exports.array = exports.any = exports.coerce = exports.ZodFirstPartyTypeKind = exports.late = exports.ZodSchema = exports.Schema = exports.custom = exports.ZodReadonly = exports.ZodPipeline = exports.ZodBranded = exports.BRAND = exports.ZodNaN = exports.ZodCatch = exports.ZodDefault = exports.ZodNullable = exports.ZodOptional = exports.ZodTransformer = exports.ZodEffects = exports.ZodPromise = exports.ZodNativeEnum = exports.ZodEnum = exports.ZodLiteral = exports.ZodLazy = exports.ZodFunction = exports.ZodSet = exports.ZodMap = exports.ZodRecord = exports.ZodTuple = exports.ZodIntersection = exports.ZodDiscriminatedUnion = exports.ZodUnion = exports.ZodObject = exports.ZodArray = exports.ZodVoid = exports.ZodNever = exports.ZodUnknown = exports.ZodAny = exports.ZodNull = exports.ZodUndefined = exports.ZodSymbol = exports.ZodDate = exports.ZodBoolean = exports.ZodBigInt = exports.ZodNumber = exports.ZodString = exports.datetimeRegex = exports.ZodType = void 0;
|
|
16
|
+
exports.NEVER = exports.void = exports.unknown = exports.union = exports.undefined = exports.tuple = exports.transformer = exports.symbol = exports.string = exports.strictObject = exports.set = exports.record = exports.promise = exports.preprocess = exports.pipeline = exports.ostring = exports.optional = exports.onumber = exports.oboolean = exports.object = exports.number = exports.nullable = exports.null = exports.never = exports.nativeEnum = exports.nan = exports.map = exports.literal = exports.lazy = exports.intersection = exports.instanceof = exports.function = exports.enum = exports.effect = exports.discriminatedUnion = exports.date = void 0;
|
|
5
17
|
const errors_1 = require("./errors");
|
|
6
18
|
const errorUtil_1 = require("./helpers/errorUtil");
|
|
7
19
|
const parseUtil_1 = require("./helpers/parseUtil");
|
|
@@ -57,12 +69,17 @@ function processCreateParams(params) {
|
|
|
57
69
|
if (errorMap)
|
|
58
70
|
return { errorMap: errorMap, description };
|
|
59
71
|
const customMap = (iss, ctx) => {
|
|
60
|
-
|
|
61
|
-
|
|
72
|
+
var _a, _b;
|
|
73
|
+
const { message } = params;
|
|
74
|
+
if (iss.code === "invalid_enum_value") {
|
|
75
|
+
return { message: message !== null && message !== void 0 ? message : ctx.defaultError };
|
|
76
|
+
}
|
|
62
77
|
if (typeof ctx.data === "undefined") {
|
|
63
|
-
return { message:
|
|
78
|
+
return { message: (_a = message !== null && message !== void 0 ? message : required_error) !== null && _a !== void 0 ? _a : ctx.defaultError };
|
|
64
79
|
}
|
|
65
|
-
|
|
80
|
+
if (iss.code !== "invalid_type")
|
|
81
|
+
return { message: ctx.defaultError };
|
|
82
|
+
return { message: (_b = message !== null && message !== void 0 ? message : invalid_type_error) !== null && _b !== void 0 ? _b : ctx.defaultError };
|
|
66
83
|
};
|
|
67
84
|
return { errorMap: customMap, description };
|
|
68
85
|
}
|
|
@@ -323,11 +340,13 @@ exports.ZodType = ZodType;
|
|
|
323
340
|
exports.Schema = ZodType;
|
|
324
341
|
exports.ZodSchema = ZodType;
|
|
325
342
|
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
326
|
-
const cuid2Regex = /^[
|
|
343
|
+
const cuid2Regex = /^[0-9a-z]+$/;
|
|
327
344
|
const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/;
|
|
328
345
|
// const uuidRegex =
|
|
329
346
|
// /^([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;
|
|
330
347
|
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;
|
|
348
|
+
const nanoidRegex = /^[a-z0-9_-]{21}$/i;
|
|
349
|
+
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)?)??$/;
|
|
331
350
|
// from https://stackoverflow.com/a/46181/1550155
|
|
332
351
|
// old version: too slow, didn't support unicode
|
|
333
352
|
// 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;
|
|
@@ -340,41 +359,49 @@ const uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-
|
|
|
340
359
|
// /^[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])?)*$/;
|
|
341
360
|
// const emailRegex =
|
|
342
361
|
// /^(?:[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;
|
|
343
|
-
const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_
|
|
362
|
+
const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
344
363
|
// const emailRegex =
|
|
345
364
|
// /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;
|
|
346
365
|
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
|
|
347
366
|
const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
348
367
|
let emojiRegex;
|
|
349
|
-
|
|
368
|
+
// faster, simpler, safer
|
|
369
|
+
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])$/;
|
|
350
370
|
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})))$/;
|
|
351
|
-
//
|
|
352
|
-
const
|
|
371
|
+
// https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
|
|
372
|
+
const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
373
|
+
// simple
|
|
374
|
+
// const dateRegexSource = `\\d{4}-\\d{2}-\\d{2}`;
|
|
375
|
+
// no leap year validation
|
|
376
|
+
// 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))`;
|
|
377
|
+
// with leap year validation
|
|
378
|
+
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])))`;
|
|
379
|
+
const dateRegex = new RegExp(`^${dateRegexSource}$`);
|
|
380
|
+
function timeRegexSource(args) {
|
|
381
|
+
// let regex = `\\d{2}:\\d{2}:\\d{2}`;
|
|
382
|
+
let regex = `([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d`;
|
|
353
383
|
if (args.precision) {
|
|
354
|
-
|
|
355
|
-
return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${args.precision}}(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
|
|
356
|
-
}
|
|
357
|
-
else {
|
|
358
|
-
return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${args.precision}}Z$`);
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
else if (args.precision === 0) {
|
|
362
|
-
if (args.offset) {
|
|
363
|
-
return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
|
|
364
|
-
}
|
|
365
|
-
else {
|
|
366
|
-
return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$`);
|
|
367
|
-
}
|
|
384
|
+
regex = `${regex}\\.\\d{${args.precision}}`;
|
|
368
385
|
}
|
|
369
|
-
else {
|
|
370
|
-
|
|
371
|
-
return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
|
|
372
|
-
}
|
|
373
|
-
else {
|
|
374
|
-
return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?Z$`);
|
|
375
|
-
}
|
|
386
|
+
else if (args.precision == null) {
|
|
387
|
+
regex = `${regex}(\\.\\d+)?`;
|
|
376
388
|
}
|
|
377
|
-
|
|
389
|
+
return regex;
|
|
390
|
+
}
|
|
391
|
+
function timeRegex(args) {
|
|
392
|
+
return new RegExp(`^${timeRegexSource(args)}$`);
|
|
393
|
+
}
|
|
394
|
+
// Adapted from https://stackoverflow.com/a/3143231
|
|
395
|
+
function datetimeRegex(args) {
|
|
396
|
+
let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
|
|
397
|
+
const opts = [];
|
|
398
|
+
opts.push(args.local ? `Z?` : `Z`);
|
|
399
|
+
if (args.offset)
|
|
400
|
+
opts.push(`([+-]\\d{2}:?\\d{2})`);
|
|
401
|
+
regex = `${regex}(${opts.join("|")})`;
|
|
402
|
+
return new RegExp(`^${regex}$`);
|
|
403
|
+
}
|
|
404
|
+
exports.datetimeRegex = datetimeRegex;
|
|
378
405
|
function isValidIP(ip, version) {
|
|
379
406
|
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
|
|
380
407
|
return true;
|
|
@@ -396,9 +423,7 @@ class ZodString extends ZodType {
|
|
|
396
423
|
code: ZodError_1.ZodIssueCode.invalid_type,
|
|
397
424
|
expected: util_1.ZodParsedType.string,
|
|
398
425
|
received: ctx.parsedType,
|
|
399
|
-
}
|
|
400
|
-
//
|
|
401
|
-
);
|
|
426
|
+
});
|
|
402
427
|
return parseUtil_1.INVALID;
|
|
403
428
|
}
|
|
404
429
|
const status = new parseUtil_1.ParseStatus();
|
|
@@ -496,6 +521,17 @@ class ZodString extends ZodType {
|
|
|
496
521
|
status.dirty();
|
|
497
522
|
}
|
|
498
523
|
}
|
|
524
|
+
else if (check.kind === "nanoid") {
|
|
525
|
+
if (!nanoidRegex.test(input.data)) {
|
|
526
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
527
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
528
|
+
validation: "nanoid",
|
|
529
|
+
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
530
|
+
message: check.message,
|
|
531
|
+
});
|
|
532
|
+
status.dirty();
|
|
533
|
+
}
|
|
534
|
+
}
|
|
499
535
|
else if (check.kind === "cuid") {
|
|
500
536
|
if (!cuidRegex.test(input.data)) {
|
|
501
537
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
@@ -610,6 +646,41 @@ class ZodString extends ZodType {
|
|
|
610
646
|
status.dirty();
|
|
611
647
|
}
|
|
612
648
|
}
|
|
649
|
+
else if (check.kind === "date") {
|
|
650
|
+
const regex = dateRegex;
|
|
651
|
+
if (!regex.test(input.data)) {
|
|
652
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
653
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
654
|
+
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
655
|
+
validation: "date",
|
|
656
|
+
message: check.message,
|
|
657
|
+
});
|
|
658
|
+
status.dirty();
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
else if (check.kind === "time") {
|
|
662
|
+
const regex = timeRegex(check);
|
|
663
|
+
if (!regex.test(input.data)) {
|
|
664
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
665
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
666
|
+
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
667
|
+
validation: "time",
|
|
668
|
+
message: check.message,
|
|
669
|
+
});
|
|
670
|
+
status.dirty();
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
else if (check.kind === "duration") {
|
|
674
|
+
if (!durationRegex.test(input.data)) {
|
|
675
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
676
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
677
|
+
validation: "duration",
|
|
678
|
+
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
679
|
+
message: check.message,
|
|
680
|
+
});
|
|
681
|
+
status.dirty();
|
|
682
|
+
}
|
|
683
|
+
}
|
|
613
684
|
else if (check.kind === "ip") {
|
|
614
685
|
if (!isValidIP(input.data, check.version)) {
|
|
615
686
|
ctx = this._getOrReturnCtx(input, ctx);
|
|
@@ -621,6 +692,17 @@ class ZodString extends ZodType {
|
|
|
621
692
|
status.dirty();
|
|
622
693
|
}
|
|
623
694
|
}
|
|
695
|
+
else if (check.kind === "base64") {
|
|
696
|
+
if (!base64Regex.test(input.data)) {
|
|
697
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
698
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
699
|
+
validation: "base64",
|
|
700
|
+
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
701
|
+
message: check.message,
|
|
702
|
+
});
|
|
703
|
+
status.dirty();
|
|
704
|
+
}
|
|
705
|
+
}
|
|
624
706
|
else {
|
|
625
707
|
util_1.util.assertNever(check);
|
|
626
708
|
}
|
|
@@ -652,6 +734,9 @@ class ZodString extends ZodType {
|
|
|
652
734
|
uuid(message) {
|
|
653
735
|
return this._addCheck({ kind: "uuid", ...errorUtil_1.errorUtil.errToObj(message) });
|
|
654
736
|
}
|
|
737
|
+
nanoid(message) {
|
|
738
|
+
return this._addCheck({ kind: "nanoid", ...errorUtil_1.errorUtil.errToObj(message) });
|
|
739
|
+
}
|
|
655
740
|
cuid(message) {
|
|
656
741
|
return this._addCheck({ kind: "cuid", ...errorUtil_1.errorUtil.errToObj(message) });
|
|
657
742
|
}
|
|
@@ -661,16 +746,20 @@ class ZodString extends ZodType {
|
|
|
661
746
|
ulid(message) {
|
|
662
747
|
return this._addCheck({ kind: "ulid", ...errorUtil_1.errorUtil.errToObj(message) });
|
|
663
748
|
}
|
|
749
|
+
base64(message) {
|
|
750
|
+
return this._addCheck({ kind: "base64", ...errorUtil_1.errorUtil.errToObj(message) });
|
|
751
|
+
}
|
|
664
752
|
ip(options) {
|
|
665
753
|
return this._addCheck({ kind: "ip", ...errorUtil_1.errorUtil.errToObj(options) });
|
|
666
754
|
}
|
|
667
755
|
datetime(options) {
|
|
668
|
-
var _a;
|
|
756
|
+
var _a, _b;
|
|
669
757
|
if (typeof options === "string") {
|
|
670
758
|
return this._addCheck({
|
|
671
759
|
kind: "datetime",
|
|
672
760
|
precision: null,
|
|
673
761
|
offset: false,
|
|
762
|
+
local: false,
|
|
674
763
|
message: options,
|
|
675
764
|
});
|
|
676
765
|
}
|
|
@@ -678,9 +767,30 @@ class ZodString extends ZodType {
|
|
|
678
767
|
kind: "datetime",
|
|
679
768
|
precision: typeof (options === null || options === void 0 ? void 0 : options.precision) === "undefined" ? null : options === null || options === void 0 ? void 0 : options.precision,
|
|
680
769
|
offset: (_a = options === null || options === void 0 ? void 0 : options.offset) !== null && _a !== void 0 ? _a : false,
|
|
770
|
+
local: (_b = options === null || options === void 0 ? void 0 : options.local) !== null && _b !== void 0 ? _b : false,
|
|
771
|
+
...errorUtil_1.errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
|
|
772
|
+
});
|
|
773
|
+
}
|
|
774
|
+
date(message) {
|
|
775
|
+
return this._addCheck({ kind: "date", message });
|
|
776
|
+
}
|
|
777
|
+
time(options) {
|
|
778
|
+
if (typeof options === "string") {
|
|
779
|
+
return this._addCheck({
|
|
780
|
+
kind: "time",
|
|
781
|
+
precision: null,
|
|
782
|
+
message: options,
|
|
783
|
+
});
|
|
784
|
+
}
|
|
785
|
+
return this._addCheck({
|
|
786
|
+
kind: "time",
|
|
787
|
+
precision: typeof (options === null || options === void 0 ? void 0 : options.precision) === "undefined" ? null : options === null || options === void 0 ? void 0 : options.precision,
|
|
681
788
|
...errorUtil_1.errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
|
|
682
789
|
});
|
|
683
790
|
}
|
|
791
|
+
duration(message) {
|
|
792
|
+
return this._addCheck({ kind: "duration", ...errorUtil_1.errorUtil.errToObj(message) });
|
|
793
|
+
}
|
|
684
794
|
regex(regex, message) {
|
|
685
795
|
return this._addCheck({
|
|
686
796
|
kind: "regex",
|
|
@@ -759,6 +869,15 @@ class ZodString extends ZodType {
|
|
|
759
869
|
get isDatetime() {
|
|
760
870
|
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
761
871
|
}
|
|
872
|
+
get isDate() {
|
|
873
|
+
return !!this._def.checks.find((ch) => ch.kind === "date");
|
|
874
|
+
}
|
|
875
|
+
get isTime() {
|
|
876
|
+
return !!this._def.checks.find((ch) => ch.kind === "time");
|
|
877
|
+
}
|
|
878
|
+
get isDuration() {
|
|
879
|
+
return !!this._def.checks.find((ch) => ch.kind === "duration");
|
|
880
|
+
}
|
|
762
881
|
get isEmail() {
|
|
763
882
|
return !!this._def.checks.find((ch) => ch.kind === "email");
|
|
764
883
|
}
|
|
@@ -771,6 +890,9 @@ class ZodString extends ZodType {
|
|
|
771
890
|
get isUUID() {
|
|
772
891
|
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
773
892
|
}
|
|
893
|
+
get isNANOID() {
|
|
894
|
+
return !!this._def.checks.find((ch) => ch.kind === "nanoid");
|
|
895
|
+
}
|
|
774
896
|
get isCUID() {
|
|
775
897
|
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
776
898
|
}
|
|
@@ -783,6 +905,9 @@ class ZodString extends ZodType {
|
|
|
783
905
|
get isIP() {
|
|
784
906
|
return !!this._def.checks.find((ch) => ch.kind === "ip");
|
|
785
907
|
}
|
|
908
|
+
get isBase64() {
|
|
909
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64");
|
|
910
|
+
}
|
|
786
911
|
get minLength() {
|
|
787
912
|
let min = null;
|
|
788
913
|
for (const ch of this._def.checks) {
|
|
@@ -1784,9 +1909,10 @@ class ZodObject extends ZodType {
|
|
|
1784
1909
|
const syncPairs = [];
|
|
1785
1910
|
for (const pair of pairs) {
|
|
1786
1911
|
const key = await pair.key;
|
|
1912
|
+
const value = await pair.value;
|
|
1787
1913
|
syncPairs.push({
|
|
1788
1914
|
key,
|
|
1789
|
-
value
|
|
1915
|
+
value,
|
|
1790
1916
|
alwaysSet: pair.alwaysSet,
|
|
1791
1917
|
});
|
|
1792
1918
|
}
|
|
@@ -2162,7 +2288,7 @@ const getDiscriminator = (type) => {
|
|
|
2162
2288
|
}
|
|
2163
2289
|
else if (type instanceof ZodNativeEnum) {
|
|
2164
2290
|
// eslint-disable-next-line ban/ban
|
|
2165
|
-
return
|
|
2291
|
+
return util_1.util.objectValues(type.enum);
|
|
2166
2292
|
}
|
|
2167
2293
|
else if (type instanceof ZodDefault) {
|
|
2168
2294
|
return getDiscriminator(type._def.innerType);
|
|
@@ -2173,8 +2299,23 @@ const getDiscriminator = (type) => {
|
|
|
2173
2299
|
else if (type instanceof ZodNull) {
|
|
2174
2300
|
return [null];
|
|
2175
2301
|
}
|
|
2302
|
+
else if (type instanceof ZodOptional) {
|
|
2303
|
+
return [undefined, ...getDiscriminator(type.unwrap())];
|
|
2304
|
+
}
|
|
2305
|
+
else if (type instanceof ZodNullable) {
|
|
2306
|
+
return [null, ...getDiscriminator(type.unwrap())];
|
|
2307
|
+
}
|
|
2308
|
+
else if (type instanceof ZodBranded) {
|
|
2309
|
+
return getDiscriminator(type.unwrap());
|
|
2310
|
+
}
|
|
2311
|
+
else if (type instanceof ZodReadonly) {
|
|
2312
|
+
return getDiscriminator(type.unwrap());
|
|
2313
|
+
}
|
|
2314
|
+
else if (type instanceof ZodCatch) {
|
|
2315
|
+
return getDiscriminator(type._def.innerType);
|
|
2316
|
+
}
|
|
2176
2317
|
else {
|
|
2177
|
-
return
|
|
2318
|
+
return [];
|
|
2178
2319
|
}
|
|
2179
2320
|
};
|
|
2180
2321
|
class ZodDiscriminatedUnion extends ZodType {
|
|
@@ -2237,7 +2378,7 @@ class ZodDiscriminatedUnion extends ZodType {
|
|
|
2237
2378
|
// try {
|
|
2238
2379
|
for (const type of options) {
|
|
2239
2380
|
const discriminatorValues = getDiscriminator(type.shape[discriminator]);
|
|
2240
|
-
if (!discriminatorValues) {
|
|
2381
|
+
if (!discriminatorValues.length) {
|
|
2241
2382
|
throw new Error(`A discriminator value for key \`${discriminator}\` could not be extracted from all schema options`);
|
|
2242
2383
|
}
|
|
2243
2384
|
for (const value of discriminatorValues) {
|
|
@@ -2453,6 +2594,7 @@ class ZodRecord extends ZodType {
|
|
|
2453
2594
|
pairs.push({
|
|
2454
2595
|
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
|
|
2455
2596
|
value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
|
|
2597
|
+
alwaysSet: key in ctx.data,
|
|
2456
2598
|
});
|
|
2457
2599
|
}
|
|
2458
2600
|
if (ctx.common.async) {
|
|
@@ -2818,6 +2960,10 @@ function createZodEnum(values, params) {
|
|
|
2818
2960
|
});
|
|
2819
2961
|
}
|
|
2820
2962
|
class ZodEnum extends ZodType {
|
|
2963
|
+
constructor() {
|
|
2964
|
+
super(...arguments);
|
|
2965
|
+
_ZodEnum_cache.set(this, void 0);
|
|
2966
|
+
}
|
|
2821
2967
|
_parse(input) {
|
|
2822
2968
|
if (typeof input.data !== "string") {
|
|
2823
2969
|
const ctx = this._getOrReturnCtx(input);
|
|
@@ -2829,7 +2975,10 @@ class ZodEnum extends ZodType {
|
|
|
2829
2975
|
});
|
|
2830
2976
|
return parseUtil_1.INVALID;
|
|
2831
2977
|
}
|
|
2832
|
-
if (this
|
|
2978
|
+
if (!__classPrivateFieldGet(this, _ZodEnum_cache, "f")) {
|
|
2979
|
+
__classPrivateFieldSet(this, _ZodEnum_cache, new Set(this._def.values), "f");
|
|
2980
|
+
}
|
|
2981
|
+
if (!__classPrivateFieldGet(this, _ZodEnum_cache, "f").has(input.data)) {
|
|
2833
2982
|
const ctx = this._getOrReturnCtx(input);
|
|
2834
2983
|
const expectedValues = this._def.values;
|
|
2835
2984
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
@@ -2865,16 +3014,27 @@ class ZodEnum extends ZodType {
|
|
|
2865
3014
|
}
|
|
2866
3015
|
return enumValues;
|
|
2867
3016
|
}
|
|
2868
|
-
extract(values) {
|
|
2869
|
-
return ZodEnum.create(values
|
|
3017
|
+
extract(values, newDef = this._def) {
|
|
3018
|
+
return ZodEnum.create(values, {
|
|
3019
|
+
...this._def,
|
|
3020
|
+
...newDef,
|
|
3021
|
+
});
|
|
2870
3022
|
}
|
|
2871
|
-
exclude(values) {
|
|
2872
|
-
return ZodEnum.create(this.options.filter((opt) => !values.includes(opt))
|
|
3023
|
+
exclude(values, newDef = this._def) {
|
|
3024
|
+
return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
|
|
3025
|
+
...this._def,
|
|
3026
|
+
...newDef,
|
|
3027
|
+
});
|
|
2873
3028
|
}
|
|
2874
3029
|
}
|
|
2875
3030
|
exports.ZodEnum = ZodEnum;
|
|
3031
|
+
_ZodEnum_cache = new WeakMap();
|
|
2876
3032
|
ZodEnum.create = createZodEnum;
|
|
2877
3033
|
class ZodNativeEnum extends ZodType {
|
|
3034
|
+
constructor() {
|
|
3035
|
+
super(...arguments);
|
|
3036
|
+
_ZodNativeEnum_cache.set(this, void 0);
|
|
3037
|
+
}
|
|
2878
3038
|
_parse(input) {
|
|
2879
3039
|
const nativeEnumValues = util_1.util.getValidEnumValues(this._def.values);
|
|
2880
3040
|
const ctx = this._getOrReturnCtx(input);
|
|
@@ -2888,7 +3048,10 @@ class ZodNativeEnum extends ZodType {
|
|
|
2888
3048
|
});
|
|
2889
3049
|
return parseUtil_1.INVALID;
|
|
2890
3050
|
}
|
|
2891
|
-
if (
|
|
3051
|
+
if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache, "f")) {
|
|
3052
|
+
__classPrivateFieldSet(this, _ZodNativeEnum_cache, new Set(util_1.util.getValidEnumValues(this._def.values)), "f");
|
|
3053
|
+
}
|
|
3054
|
+
if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache, "f").has(input.data)) {
|
|
2892
3055
|
const expectedValues = util_1.util.objectValues(nativeEnumValues);
|
|
2893
3056
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
2894
3057
|
received: ctx.data,
|
|
@@ -2904,6 +3067,7 @@ class ZodNativeEnum extends ZodType {
|
|
|
2904
3067
|
}
|
|
2905
3068
|
}
|
|
2906
3069
|
exports.ZodNativeEnum = ZodNativeEnum;
|
|
3070
|
+
_ZodNativeEnum_cache = new WeakMap();
|
|
2907
3071
|
ZodNativeEnum.create = (values, params) => {
|
|
2908
3072
|
return new ZodNativeEnum({
|
|
2909
3073
|
values: values,
|
|
@@ -2974,33 +3138,43 @@ class ZodEffects extends ZodType {
|
|
|
2974
3138
|
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
2975
3139
|
if (effect.type === "preprocess") {
|
|
2976
3140
|
const processed = effect.transform(ctx.data, checkCtx);
|
|
2977
|
-
if (ctx.common.issues.length) {
|
|
2978
|
-
return {
|
|
2979
|
-
status: "dirty",
|
|
2980
|
-
value: ctx.data,
|
|
2981
|
-
};
|
|
2982
|
-
}
|
|
2983
3141
|
if (ctx.common.async) {
|
|
2984
|
-
return Promise.resolve(processed).then((processed) => {
|
|
2985
|
-
|
|
3142
|
+
return Promise.resolve(processed).then(async (processed) => {
|
|
3143
|
+
if (status.value === "aborted")
|
|
3144
|
+
return parseUtil_1.INVALID;
|
|
3145
|
+
const result = await this._def.schema._parseAsync({
|
|
2986
3146
|
data: processed,
|
|
2987
3147
|
path: ctx.path,
|
|
2988
3148
|
parent: ctx,
|
|
2989
3149
|
});
|
|
3150
|
+
if (result.status === "aborted")
|
|
3151
|
+
return parseUtil_1.INVALID;
|
|
3152
|
+
if (result.status === "dirty")
|
|
3153
|
+
return (0, parseUtil_1.DIRTY)(result.value);
|
|
3154
|
+
if (status.value === "dirty")
|
|
3155
|
+
return (0, parseUtil_1.DIRTY)(result.value);
|
|
3156
|
+
return result;
|
|
2990
3157
|
});
|
|
2991
3158
|
}
|
|
2992
3159
|
else {
|
|
2993
|
-
|
|
3160
|
+
if (status.value === "aborted")
|
|
3161
|
+
return parseUtil_1.INVALID;
|
|
3162
|
+
const result = this._def.schema._parseSync({
|
|
2994
3163
|
data: processed,
|
|
2995
3164
|
path: ctx.path,
|
|
2996
3165
|
parent: ctx,
|
|
2997
3166
|
});
|
|
3167
|
+
if (result.status === "aborted")
|
|
3168
|
+
return parseUtil_1.INVALID;
|
|
3169
|
+
if (result.status === "dirty")
|
|
3170
|
+
return (0, parseUtil_1.DIRTY)(result.value);
|
|
3171
|
+
if (status.value === "dirty")
|
|
3172
|
+
return (0, parseUtil_1.DIRTY)(result.value);
|
|
3173
|
+
return result;
|
|
2998
3174
|
}
|
|
2999
3175
|
}
|
|
3000
3176
|
if (effect.type === "refinement") {
|
|
3001
|
-
const executeRefinement = (acc
|
|
3002
|
-
// effect: RefinementEffect<any>
|
|
3003
|
-
) => {
|
|
3177
|
+
const executeRefinement = (acc) => {
|
|
3004
3178
|
const result = effect.refinement(acc, checkCtx);
|
|
3005
3179
|
if (ctx.common.async) {
|
|
3006
3180
|
return Promise.resolve(result);
|
|
@@ -3312,10 +3486,18 @@ exports.ZodPipeline = ZodPipeline;
|
|
|
3312
3486
|
class ZodReadonly extends ZodType {
|
|
3313
3487
|
_parse(input) {
|
|
3314
3488
|
const result = this._def.innerType._parse(input);
|
|
3315
|
-
|
|
3316
|
-
|
|
3317
|
-
|
|
3318
|
-
|
|
3489
|
+
const freeze = (data) => {
|
|
3490
|
+
if ((0, parseUtil_1.isValid)(data)) {
|
|
3491
|
+
data.value = Object.freeze(data.value);
|
|
3492
|
+
}
|
|
3493
|
+
return data;
|
|
3494
|
+
};
|
|
3495
|
+
return (0, parseUtil_1.isAsync)(result)
|
|
3496
|
+
? result.then((data) => freeze(data))
|
|
3497
|
+
: freeze(result);
|
|
3498
|
+
}
|
|
3499
|
+
unwrap() {
|
|
3500
|
+
return this._def.innerType;
|
|
3319
3501
|
}
|
|
3320
3502
|
}
|
|
3321
3503
|
exports.ZodReadonly = ZodReadonly;
|
|
@@ -3326,7 +3508,7 @@ ZodReadonly.create = (type, params) => {
|
|
|
3326
3508
|
...processCreateParams(params),
|
|
3327
3509
|
});
|
|
3328
3510
|
};
|
|
3329
|
-
|
|
3511
|
+
function custom(check, params = {},
|
|
3330
3512
|
/**
|
|
3331
3513
|
* @deprecated
|
|
3332
3514
|
*
|
|
@@ -3337,7 +3519,7 @@ const custom = (check, params = {},
|
|
|
3337
3519
|
* ```
|
|
3338
3520
|
*
|
|
3339
3521
|
*/
|
|
3340
|
-
fatal)
|
|
3522
|
+
fatal) {
|
|
3341
3523
|
if (check)
|
|
3342
3524
|
return ZodAny.create().superRefine((data, ctx) => {
|
|
3343
3525
|
var _a, _b;
|
|
@@ -3353,7 +3535,7 @@ fatal) => {
|
|
|
3353
3535
|
}
|
|
3354
3536
|
});
|
|
3355
3537
|
return ZodAny.create();
|
|
3356
|
-
}
|
|
3538
|
+
}
|
|
3357
3539
|
exports.custom = custom;
|
|
3358
3540
|
exports.late = {
|
|
3359
3541
|
object: ZodObject.lazycreate,
|
|
@@ -3405,7 +3587,7 @@ const instanceOfType = (
|
|
|
3405
3587
|
// const instanceOfType = <T extends new (...args: any[]) => any>(
|
|
3406
3588
|
cls, params = {
|
|
3407
3589
|
message: `Input not instance of ${cls.name}`,
|
|
3408
|
-
}) =>
|
|
3590
|
+
}) => custom((data) => data instanceof cls, params);
|
|
3409
3591
|
exports.instanceof = instanceOfType;
|
|
3410
3592
|
const stringType = ZodString.create;
|
|
3411
3593
|
exports.string = stringType;
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zod",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.23.8",
|
|
4
4
|
"author": "Colin McDonnell <colin@colinhacks.com>",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
|
-
"url": "https://github.com/colinhacks/zod"
|
|
7
|
+
"url": "git+https://github.com/colinhacks/zod.git"
|
|
8
8
|
},
|
|
9
9
|
"main": "./lib/index.js",
|
|
10
10
|
"module": "./lib/index.mjs",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"@babel/core": "^7.22.5",
|
|
13
13
|
"@babel/preset-env": "^7.22.5",
|
|
14
14
|
"@babel/preset-typescript": "^7.22.5",
|
|
15
|
+
"@jest/globals": "^29.4.3",
|
|
15
16
|
"@rollup/plugin-typescript": "^8.2.0",
|
|
16
17
|
"@swc/core": "^1.3.66",
|
|
17
18
|
"@swc/jest": "^0.2.26",
|
|
@@ -41,7 +42,7 @@
|
|
|
41
42
|
"ts-node": "^10.9.1",
|
|
42
43
|
"tslib": "^2.3.1",
|
|
43
44
|
"tsx": "^3.8.0",
|
|
44
|
-
"typescript": "~4.5.
|
|
45
|
+
"typescript": "~4.5.5",
|
|
45
46
|
"vitest": "^0.32.2"
|
|
46
47
|
},
|
|
47
48
|
"exports": {
|
|
@@ -97,7 +98,7 @@
|
|
|
97
98
|
"test:watch": "yarn test:ts-jest --watch",
|
|
98
99
|
"test": "yarn test:ts-jest",
|
|
99
100
|
"test:babel": "jest --coverage --config ./configs/babel-jest.config.json",
|
|
100
|
-
"test:bun": "bun test",
|
|
101
|
+
"test:bun": "bun test src/",
|
|
101
102
|
"test:vitest": "npx vitest --config ./configs/vitest.config.ts",
|
|
102
103
|
"test:ts-jest": "npx jest --config ./configs/ts-jest.config.json",
|
|
103
104
|
"test:swc": "npx jest --config ./configs/swc-jest.config.json",
|
|
@@ -114,6 +115,5 @@
|
|
|
114
115
|
"npm-funding": true
|
|
115
116
|
}
|
|
116
117
|
},
|
|
117
|
-
"types": "./index.d.ts"
|
|
118
|
-
"dependencies": {}
|
|
118
|
+
"types": "./index.d.ts"
|
|
119
119
|
}
|