zod 3.20.2 → 3.20.6
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 +173 -110
- package/lib/ZodError.d.ts +5 -4
- package/lib/__tests__/Mocker.d.ts +17 -0
- package/lib/__tests__/Mocker.js +57 -0
- package/lib/benchmarks/primitives.js +59 -1
- package/lib/helpers/parseUtil.js +1 -1
- package/lib/index.mjs +226 -85
- package/lib/index.umd.js +226 -85
- package/lib/types.d.ts +57 -41
- package/lib/types.js +225 -84
- package/package.json +3 -2
package/lib/types.js
CHANGED
|
@@ -228,28 +228,29 @@ class ZodType {
|
|
|
228
228
|
return this._refinement(refinement);
|
|
229
229
|
}
|
|
230
230
|
optional() {
|
|
231
|
-
return ZodOptional.create(this);
|
|
231
|
+
return ZodOptional.create(this, this._def);
|
|
232
232
|
}
|
|
233
233
|
nullable() {
|
|
234
|
-
return ZodNullable.create(this);
|
|
234
|
+
return ZodNullable.create(this, this._def);
|
|
235
235
|
}
|
|
236
236
|
nullish() {
|
|
237
|
-
return this.
|
|
237
|
+
return this.nullable().optional();
|
|
238
238
|
}
|
|
239
239
|
array() {
|
|
240
|
-
return ZodArray.create(this);
|
|
240
|
+
return ZodArray.create(this, this._def);
|
|
241
241
|
}
|
|
242
242
|
promise() {
|
|
243
|
-
return ZodPromise.create(this);
|
|
243
|
+
return ZodPromise.create(this, this._def);
|
|
244
244
|
}
|
|
245
245
|
or(option) {
|
|
246
|
-
return ZodUnion.create([this, option]);
|
|
246
|
+
return ZodUnion.create([this, option], this._def);
|
|
247
247
|
}
|
|
248
248
|
and(incoming) {
|
|
249
|
-
return ZodIntersection.create(this, incoming);
|
|
249
|
+
return ZodIntersection.create(this, incoming, this._def);
|
|
250
250
|
}
|
|
251
251
|
transform(transform) {
|
|
252
252
|
return new ZodEffects({
|
|
253
|
+
...processCreateParams(this._def),
|
|
253
254
|
schema: this,
|
|
254
255
|
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
255
256
|
effect: { type: "transform", transform },
|
|
@@ -258,6 +259,7 @@ class ZodType {
|
|
|
258
259
|
default(def) {
|
|
259
260
|
const defaultValueFunc = typeof def === "function" ? def : () => def;
|
|
260
261
|
return new ZodDefault({
|
|
262
|
+
...processCreateParams(this._def),
|
|
261
263
|
innerType: this,
|
|
262
264
|
defaultValue: defaultValueFunc,
|
|
263
265
|
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
@@ -267,14 +269,15 @@ class ZodType {
|
|
|
267
269
|
return new ZodBranded({
|
|
268
270
|
typeName: ZodFirstPartyTypeKind.ZodBranded,
|
|
269
271
|
type: this,
|
|
270
|
-
...processCreateParams(
|
|
272
|
+
...processCreateParams(this._def),
|
|
271
273
|
});
|
|
272
274
|
}
|
|
273
275
|
catch(def) {
|
|
274
|
-
const
|
|
276
|
+
const catchValueFunc = typeof def === "function" ? def : () => def;
|
|
275
277
|
return new ZodCatch({
|
|
278
|
+
...processCreateParams(this._def),
|
|
276
279
|
innerType: this,
|
|
277
|
-
|
|
280
|
+
catchValue: catchValueFunc,
|
|
278
281
|
typeName: ZodFirstPartyTypeKind.ZodCatch,
|
|
279
282
|
});
|
|
280
283
|
}
|
|
@@ -299,12 +302,15 @@ exports.ZodType = ZodType;
|
|
|
299
302
|
exports.Schema = ZodType;
|
|
300
303
|
exports.ZodSchema = ZodType;
|
|
301
304
|
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
305
|
+
const cuid2Regex = /^[a-z][a-z0-9]*$/;
|
|
302
306
|
const uuidRegex = /^([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;
|
|
303
307
|
// from https://stackoverflow.com/a/46181/1550155
|
|
304
308
|
// old version: too slow, didn't support unicode
|
|
305
309
|
// 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;
|
|
310
|
+
//old email regex
|
|
311
|
+
// const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@((?!-)([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{1,})[^-<>()[\].,;:\s@"]$/i;
|
|
306
312
|
// eslint-disable-next-line
|
|
307
|
-
const emailRegex = /^(([^<>()[\]
|
|
313
|
+
const emailRegex = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|([^-]([a-zA-Z0-9-]*\.)+[a-zA-Z]{2,}))$/;
|
|
308
314
|
// interface IsDateStringOptions extends StringDateOptions {
|
|
309
315
|
/**
|
|
310
316
|
* Match any configuration
|
|
@@ -315,7 +321,7 @@ const emailRegex = /^(([^<>()[\]\.,;:\s@\"]+(\.[^<>()[\]\.,;:\s@\"]+)*)|(\".+\")
|
|
|
315
321
|
const datetimeRegex = (args) => {
|
|
316
322
|
if (args.precision) {
|
|
317
323
|
if (args.offset) {
|
|
318
|
-
return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${args.precision}}(([+-]\\d{2}
|
|
324
|
+
return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${args.precision}}(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
|
|
319
325
|
}
|
|
320
326
|
else {
|
|
321
327
|
return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}\\.\\d{${args.precision}}Z$`);
|
|
@@ -323,7 +329,7 @@ const datetimeRegex = (args) => {
|
|
|
323
329
|
}
|
|
324
330
|
else if (args.precision === 0) {
|
|
325
331
|
if (args.offset) {
|
|
326
|
-
return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(([+-]\\d{2}
|
|
332
|
+
return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
|
|
327
333
|
}
|
|
328
334
|
else {
|
|
329
335
|
return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}Z$`);
|
|
@@ -331,7 +337,7 @@ const datetimeRegex = (args) => {
|
|
|
331
337
|
}
|
|
332
338
|
else {
|
|
333
339
|
if (args.offset) {
|
|
334
|
-
return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(([+-]\\d{2}
|
|
340
|
+
return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(([+-]\\d{2}(:?\\d{2})?)|Z)$`);
|
|
335
341
|
}
|
|
336
342
|
else {
|
|
337
343
|
return new RegExp(`^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?Z$`);
|
|
@@ -464,6 +470,17 @@ class ZodString extends ZodType {
|
|
|
464
470
|
status.dirty();
|
|
465
471
|
}
|
|
466
472
|
}
|
|
473
|
+
else if (check.kind === "cuid2") {
|
|
474
|
+
if (!cuid2Regex.test(input.data)) {
|
|
475
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
476
|
+
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
477
|
+
validation: "cuid2",
|
|
478
|
+
code: ZodError_1.ZodIssueCode.invalid_string,
|
|
479
|
+
message: check.message,
|
|
480
|
+
});
|
|
481
|
+
status.dirty();
|
|
482
|
+
}
|
|
483
|
+
}
|
|
467
484
|
else if (check.kind === "url") {
|
|
468
485
|
try {
|
|
469
486
|
new URL(input.data);
|
|
@@ -552,6 +569,9 @@ class ZodString extends ZodType {
|
|
|
552
569
|
cuid(message) {
|
|
553
570
|
return this._addCheck({ kind: "cuid", ...errorUtil_1.errorUtil.errToObj(message) });
|
|
554
571
|
}
|
|
572
|
+
cuid2(message) {
|
|
573
|
+
return this._addCheck({ kind: "cuid2", ...errorUtil_1.errorUtil.errToObj(message) });
|
|
574
|
+
}
|
|
555
575
|
datetime(options) {
|
|
556
576
|
var _a;
|
|
557
577
|
if (typeof options === "string") {
|
|
@@ -626,6 +646,9 @@ class ZodString extends ZodType {
|
|
|
626
646
|
get isCUID() {
|
|
627
647
|
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
628
648
|
}
|
|
649
|
+
get isCUID2() {
|
|
650
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
|
|
651
|
+
}
|
|
629
652
|
get minLength() {
|
|
630
653
|
let min = null;
|
|
631
654
|
for (const ch of this._def.checks) {
|
|
@@ -867,7 +890,27 @@ class ZodNumber extends ZodType {
|
|
|
867
890
|
return max;
|
|
868
891
|
}
|
|
869
892
|
get isInt() {
|
|
870
|
-
return !!this._def.checks.find((ch) => ch.kind === "int"
|
|
893
|
+
return !!this._def.checks.find((ch) => ch.kind === "int" ||
|
|
894
|
+
(ch.kind === "multipleOf" && util_1.util.isInteger(ch.value)));
|
|
895
|
+
}
|
|
896
|
+
get isFinite() {
|
|
897
|
+
let max = null, min = null;
|
|
898
|
+
for (const ch of this._def.checks) {
|
|
899
|
+
if (ch.kind === "finite" ||
|
|
900
|
+
ch.kind === "int" ||
|
|
901
|
+
ch.kind === "multipleOf") {
|
|
902
|
+
return true;
|
|
903
|
+
}
|
|
904
|
+
else if (ch.kind === "min") {
|
|
905
|
+
if (min === null || ch.value > min)
|
|
906
|
+
min = ch.value;
|
|
907
|
+
}
|
|
908
|
+
else if (ch.kind === "max") {
|
|
909
|
+
if (max === null || ch.value < max)
|
|
910
|
+
max = ch.value;
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
return Number.isFinite(min) && Number.isFinite(max);
|
|
871
914
|
}
|
|
872
915
|
}
|
|
873
916
|
exports.ZodNumber = ZodNumber;
|
|
@@ -1239,13 +1282,13 @@ class ZodArray extends ZodType {
|
|
|
1239
1282
|
}
|
|
1240
1283
|
}
|
|
1241
1284
|
if (ctx.common.async) {
|
|
1242
|
-
return Promise.all(ctx.data.map((item, i) => {
|
|
1285
|
+
return Promise.all([...ctx.data].map((item, i) => {
|
|
1243
1286
|
return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
1244
1287
|
})).then((result) => {
|
|
1245
1288
|
return parseUtil_1.ParseStatus.mergeArray(status, result);
|
|
1246
1289
|
});
|
|
1247
1290
|
}
|
|
1248
|
-
const result = ctx.data.map((item, i) => {
|
|
1291
|
+
const result = [...ctx.data].map((item, i) => {
|
|
1249
1292
|
return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
1250
1293
|
});
|
|
1251
1294
|
return parseUtil_1.ParseStatus.mergeArray(status, result);
|
|
@@ -1302,15 +1345,6 @@ var objectUtil;
|
|
|
1302
1345
|
};
|
|
1303
1346
|
};
|
|
1304
1347
|
})(objectUtil = exports.objectUtil || (exports.objectUtil = {}));
|
|
1305
|
-
const AugmentFactory = (def) => (augmentation) => {
|
|
1306
|
-
return new ZodObject({
|
|
1307
|
-
...def,
|
|
1308
|
-
shape: () => ({
|
|
1309
|
-
...def.shape(),
|
|
1310
|
-
...augmentation,
|
|
1311
|
-
}),
|
|
1312
|
-
});
|
|
1313
|
-
};
|
|
1314
1348
|
function deepPartialify(schema) {
|
|
1315
1349
|
if (schema instanceof ZodObject) {
|
|
1316
1350
|
const newShape = {};
|
|
@@ -1348,8 +1382,43 @@ class ZodObject extends ZodType {
|
|
|
1348
1382
|
* If you want to pass through unknown properties, use `.passthrough()` instead.
|
|
1349
1383
|
*/
|
|
1350
1384
|
this.nonstrict = this.passthrough;
|
|
1351
|
-
|
|
1352
|
-
|
|
1385
|
+
// extend<
|
|
1386
|
+
// Augmentation extends ZodRawShape,
|
|
1387
|
+
// NewOutput extends util.flatten<{
|
|
1388
|
+
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
1389
|
+
// ? Augmentation[k]["_output"]
|
|
1390
|
+
// : k extends keyof Output
|
|
1391
|
+
// ? Output[k]
|
|
1392
|
+
// : never;
|
|
1393
|
+
// }>,
|
|
1394
|
+
// NewInput extends util.flatten<{
|
|
1395
|
+
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
1396
|
+
// ? Augmentation[k]["_input"]
|
|
1397
|
+
// : k extends keyof Input
|
|
1398
|
+
// ? Input[k]
|
|
1399
|
+
// : never;
|
|
1400
|
+
// }>
|
|
1401
|
+
// >(
|
|
1402
|
+
// augmentation: Augmentation
|
|
1403
|
+
// ): ZodObject<
|
|
1404
|
+
// extendShape<T, Augmentation>,
|
|
1405
|
+
// UnknownKeys,
|
|
1406
|
+
// Catchall,
|
|
1407
|
+
// NewOutput,
|
|
1408
|
+
// NewInput
|
|
1409
|
+
// > {
|
|
1410
|
+
// return new ZodObject({
|
|
1411
|
+
// ...this._def,
|
|
1412
|
+
// shape: () => ({
|
|
1413
|
+
// ...this._def.shape(),
|
|
1414
|
+
// ...augmentation,
|
|
1415
|
+
// }),
|
|
1416
|
+
// }) as any;
|
|
1417
|
+
// }
|
|
1418
|
+
/**
|
|
1419
|
+
* @deprecated Use `.extend` instead
|
|
1420
|
+
* */
|
|
1421
|
+
this.augment = this.extend;
|
|
1353
1422
|
}
|
|
1354
1423
|
_getCached() {
|
|
1355
1424
|
if (this._cached !== null)
|
|
@@ -1487,8 +1556,31 @@ class ZodObject extends ZodType {
|
|
|
1487
1556
|
unknownKeys: "passthrough",
|
|
1488
1557
|
});
|
|
1489
1558
|
}
|
|
1490
|
-
|
|
1491
|
-
|
|
1559
|
+
// const AugmentFactory =
|
|
1560
|
+
// <Def extends ZodObjectDef>(def: Def) =>
|
|
1561
|
+
// <Augmentation extends ZodRawShape>(
|
|
1562
|
+
// augmentation: Augmentation
|
|
1563
|
+
// ): ZodObject<
|
|
1564
|
+
// extendShape<ReturnType<Def["shape"]>, Augmentation>,
|
|
1565
|
+
// Def["unknownKeys"],
|
|
1566
|
+
// Def["catchall"]
|
|
1567
|
+
// > => {
|
|
1568
|
+
// return new ZodObject({
|
|
1569
|
+
// ...def,
|
|
1570
|
+
// shape: () => ({
|
|
1571
|
+
// ...def.shape(),
|
|
1572
|
+
// ...augmentation,
|
|
1573
|
+
// }),
|
|
1574
|
+
// }) as any;
|
|
1575
|
+
// };
|
|
1576
|
+
extend(augmentation) {
|
|
1577
|
+
return new ZodObject({
|
|
1578
|
+
...this._def,
|
|
1579
|
+
shape: () => ({
|
|
1580
|
+
...this._def.shape(),
|
|
1581
|
+
...augmentation,
|
|
1582
|
+
}),
|
|
1583
|
+
});
|
|
1492
1584
|
}
|
|
1493
1585
|
/**
|
|
1494
1586
|
* Prior to zod@1.0.12 there was a bug in the
|
|
@@ -1496,10 +1588,6 @@ class ZodObject extends ZodType {
|
|
|
1496
1588
|
* upgrade if you are experiencing issues.
|
|
1497
1589
|
*/
|
|
1498
1590
|
merge(merging) {
|
|
1499
|
-
// const mergedShape = objectUtil.mergeShapes(
|
|
1500
|
-
// this._def.shape(),
|
|
1501
|
-
// merging._def.shape()
|
|
1502
|
-
// );
|
|
1503
1591
|
const merged = new ZodObject({
|
|
1504
1592
|
unknownKeys: merging._def.unknownKeys,
|
|
1505
1593
|
catchall: merging._def.catchall,
|
|
@@ -1508,6 +1596,65 @@ class ZodObject extends ZodType {
|
|
|
1508
1596
|
});
|
|
1509
1597
|
return merged;
|
|
1510
1598
|
}
|
|
1599
|
+
// merge<
|
|
1600
|
+
// Incoming extends AnyZodObject,
|
|
1601
|
+
// Augmentation extends Incoming["shape"],
|
|
1602
|
+
// NewOutput extends {
|
|
1603
|
+
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
1604
|
+
// ? Augmentation[k]["_output"]
|
|
1605
|
+
// : k extends keyof Output
|
|
1606
|
+
// ? Output[k]
|
|
1607
|
+
// : never;
|
|
1608
|
+
// },
|
|
1609
|
+
// NewInput extends {
|
|
1610
|
+
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
1611
|
+
// ? Augmentation[k]["_input"]
|
|
1612
|
+
// : k extends keyof Input
|
|
1613
|
+
// ? Input[k]
|
|
1614
|
+
// : never;
|
|
1615
|
+
// }
|
|
1616
|
+
// >(
|
|
1617
|
+
// merging: Incoming
|
|
1618
|
+
// ): ZodObject<
|
|
1619
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
1620
|
+
// Incoming["_def"]["unknownKeys"],
|
|
1621
|
+
// Incoming["_def"]["catchall"],
|
|
1622
|
+
// NewOutput,
|
|
1623
|
+
// NewInput
|
|
1624
|
+
// > {
|
|
1625
|
+
// const merged: any = new ZodObject({
|
|
1626
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
1627
|
+
// catchall: merging._def.catchall,
|
|
1628
|
+
// shape: () =>
|
|
1629
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
1630
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
1631
|
+
// }) as any;
|
|
1632
|
+
// return merged;
|
|
1633
|
+
// }
|
|
1634
|
+
setKey(key, schema) {
|
|
1635
|
+
return this.augment({ [key]: schema });
|
|
1636
|
+
}
|
|
1637
|
+
// merge<Incoming extends AnyZodObject>(
|
|
1638
|
+
// merging: Incoming
|
|
1639
|
+
// ): //ZodObject<T & Incoming["_shape"], UnknownKeys, Catchall> = (merging) => {
|
|
1640
|
+
// ZodObject<
|
|
1641
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
1642
|
+
// Incoming["_def"]["unknownKeys"],
|
|
1643
|
+
// Incoming["_def"]["catchall"]
|
|
1644
|
+
// > {
|
|
1645
|
+
// // const mergedShape = objectUtil.mergeShapes(
|
|
1646
|
+
// // this._def.shape(),
|
|
1647
|
+
// // merging._def.shape()
|
|
1648
|
+
// // );
|
|
1649
|
+
// const merged: any = new ZodObject({
|
|
1650
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
1651
|
+
// catchall: merging._def.catchall,
|
|
1652
|
+
// shape: () =>
|
|
1653
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
1654
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
1655
|
+
// }) as any;
|
|
1656
|
+
// return merged;
|
|
1657
|
+
// }
|
|
1511
1658
|
catchall(index) {
|
|
1512
1659
|
return new ZodObject({
|
|
1513
1660
|
...this._def,
|
|
@@ -1516,10 +1663,10 @@ class ZodObject extends ZodType {
|
|
|
1516
1663
|
}
|
|
1517
1664
|
pick(mask) {
|
|
1518
1665
|
const shape = {};
|
|
1519
|
-
util_1.util.objectKeys(mask).
|
|
1520
|
-
|
|
1521
|
-
if (this.shape[key])
|
|
1666
|
+
util_1.util.objectKeys(mask).forEach((key) => {
|
|
1667
|
+
if (mask[key] && this.shape[key]) {
|
|
1522
1668
|
shape[key] = this.shape[key];
|
|
1669
|
+
}
|
|
1523
1670
|
});
|
|
1524
1671
|
return new ZodObject({
|
|
1525
1672
|
...this._def,
|
|
@@ -1528,8 +1675,8 @@ class ZodObject extends ZodType {
|
|
|
1528
1675
|
}
|
|
1529
1676
|
omit(mask) {
|
|
1530
1677
|
const shape = {};
|
|
1531
|
-
util_1.util.objectKeys(this.shape).
|
|
1532
|
-
if (
|
|
1678
|
+
util_1.util.objectKeys(this.shape).forEach((key) => {
|
|
1679
|
+
if (!mask[key]) {
|
|
1533
1680
|
shape[key] = this.shape[key];
|
|
1534
1681
|
}
|
|
1535
1682
|
});
|
|
@@ -1543,26 +1690,15 @@ class ZodObject extends ZodType {
|
|
|
1543
1690
|
}
|
|
1544
1691
|
partial(mask) {
|
|
1545
1692
|
const newShape = {};
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
newShape[key] = this.shape[key].optional();
|
|
1553
|
-
}
|
|
1554
|
-
});
|
|
1555
|
-
return new ZodObject({
|
|
1556
|
-
...this._def,
|
|
1557
|
-
shape: () => newShape,
|
|
1558
|
-
});
|
|
1559
|
-
}
|
|
1560
|
-
else {
|
|
1561
|
-
for (const key in this.shape) {
|
|
1562
|
-
const fieldSchema = this.shape[key];
|
|
1693
|
+
util_1.util.objectKeys(this.shape).forEach((key) => {
|
|
1694
|
+
const fieldSchema = this.shape[key];
|
|
1695
|
+
if (mask && !mask[key]) {
|
|
1696
|
+
newShape[key] = fieldSchema;
|
|
1697
|
+
}
|
|
1698
|
+
else {
|
|
1563
1699
|
newShape[key] = fieldSchema.optional();
|
|
1564
1700
|
}
|
|
1565
|
-
}
|
|
1701
|
+
});
|
|
1566
1702
|
return new ZodObject({
|
|
1567
1703
|
...this._def,
|
|
1568
1704
|
shape: () => newShape,
|
|
@@ -1570,23 +1706,11 @@ class ZodObject extends ZodType {
|
|
|
1570
1706
|
}
|
|
1571
1707
|
required(mask) {
|
|
1572
1708
|
const newShape = {};
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
else {
|
|
1579
|
-
const fieldSchema = this.shape[key];
|
|
1580
|
-
let newField = fieldSchema;
|
|
1581
|
-
while (newField instanceof ZodOptional) {
|
|
1582
|
-
newField = newField._def.innerType;
|
|
1583
|
-
}
|
|
1584
|
-
newShape[key] = newField;
|
|
1585
|
-
}
|
|
1586
|
-
});
|
|
1587
|
-
}
|
|
1588
|
-
else {
|
|
1589
|
-
for (const key in this.shape) {
|
|
1709
|
+
util_1.util.objectKeys(this.shape).forEach((key) => {
|
|
1710
|
+
if (mask && !mask[key]) {
|
|
1711
|
+
newShape[key] = this.shape[key];
|
|
1712
|
+
}
|
|
1713
|
+
else {
|
|
1590
1714
|
const fieldSchema = this.shape[key];
|
|
1591
1715
|
let newField = fieldSchema;
|
|
1592
1716
|
while (newField instanceof ZodOptional) {
|
|
@@ -1594,7 +1718,7 @@ class ZodObject extends ZodType {
|
|
|
1594
1718
|
}
|
|
1595
1719
|
newShape[key] = newField;
|
|
1596
1720
|
}
|
|
1597
|
-
}
|
|
1721
|
+
});
|
|
1598
1722
|
return new ZodObject({
|
|
1599
1723
|
...this._def,
|
|
1600
1724
|
shape: () => newShape,
|
|
@@ -1979,7 +2103,7 @@ class ZodTuple extends ZodType {
|
|
|
1979
2103
|
});
|
|
1980
2104
|
status.dirty();
|
|
1981
2105
|
}
|
|
1982
|
-
const items = ctx.data
|
|
2106
|
+
const items = [...ctx.data]
|
|
1983
2107
|
.map((item, itemIndex) => {
|
|
1984
2108
|
const schema = this._def.items[itemIndex] || this._def.rest;
|
|
1985
2109
|
if (!schema)
|
|
@@ -2365,6 +2489,7 @@ class ZodLiteral extends ZodType {
|
|
|
2365
2489
|
if (input.data !== this._def.value) {
|
|
2366
2490
|
const ctx = this._getOrReturnCtx(input);
|
|
2367
2491
|
(0, parseUtil_1.addIssueToContext)(ctx, {
|
|
2492
|
+
received: ctx.data,
|
|
2368
2493
|
code: ZodError_1.ZodIssueCode.invalid_literal,
|
|
2369
2494
|
expected: this._def.value,
|
|
2370
2495
|
});
|
|
@@ -2439,6 +2564,12 @@ class ZodEnum extends ZodType {
|
|
|
2439
2564
|
}
|
|
2440
2565
|
return enumValues;
|
|
2441
2566
|
}
|
|
2567
|
+
extract(values) {
|
|
2568
|
+
return ZodEnum.create(values);
|
|
2569
|
+
}
|
|
2570
|
+
exclude(values) {
|
|
2571
|
+
return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)));
|
|
2572
|
+
}
|
|
2442
2573
|
}
|
|
2443
2574
|
exports.ZodEnum = ZodEnum;
|
|
2444
2575
|
ZodEnum.create = createZodEnum;
|
|
@@ -2480,6 +2611,9 @@ ZodNativeEnum.create = (values, params) => {
|
|
|
2480
2611
|
});
|
|
2481
2612
|
};
|
|
2482
2613
|
class ZodPromise extends ZodType {
|
|
2614
|
+
unwrap() {
|
|
2615
|
+
return this._def.type;
|
|
2616
|
+
}
|
|
2483
2617
|
_parse(input) {
|
|
2484
2618
|
const { ctx } = this._processInputParams(input);
|
|
2485
2619
|
if (ctx.parsedType !== util_1.ZodParsedType.promise &&
|
|
@@ -2725,24 +2859,30 @@ class ZodCatch extends ZodType {
|
|
|
2725
2859
|
const result = this._def.innerType._parse({
|
|
2726
2860
|
data: ctx.data,
|
|
2727
2861
|
path: ctx.path,
|
|
2728
|
-
parent:
|
|
2862
|
+
parent: {
|
|
2863
|
+
...ctx,
|
|
2864
|
+
common: {
|
|
2865
|
+
...ctx.common,
|
|
2866
|
+
issues: [], // don't collect issues from inner type
|
|
2867
|
+
},
|
|
2868
|
+
},
|
|
2729
2869
|
});
|
|
2730
2870
|
if ((0, parseUtil_1.isAsync)(result)) {
|
|
2731
2871
|
return result.then((result) => {
|
|
2732
2872
|
return {
|
|
2733
2873
|
status: "valid",
|
|
2734
|
-
value: result.status === "valid" ? result.value : this._def.
|
|
2874
|
+
value: result.status === "valid" ? result.value : this._def.catchValue(),
|
|
2735
2875
|
};
|
|
2736
2876
|
});
|
|
2737
2877
|
}
|
|
2738
2878
|
else {
|
|
2739
2879
|
return {
|
|
2740
2880
|
status: "valid",
|
|
2741
|
-
value: result.status === "valid" ? result.value : this._def.
|
|
2881
|
+
value: result.status === "valid" ? result.value : this._def.catchValue(),
|
|
2742
2882
|
};
|
|
2743
2883
|
}
|
|
2744
2884
|
}
|
|
2745
|
-
|
|
2885
|
+
removeCatch() {
|
|
2746
2886
|
return this._def.innerType;
|
|
2747
2887
|
}
|
|
2748
2888
|
}
|
|
@@ -2751,9 +2891,7 @@ ZodCatch.create = (type, params) => {
|
|
|
2751
2891
|
return new ZodCatch({
|
|
2752
2892
|
innerType: type,
|
|
2753
2893
|
typeName: ZodFirstPartyTypeKind.ZodCatch,
|
|
2754
|
-
|
|
2755
|
-
? params.default
|
|
2756
|
-
: () => params.default,
|
|
2894
|
+
catchValue: typeof params.catch === "function" ? params.catch : () => params.catch,
|
|
2757
2895
|
...processCreateParams(params),
|
|
2758
2896
|
});
|
|
2759
2897
|
};
|
|
@@ -2996,7 +3134,10 @@ exports.oboolean = oboolean;
|
|
|
2996
3134
|
exports.coerce = {
|
|
2997
3135
|
string: ((arg) => ZodString.create({ ...arg, coerce: true })),
|
|
2998
3136
|
number: ((arg) => ZodNumber.create({ ...arg, coerce: true })),
|
|
2999
|
-
boolean: ((arg) => ZodBoolean.create({
|
|
3137
|
+
boolean: ((arg) => ZodBoolean.create({
|
|
3138
|
+
...arg,
|
|
3139
|
+
coerce: true,
|
|
3140
|
+
})),
|
|
3000
3141
|
bigint: ((arg) => ZodBigInt.create({ ...arg, coerce: true })),
|
|
3001
3142
|
date: ((arg) => ZodDate.create({ ...arg, coerce: true })),
|
|
3002
3143
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zod",
|
|
3
|
-
"version": "3.20.
|
|
3
|
+
"version": "3.20.6",
|
|
4
4
|
"description": "TypeScript-first schema declaration and validation library with static type inference",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./index.d.ts",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"bugs": {
|
|
30
30
|
"url": "https://github.com/colinhacks/zod/issues"
|
|
31
31
|
},
|
|
32
|
-
"homepage": "https://
|
|
32
|
+
"homepage": "https://zod.dev",
|
|
33
33
|
"funding": "https://github.com/sponsors/colinhacks",
|
|
34
34
|
"support": {
|
|
35
35
|
"backing": {
|
|
@@ -57,6 +57,7 @@
|
|
|
57
57
|
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
58
58
|
"build:types": "tsc -p tsconfig.types.json",
|
|
59
59
|
"rollup": "rollup --config rollup.config.js",
|
|
60
|
+
"test:watch": "jest --watch",
|
|
60
61
|
"test": "jest --coverage",
|
|
61
62
|
"test:deno": "cd deno && deno test",
|
|
62
63
|
"prepublishOnly": "npm run test && npm run build && npm run build:deno",
|