zod 3.21.0 → 3.21.2
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 +86 -10
- package/lib/ZodError.d.ts +4 -1
- package/lib/ZodError.js +0 -8
- package/lib/helpers/parseUtil.js +1 -1
- package/lib/helpers/partialUtil.d.ts +4 -4
- package/lib/helpers/util.d.ts +25 -4
- package/lib/helpers/util.js +13 -4
- package/lib/index.mjs +84 -23
- package/lib/index.umd.js +84 -23
- package/lib/locales/en.js +7 -1
- package/lib/types.d.ts +40 -76
- package/lib/types.js +75 -209
- package/package.json +55 -54
package/README.md
CHANGED
|
@@ -118,6 +118,7 @@
|
|
|
118
118
|
- [.or](#or)
|
|
119
119
|
- [.and](#and)
|
|
120
120
|
- [.brand](#brand)
|
|
121
|
+
- [.pipe](#pipe)
|
|
121
122
|
- [Guides and concepts](#guides-and-concepts)
|
|
122
123
|
- [Type inference](#type-inference)
|
|
123
124
|
- [Writing generic functions](#writing-generic-functions)
|
|
@@ -205,6 +206,26 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
|
|
|
205
206
|
<a href="https://proxy.com">proxy.com</a>
|
|
206
207
|
</td>
|
|
207
208
|
</tr>
|
|
209
|
+
<tr>
|
|
210
|
+
<td align="center">
|
|
211
|
+
<a href="https://trigger.dev/">
|
|
212
|
+
<img src="https://avatars.githubusercontent.com/u/95297378?s=200&v=4" width="200px;" alt="Trigger.dev logo" />
|
|
213
|
+
</a>
|
|
214
|
+
<br />
|
|
215
|
+
<b>Trigger.dev</b>
|
|
216
|
+
<br />
|
|
217
|
+
<a href="https://trigger.dev">trigger.dev</a>
|
|
218
|
+
</td>
|
|
219
|
+
<!-- <td align="center">
|
|
220
|
+
<a href="https://proxy.com/">
|
|
221
|
+
<img src="https://avatars.githubusercontent.com/u/14321439?s=200&v=4" width="200px;" alt="Proxy logo" />
|
|
222
|
+
</a>
|
|
223
|
+
<br />
|
|
224
|
+
<b>Proxy</b>
|
|
225
|
+
<br />
|
|
226
|
+
<a href="https://proxy.com">proxy.com</a>
|
|
227
|
+
</td> -->
|
|
228
|
+
</tr>
|
|
208
229
|
</table>
|
|
209
230
|
|
|
210
231
|
#### Silver
|
|
@@ -308,7 +329,7 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
|
|
|
308
329
|
</td>
|
|
309
330
|
</tr>
|
|
310
331
|
<tr>
|
|
311
|
-
|
|
332
|
+
<td align="center">
|
|
312
333
|
<a href="https://fungible.systems/">
|
|
313
334
|
<img src="https://avatars.githubusercontent.com/u/80220121?s=200&v=4" width="100px;" alt="Fungible Systems logo"/>
|
|
314
335
|
</a>
|
|
@@ -340,6 +361,39 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
|
|
|
340
361
|
<br />
|
|
341
362
|
</td>
|
|
342
363
|
</tr>
|
|
364
|
+
<tr>
|
|
365
|
+
<td align="center">
|
|
366
|
+
<a href="https://learnwithjason.dev">
|
|
367
|
+
<img src="https://avatars.githubusercontent.com/u/66575486?s=200&v=4" width="100px;" alt="Learn with Jason logo"/>
|
|
368
|
+
</a>
|
|
369
|
+
<br />
|
|
370
|
+
<b>Jason Lengstorf</b>
|
|
371
|
+
<br/>
|
|
372
|
+
<a href="https://learnwithjason.dev/">learnwithjason.dev</a>
|
|
373
|
+
<br />
|
|
374
|
+
</td>
|
|
375
|
+
<td align="center">
|
|
376
|
+
<a href="https://ill.inc/">
|
|
377
|
+
<img src="https://avatars.githubusercontent.com/u/89107581?s=200&v=4" width="100px;" alt="Global Illumination"/>
|
|
378
|
+
</a>
|
|
379
|
+
<br />
|
|
380
|
+
<b>Global Illumination, Inc.</b>
|
|
381
|
+
<br/>
|
|
382
|
+
<a href="https://ill.inc/">ill.inc</a>
|
|
383
|
+
<br />
|
|
384
|
+
</td>
|
|
385
|
+
<!-- <td align="center">
|
|
386
|
+
<a href="https://www.avanawallet.com/">
|
|
387
|
+
<img src="https://avatars.githubusercontent.com/u/105452197?s=200&v=4" width="100px;" alt="Avana Wallet logo"/>
|
|
388
|
+
</a>
|
|
389
|
+
<br />
|
|
390
|
+
<b>Avana Wallet</b>
|
|
391
|
+
<br/>
|
|
392
|
+
<a href="https://www.avanawallet.com/">avanawallet.com</a><br/>
|
|
393
|
+
<span>Solana non-custodial wallet</span>
|
|
394
|
+
<br />
|
|
395
|
+
</td> -->
|
|
396
|
+
</tr>
|
|
343
397
|
</table>
|
|
344
398
|
|
|
345
399
|
### Ecosystem
|
|
@@ -586,6 +640,7 @@ tuna.value; // "tuna"
|
|
|
586
640
|
Zod includes a handful of string-specific validations.
|
|
587
641
|
|
|
588
642
|
```ts
|
|
643
|
+
// validations
|
|
589
644
|
z.string().max(5);
|
|
590
645
|
z.string().min(5);
|
|
591
646
|
z.string().length(5);
|
|
@@ -595,16 +650,19 @@ z.string().emoji();
|
|
|
595
650
|
z.string().uuid();
|
|
596
651
|
z.string().cuid();
|
|
597
652
|
z.string().cuid2();
|
|
653
|
+
z.string().ulid();
|
|
598
654
|
z.string().regex(regex);
|
|
655
|
+
z.string().includes(string);
|
|
599
656
|
z.string().startsWith(string);
|
|
600
657
|
z.string().endsWith(string);
|
|
601
|
-
z.string().trim(); // trim whitespace
|
|
602
658
|
z.string().datetime(); // defaults to UTC, see below for options
|
|
603
659
|
z.string().ip(); // defaults to IPv4 and IPv6, see below for options
|
|
604
|
-
```
|
|
605
660
|
|
|
606
|
-
|
|
607
|
-
|
|
661
|
+
// transformations
|
|
662
|
+
z.string().trim(); // trim whitespace
|
|
663
|
+
z.string().toLowerCase(); // toLowerCase
|
|
664
|
+
z.string().toUpperCase(); // toUpperCase
|
|
665
|
+
```
|
|
608
666
|
|
|
609
667
|
> Check out [validator.js](https://github.com/validatorjs/validator.js) for a bunch of other useful string validation functions that can be used in conjunction with [Refinements](#refine).
|
|
610
668
|
|
|
@@ -627,13 +685,14 @@ z.string().email({ message: "Invalid email address" });
|
|
|
627
685
|
z.string().url({ message: "Invalid url" });
|
|
628
686
|
z.string().emoji({ message: "Contains non-emoji characters" });
|
|
629
687
|
z.string().uuid({ message: "Invalid UUID" });
|
|
688
|
+
z.string().includes("tuna", { message: "Must include tuna" });
|
|
630
689
|
z.string().startsWith("https://", { message: "Must provide secure URL" });
|
|
631
690
|
z.string().endsWith(".com", { message: "Only .com domains allowed" });
|
|
632
691
|
z.string().datetime({ message: "Invalid datetime string! Must be UTC." });
|
|
633
692
|
z.string().ip({ message: "Invalid IP address" });
|
|
634
693
|
```
|
|
635
694
|
|
|
636
|
-
###
|
|
695
|
+
### ISO datetimes
|
|
637
696
|
|
|
638
697
|
The `z.string().datetime()` method defaults to UTC validation: no timezone offsets with arbitrary sub-second decimal precision.
|
|
639
698
|
|
|
@@ -668,7 +727,7 @@ datetime.parse("2020-01-01T00:00:00Z"); // fail
|
|
|
668
727
|
datetime.parse("2020-01-01T00:00:00.123456Z"); // fail
|
|
669
728
|
```
|
|
670
729
|
|
|
671
|
-
### IP
|
|
730
|
+
### IP addresses
|
|
672
731
|
|
|
673
732
|
The `z.string().ip()` method by default validate IPv4 and IPv6.
|
|
674
733
|
|
|
@@ -1759,9 +1818,14 @@ This returns a `ZodEffects` instance. `ZodEffects` is a wrapper class that conta
|
|
|
1759
1818
|
You can create a Zod schema for any TypeScript type by using `z.custom()`. This is useful for creating schemas for types that are not supported by Zod out of the box, such as template string literals.
|
|
1760
1819
|
|
|
1761
1820
|
```ts
|
|
1762
|
-
const px = z.custom<`${number}px`>((val) =>
|
|
1763
|
-
px
|
|
1764
|
-
|
|
1821
|
+
const px = z.custom<`${number}px`>((val) => {
|
|
1822
|
+
return /^\d+px$/.test(val as string);
|
|
1823
|
+
});
|
|
1824
|
+
|
|
1825
|
+
type px = z.infer<typeof px>; // `${number}px`
|
|
1826
|
+
|
|
1827
|
+
px.parse("42px"); // "42px"
|
|
1828
|
+
px.parse("42vw"); // throws;
|
|
1765
1829
|
```
|
|
1766
1830
|
|
|
1767
1831
|
If you don't provide a validation function, Zod will allow any value. This can be dangerous!
|
|
@@ -2318,6 +2382,18 @@ type Cat = z.infer<typeof Cat>;
|
|
|
2318
2382
|
|
|
2319
2383
|
Note that branded types do not affect the runtime result of `.parse`. It is a static-only construct.
|
|
2320
2384
|
|
|
2385
|
+
### `.pipe()`
|
|
2386
|
+
|
|
2387
|
+
Schemas can be chained into validation "pipelines". It's useful for easily validating the result after a `.transform()`:
|
|
2388
|
+
|
|
2389
|
+
```ts
|
|
2390
|
+
z.string()
|
|
2391
|
+
.transform((val) => val.length)
|
|
2392
|
+
.pipe(z.number().min(5));
|
|
2393
|
+
```
|
|
2394
|
+
|
|
2395
|
+
The `.pipe()` method returns a `ZodPipeline` instance.
|
|
2396
|
+
|
|
2321
2397
|
## Guides and concepts
|
|
2322
2398
|
|
|
2323
2399
|
### Type inference
|
package/lib/ZodError.d.ts
CHANGED
|
@@ -70,7 +70,10 @@ export interface ZodInvalidReturnTypeIssue extends ZodIssueBase {
|
|
|
70
70
|
export interface ZodInvalidDateIssue extends ZodIssueBase {
|
|
71
71
|
code: typeof ZodIssueCode.invalid_date;
|
|
72
72
|
}
|
|
73
|
-
export declare type StringValidation = "email" | "url" | "emoji" | "uuid" | "regex" | "cuid" | "cuid2" | "datetime" | "ip" | {
|
|
73
|
+
export declare type StringValidation = "email" | "url" | "emoji" | "uuid" | "regex" | "cuid" | "cuid2" | "ulid" | "datetime" | "ip" | {
|
|
74
|
+
includes: string;
|
|
75
|
+
position?: number;
|
|
76
|
+
} | {
|
|
74
77
|
startsWith: string;
|
|
75
78
|
} | {
|
|
76
79
|
endsWith: string;
|
package/lib/ZodError.js
CHANGED
|
@@ -37,7 +37,6 @@ class ZodError extends Error {
|
|
|
37
37
|
};
|
|
38
38
|
const actualProto = new.target.prototype;
|
|
39
39
|
if (Object.setPrototypeOf) {
|
|
40
|
-
// eslint-disable-next-line ban/ban
|
|
41
40
|
Object.setPrototypeOf(this, actualProto);
|
|
42
41
|
}
|
|
43
42
|
else {
|
|
@@ -77,13 +76,6 @@ class ZodError extends Error {
|
|
|
77
76
|
const terminal = i === issue.path.length - 1;
|
|
78
77
|
if (!terminal) {
|
|
79
78
|
curr[el] = curr[el] || { _errors: [] };
|
|
80
|
-
// if (typeof el === "string") {
|
|
81
|
-
// curr[el] = curr[el] || { _errors: [] };
|
|
82
|
-
// } else if (typeof el === "number") {
|
|
83
|
-
// const errorArray: any = [];
|
|
84
|
-
// errorArray._errors = [];
|
|
85
|
-
// curr[el] = curr[el] || errorArray;
|
|
86
|
-
// }
|
|
87
79
|
}
|
|
88
80
|
else {
|
|
89
81
|
curr[el] = curr[el] || { _errors: [] };
|
package/lib/helpers/parseUtil.js
CHANGED
|
@@ -38,7 +38,7 @@ function addIssueToContext(ctx, issueData) {
|
|
|
38
38
|
ctx.common.contextualErrorMap,
|
|
39
39
|
ctx.schemaErrorMap,
|
|
40
40
|
(0, errors_1.getErrorMap)(),
|
|
41
|
-
en_1.default,
|
|
41
|
+
en_1.default,
|
|
42
42
|
].filter((x) => !!x),
|
|
43
43
|
});
|
|
44
44
|
ctx.common.issues.push(issue);
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type { ZodArray, ZodNullable, ZodObject, ZodOptional, ZodTuple, ZodTupleItems, ZodTypeAny } from "../index";
|
|
1
|
+
import type { ZodArray, ZodNullable, ZodObject, ZodOptional, ZodRawShape, ZodTuple, ZodTupleItems, ZodTypeAny } from "../index";
|
|
2
2
|
export declare namespace partialUtil {
|
|
3
|
-
type DeepPartial<T extends ZodTypeAny> = T extends ZodObject<
|
|
4
|
-
[k in keyof
|
|
5
|
-
},
|
|
3
|
+
type DeepPartial<T extends ZodTypeAny> = T extends ZodObject<ZodRawShape> ? ZodObject<{
|
|
4
|
+
[k in keyof T["shape"]]: ZodOptional<DeepPartial<T["shape"][k]>>;
|
|
5
|
+
}, T["_def"]["unknownKeys"], T["_def"]["catchall"]> : T extends ZodArray<infer Type, infer Card> ? ZodArray<DeepPartial<Type>, Card> : T extends ZodOptional<infer Type> ? ZodOptional<DeepPartial<Type>> : T extends ZodNullable<infer Type> ? ZodNullable<DeepPartial<Type>> : T extends ZodTuple<infer Items> ? {
|
|
6
6
|
[k in keyof Items]: Items[k] extends ZodTypeAny ? DeepPartial<Items[k]> : never;
|
|
7
7
|
} extends infer PI ? PI extends ZodTupleItems ? ZodTuple<PI> : never : never : T;
|
|
8
8
|
}
|
package/lib/helpers/util.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export declare namespace util {
|
|
2
2
|
type AssertEqual<T, U> = (<V>() => V extends T ? 1 : 2) extends <V>() => V extends U ? 1 : 2 ? true : false;
|
|
3
|
+
export type isAny<T> = 0 extends 1 & T ? true : false;
|
|
3
4
|
export const assertEqual: <A, B>(val: AssertEqual<A, B>) => AssertEqual<A, B>;
|
|
4
5
|
export function assertIs<T>(_arg: T): void;
|
|
5
6
|
export function assertNever(_x: never): never;
|
|
@@ -11,16 +12,36 @@ export declare namespace util {
|
|
|
11
12
|
export const objectValues: (obj: any) => any[];
|
|
12
13
|
export const objectKeys: ObjectConstructor["keys"];
|
|
13
14
|
export const find: <T>(arr: T[], checker: (arg: T) => any) => T | undefined;
|
|
14
|
-
export type identity<T> = T
|
|
15
|
-
export type flatten<T> =
|
|
16
|
-
[k in keyof T]: T[k];
|
|
17
|
-
}>;
|
|
15
|
+
export type identity<T> = objectUtil.identity<T>;
|
|
16
|
+
export type flatten<T> = objectUtil.flatten<T>;
|
|
18
17
|
export type noUndefined<T> = T extends undefined ? never : T;
|
|
19
18
|
export const isInteger: NumberConstructor["isInteger"];
|
|
20
19
|
export function joinValues<T extends any[]>(array: T, separator?: string): string;
|
|
21
20
|
export const jsonStringifyReplacer: (_: string, value: any) => any;
|
|
22
21
|
export {};
|
|
23
22
|
}
|
|
23
|
+
export declare namespace objectUtil {
|
|
24
|
+
export type MergeShapes<U, V> = {
|
|
25
|
+
[k in Exclude<keyof U, keyof V>]: U[k];
|
|
26
|
+
} & V;
|
|
27
|
+
type requiredKeys<T extends object> = {
|
|
28
|
+
[k in keyof T]: undefined extends T[k] ? never : k;
|
|
29
|
+
}[keyof T];
|
|
30
|
+
export type addQuestionMarks<T extends object, R extends keyof T = requiredKeys<T>> = Pick<T, R> & Partial<T>;
|
|
31
|
+
export type identity<T> = T;
|
|
32
|
+
export type flatten<T> = identity<{
|
|
33
|
+
[k in keyof T]: T[k];
|
|
34
|
+
}>;
|
|
35
|
+
export type noNeverKeys<T> = {
|
|
36
|
+
[k in keyof T]: [T[k]] extends [never] ? never : k;
|
|
37
|
+
}[keyof T];
|
|
38
|
+
export type noNever<T> = identity<{
|
|
39
|
+
[k in noNeverKeys<T>]: k extends keyof T ? T[k] : never;
|
|
40
|
+
}>;
|
|
41
|
+
export const mergeShapes: <U, T>(first: U, second: T) => T & U;
|
|
42
|
+
export type extendShape<A, B> = flatten<Omit<A, keyof B> & B>;
|
|
43
|
+
export {};
|
|
44
|
+
}
|
|
24
45
|
export declare const ZodParsedType: {
|
|
25
46
|
function: "function";
|
|
26
47
|
number: "number";
|
package/lib/helpers/util.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getParsedType = exports.ZodParsedType = exports.util = void 0;
|
|
3
|
+
exports.getParsedType = exports.ZodParsedType = exports.objectUtil = exports.util = void 0;
|
|
4
4
|
var util;
|
|
5
5
|
(function (util) {
|
|
6
6
|
util.assertEqual = (val) => val;
|
|
@@ -30,8 +30,8 @@ var util;
|
|
|
30
30
|
return obj[e];
|
|
31
31
|
});
|
|
32
32
|
};
|
|
33
|
-
util.objectKeys = typeof Object.keys === "function"
|
|
34
|
-
? (obj) => Object.keys(obj)
|
|
33
|
+
util.objectKeys = typeof Object.keys === "function"
|
|
34
|
+
? (obj) => Object.keys(obj)
|
|
35
35
|
: (object) => {
|
|
36
36
|
const keys = [];
|
|
37
37
|
for (const key in object) {
|
|
@@ -49,7 +49,7 @@ var util;
|
|
|
49
49
|
return undefined;
|
|
50
50
|
};
|
|
51
51
|
util.isInteger = typeof Number.isInteger === "function"
|
|
52
|
-
? (val) => Number.isInteger(val)
|
|
52
|
+
? (val) => Number.isInteger(val)
|
|
53
53
|
: (val) => typeof val === "number" && isFinite(val) && Math.floor(val) === val;
|
|
54
54
|
function joinValues(array, separator = " | ") {
|
|
55
55
|
return array
|
|
@@ -64,6 +64,15 @@ var util;
|
|
|
64
64
|
return value;
|
|
65
65
|
};
|
|
66
66
|
})(util = exports.util || (exports.util = {}));
|
|
67
|
+
var objectUtil;
|
|
68
|
+
(function (objectUtil) {
|
|
69
|
+
objectUtil.mergeShapes = (first, second) => {
|
|
70
|
+
return {
|
|
71
|
+
...first,
|
|
72
|
+
...second,
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
})(objectUtil = exports.objectUtil || (exports.objectUtil = {}));
|
|
67
76
|
exports.ZodParsedType = util.arrayToEnum([
|
|
68
77
|
"string",
|
|
69
78
|
"nan",
|
package/lib/index.mjs
CHANGED
|
@@ -61,6 +61,15 @@ var util;
|
|
|
61
61
|
return value;
|
|
62
62
|
};
|
|
63
63
|
})(util || (util = {}));
|
|
64
|
+
var objectUtil;
|
|
65
|
+
(function (objectUtil) {
|
|
66
|
+
objectUtil.mergeShapes = (first, second) => {
|
|
67
|
+
return {
|
|
68
|
+
...first,
|
|
69
|
+
...second, // second overwrites first
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
})(objectUtil || (objectUtil = {}));
|
|
64
73
|
const ZodParsedType = util.arrayToEnum([
|
|
65
74
|
"string",
|
|
66
75
|
"nan",
|
|
@@ -292,7 +301,13 @@ const errorMap = (issue, _ctx) => {
|
|
|
292
301
|
break;
|
|
293
302
|
case ZodIssueCode.invalid_string:
|
|
294
303
|
if (typeof issue.validation === "object") {
|
|
295
|
-
if ("
|
|
304
|
+
if ("includes" in issue.validation) {
|
|
305
|
+
message = `Invalid input: must include "${issue.validation.includes}"`;
|
|
306
|
+
if (typeof issue.validation.position === "number") {
|
|
307
|
+
message = `${message} at one or more positions greater than or equal to ${issue.validation.position}`;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
else if ("startsWith" in issue.validation) {
|
|
296
311
|
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
297
312
|
}
|
|
298
313
|
else if ("endsWith" in issue.validation) {
|
|
@@ -488,13 +503,22 @@ var errorUtil;
|
|
|
488
503
|
|
|
489
504
|
class ParseInputLazyPath {
|
|
490
505
|
constructor(parent, value, path, key) {
|
|
506
|
+
this._cachedPath = [];
|
|
491
507
|
this.parent = parent;
|
|
492
508
|
this.data = value;
|
|
493
509
|
this._path = path;
|
|
494
510
|
this._key = key;
|
|
495
511
|
}
|
|
496
512
|
get path() {
|
|
497
|
-
|
|
513
|
+
if (!this._cachedPath.length) {
|
|
514
|
+
if (this._key instanceof Array) {
|
|
515
|
+
this._cachedPath.push(...this._path, ...this._key);
|
|
516
|
+
}
|
|
517
|
+
else {
|
|
518
|
+
this._cachedPath.push(...this._path, this._key);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
return this._cachedPath;
|
|
498
522
|
}
|
|
499
523
|
}
|
|
500
524
|
const handleResult = (ctx, result) => {
|
|
@@ -505,8 +529,16 @@ const handleResult = (ctx, result) => {
|
|
|
505
529
|
if (!ctx.common.issues.length) {
|
|
506
530
|
throw new Error("Validation failed but no issues detected.");
|
|
507
531
|
}
|
|
508
|
-
|
|
509
|
-
|
|
532
|
+
return {
|
|
533
|
+
success: false,
|
|
534
|
+
get error() {
|
|
535
|
+
if (this._error)
|
|
536
|
+
return this._error;
|
|
537
|
+
const error = new ZodError(ctx.common.issues);
|
|
538
|
+
this._error = error;
|
|
539
|
+
return this._error;
|
|
540
|
+
},
|
|
541
|
+
};
|
|
510
542
|
}
|
|
511
543
|
};
|
|
512
544
|
function processCreateParams(params) {
|
|
@@ -779,6 +811,7 @@ class ZodType {
|
|
|
779
811
|
}
|
|
780
812
|
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
781
813
|
const cuid2Regex = /^[a-z][a-z0-9]*$/;
|
|
814
|
+
const ulidRegex = /[0-9A-HJKMNP-TV-Z]{26}/;
|
|
782
815
|
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;
|
|
783
816
|
// from https://stackoverflow.com/a/46181/1550155
|
|
784
817
|
// old version: too slow, didn't support unicode
|
|
@@ -983,6 +1016,17 @@ class ZodString extends ZodType {
|
|
|
983
1016
|
status.dirty();
|
|
984
1017
|
}
|
|
985
1018
|
}
|
|
1019
|
+
else if (check.kind === "ulid") {
|
|
1020
|
+
if (!ulidRegex.test(input.data)) {
|
|
1021
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1022
|
+
addIssueToContext(ctx, {
|
|
1023
|
+
validation: "ulid",
|
|
1024
|
+
code: ZodIssueCode.invalid_string,
|
|
1025
|
+
message: check.message,
|
|
1026
|
+
});
|
|
1027
|
+
status.dirty();
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
986
1030
|
else if (check.kind === "url") {
|
|
987
1031
|
try {
|
|
988
1032
|
new URL(input.data);
|
|
@@ -1013,6 +1057,17 @@ class ZodString extends ZodType {
|
|
|
1013
1057
|
else if (check.kind === "trim") {
|
|
1014
1058
|
input.data = input.data.trim();
|
|
1015
1059
|
}
|
|
1060
|
+
else if (check.kind === "includes") {
|
|
1061
|
+
if (!input.data.includes(check.value, check.position)) {
|
|
1062
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1063
|
+
addIssueToContext(ctx, {
|
|
1064
|
+
code: ZodIssueCode.invalid_string,
|
|
1065
|
+
validation: { includes: check.value, position: check.position },
|
|
1066
|
+
message: check.message,
|
|
1067
|
+
});
|
|
1068
|
+
status.dirty();
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1016
1071
|
else if (check.kind === "toLowerCase") {
|
|
1017
1072
|
input.data = input.data.toLowerCase();
|
|
1018
1073
|
}
|
|
@@ -1094,6 +1149,9 @@ class ZodString extends ZodType {
|
|
|
1094
1149
|
cuid2(message) {
|
|
1095
1150
|
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
|
|
1096
1151
|
}
|
|
1152
|
+
ulid(message) {
|
|
1153
|
+
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
|
|
1154
|
+
}
|
|
1097
1155
|
ip(options) {
|
|
1098
1156
|
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
|
|
1099
1157
|
}
|
|
@@ -1121,6 +1179,14 @@ class ZodString extends ZodType {
|
|
|
1121
1179
|
...errorUtil.errToObj(message),
|
|
1122
1180
|
});
|
|
1123
1181
|
}
|
|
1182
|
+
includes(value, options) {
|
|
1183
|
+
return this._addCheck({
|
|
1184
|
+
kind: "includes",
|
|
1185
|
+
value: value,
|
|
1186
|
+
position: options === null || options === void 0 ? void 0 : options.position,
|
|
1187
|
+
...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message),
|
|
1188
|
+
});
|
|
1189
|
+
}
|
|
1124
1190
|
startsWith(value, message) {
|
|
1125
1191
|
return this._addCheck({
|
|
1126
1192
|
kind: "startsWith",
|
|
@@ -1177,6 +1243,9 @@ class ZodString extends ZodType {
|
|
|
1177
1243
|
get isCUID2() {
|
|
1178
1244
|
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
|
|
1179
1245
|
}
|
|
1246
|
+
get isULID() {
|
|
1247
|
+
return !!this._def.checks.find((ch) => ch.kind === "ulid");
|
|
1248
|
+
}
|
|
1180
1249
|
get isIP() {
|
|
1181
1250
|
return !!this._def.checks.find((ch) => ch.kind === "ip");
|
|
1182
1251
|
}
|
|
@@ -2007,22 +2076,6 @@ ZodArray.create = (schema, params) => {
|
|
|
2007
2076
|
...processCreateParams(params),
|
|
2008
2077
|
});
|
|
2009
2078
|
};
|
|
2010
|
-
/////////////////////////////////////////
|
|
2011
|
-
/////////////////////////////////////////
|
|
2012
|
-
////////// //////////
|
|
2013
|
-
////////// ZodObject //////////
|
|
2014
|
-
////////// //////////
|
|
2015
|
-
/////////////////////////////////////////
|
|
2016
|
-
/////////////////////////////////////////
|
|
2017
|
-
var objectUtil;
|
|
2018
|
-
(function (objectUtil) {
|
|
2019
|
-
objectUtil.mergeShapes = (first, second) => {
|
|
2020
|
-
return {
|
|
2021
|
-
...first,
|
|
2022
|
-
...second, // second overwrites first
|
|
2023
|
-
};
|
|
2024
|
-
};
|
|
2025
|
-
})(objectUtil || (objectUtil = {}));
|
|
2026
2079
|
function deepPartialify(schema) {
|
|
2027
2080
|
if (schema instanceof ZodObject) {
|
|
2028
2081
|
const newShape = {};
|
|
@@ -2036,7 +2089,10 @@ function deepPartialify(schema) {
|
|
|
2036
2089
|
});
|
|
2037
2090
|
}
|
|
2038
2091
|
else if (schema instanceof ZodArray) {
|
|
2039
|
-
return ZodArray
|
|
2092
|
+
return new ZodArray({
|
|
2093
|
+
...schema._def,
|
|
2094
|
+
type: deepPartialify(schema.element),
|
|
2095
|
+
});
|
|
2040
2096
|
}
|
|
2041
2097
|
else if (schema instanceof ZodOptional) {
|
|
2042
2098
|
return ZodOptional.create(deepPartialify(schema.unwrap()));
|
|
@@ -2268,7 +2324,10 @@ class ZodObject extends ZodType {
|
|
|
2268
2324
|
const merged = new ZodObject({
|
|
2269
2325
|
unknownKeys: merging._def.unknownKeys,
|
|
2270
2326
|
catchall: merging._def.catchall,
|
|
2271
|
-
shape: () =>
|
|
2327
|
+
shape: () => ({
|
|
2328
|
+
...this._def.shape(),
|
|
2329
|
+
...merging._def.shape(),
|
|
2330
|
+
}),
|
|
2272
2331
|
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2273
2332
|
});
|
|
2274
2333
|
return merged;
|
|
@@ -3542,6 +3601,7 @@ class ZodCatch extends ZodType {
|
|
|
3542
3601
|
get error() {
|
|
3543
3602
|
return new ZodError(newCtx.common.issues);
|
|
3544
3603
|
},
|
|
3604
|
+
input: newCtx.data,
|
|
3545
3605
|
}),
|
|
3546
3606
|
};
|
|
3547
3607
|
});
|
|
@@ -3555,6 +3615,7 @@ class ZodCatch extends ZodType {
|
|
|
3555
3615
|
get error() {
|
|
3556
3616
|
return new ZodError(newCtx.common.issues);
|
|
3557
3617
|
},
|
|
3618
|
+
input: newCtx.data,
|
|
3558
3619
|
}),
|
|
3559
3620
|
};
|
|
3560
3621
|
}
|
|
@@ -3792,6 +3853,7 @@ var z = /*#__PURE__*/Object.freeze({
|
|
|
3792
3853
|
isValid: isValid,
|
|
3793
3854
|
isAsync: isAsync,
|
|
3794
3855
|
get util () { return util; },
|
|
3856
|
+
get objectUtil () { return objectUtil; },
|
|
3795
3857
|
ZodParsedType: ZodParsedType,
|
|
3796
3858
|
getParsedType: getParsedType,
|
|
3797
3859
|
ZodType: ZodType,
|
|
@@ -3808,7 +3870,6 @@ var z = /*#__PURE__*/Object.freeze({
|
|
|
3808
3870
|
ZodNever: ZodNever,
|
|
3809
3871
|
ZodVoid: ZodVoid,
|
|
3810
3872
|
ZodArray: ZodArray,
|
|
3811
|
-
get objectUtil () { return objectUtil; },
|
|
3812
3873
|
ZodObject: ZodObject,
|
|
3813
3874
|
ZodUnion: ZodUnion,
|
|
3814
3875
|
ZodDiscriminatedUnion: ZodDiscriminatedUnion,
|