zod 3.20.6 → 3.21.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/README.md +159 -18
- package/lib/ZodError.d.ts +17 -13
- package/lib/ZodError.js +0 -8
- package/lib/benchmarks/index.js +33 -8
- package/lib/helpers/parseUtil.js +1 -1
- package/lib/helpers/util.d.ts +27 -4
- package/lib/helpers/util.js +13 -4
- package/lib/index.d.ts +3 -3
- package/lib/index.js +3 -3
- package/lib/index.mjs +350 -47
- package/lib/index.umd.js +351 -48
- package/lib/locales/en.js +15 -3
- package/lib/types.d.ts +103 -75
- package/lib/types.js +322 -220
- package/package.json +55 -54
package/README.md
CHANGED
|
@@ -24,6 +24,8 @@
|
|
|
24
24
|
<span> • </span>
|
|
25
25
|
<a href="https://www.npmjs.com/package/zod">npm</a>
|
|
26
26
|
<span> • </span>
|
|
27
|
+
<a href="https://deno.land/x/zod">deno</a>
|
|
28
|
+
<span> • </span>
|
|
27
29
|
<a href="https://github.com/colinhacks/zod/issues/new">Issues</a>
|
|
28
30
|
<span> • </span>
|
|
29
31
|
<a href="https://twitter.com/colinhacks">@colinhacks</a>
|
|
@@ -55,7 +57,10 @@
|
|
|
55
57
|
- [Coercion for primitives](#coercion-for-primitives)
|
|
56
58
|
- [Literals](#literals)
|
|
57
59
|
- [Strings](#strings)
|
|
60
|
+
- [Datetime](#datetime-validation)
|
|
61
|
+
- [IP](#ip-address-validation)
|
|
58
62
|
- [Numbers](#numbers)
|
|
63
|
+
- [BigInts](#bigints)
|
|
59
64
|
- [NaNs](#nans)
|
|
60
65
|
- [Booleans](#booleans)
|
|
61
66
|
- [Dates](#dates)
|
|
@@ -113,6 +118,7 @@
|
|
|
113
118
|
- [.or](#or)
|
|
114
119
|
- [.and](#and)
|
|
115
120
|
- [.brand](#brand)
|
|
121
|
+
- [.pipe](#pipe)
|
|
116
122
|
- [Guides and concepts](#guides-and-concepts)
|
|
117
123
|
- [Type inference](#type-inference)
|
|
118
124
|
- [Writing generic functions](#writing-generic-functions)
|
|
@@ -200,6 +206,26 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
|
|
|
200
206
|
<a href="https://proxy.com">proxy.com</a>
|
|
201
207
|
</td>
|
|
202
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>
|
|
203
229
|
</table>
|
|
204
230
|
|
|
205
231
|
#### Silver
|
|
@@ -303,7 +329,7 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
|
|
|
303
329
|
</td>
|
|
304
330
|
</tr>
|
|
305
331
|
<tr>
|
|
306
|
-
|
|
332
|
+
<td align="center">
|
|
307
333
|
<a href="https://fungible.systems/">
|
|
308
334
|
<img src="https://avatars.githubusercontent.com/u/80220121?s=200&v=4" width="100px;" alt="Fungible Systems logo"/>
|
|
309
335
|
</a>
|
|
@@ -335,6 +361,39 @@ Sponsorship at any level is appreciated and encouraged. For individual developer
|
|
|
335
361
|
<br />
|
|
336
362
|
</td>
|
|
337
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>
|
|
338
397
|
</table>
|
|
339
398
|
|
|
340
399
|
### Ecosystem
|
|
@@ -581,19 +640,28 @@ tuna.value; // "tuna"
|
|
|
581
640
|
Zod includes a handful of string-specific validations.
|
|
582
641
|
|
|
583
642
|
```ts
|
|
643
|
+
// validations
|
|
584
644
|
z.string().max(5);
|
|
585
645
|
z.string().min(5);
|
|
586
646
|
z.string().length(5);
|
|
587
647
|
z.string().email();
|
|
588
648
|
z.string().url();
|
|
649
|
+
z.string().emoji();
|
|
589
650
|
z.string().uuid();
|
|
590
651
|
z.string().cuid();
|
|
591
652
|
z.string().cuid2();
|
|
653
|
+
z.string().ulid();
|
|
592
654
|
z.string().regex(regex);
|
|
655
|
+
z.string().includes(string);
|
|
593
656
|
z.string().startsWith(string);
|
|
594
657
|
z.string().endsWith(string);
|
|
595
|
-
z.string().trim(); // trim whitespace
|
|
596
658
|
z.string().datetime(); // defaults to UTC, see below for options
|
|
659
|
+
z.string().ip(); // defaults to IPv4 and IPv6, see below for options
|
|
660
|
+
|
|
661
|
+
// transformations
|
|
662
|
+
z.string().trim(); // trim whitespace
|
|
663
|
+
z.string().toLowerCase(); // toLowerCase
|
|
664
|
+
z.string().toUpperCase(); // toUpperCase
|
|
597
665
|
```
|
|
598
666
|
|
|
599
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).
|
|
@@ -615,13 +683,16 @@ z.string().max(5, { message: "Must be 5 or fewer characters long" });
|
|
|
615
683
|
z.string().length(5, { message: "Must be exactly 5 characters long" });
|
|
616
684
|
z.string().email({ message: "Invalid email address" });
|
|
617
685
|
z.string().url({ message: "Invalid url" });
|
|
686
|
+
z.string().emoji({ message: "Contains non-emoji characters" });
|
|
618
687
|
z.string().uuid({ message: "Invalid UUID" });
|
|
688
|
+
z.string().includes("tuna", { message: "Must include tuna" });
|
|
619
689
|
z.string().startsWith("https://", { message: "Must provide secure URL" });
|
|
620
690
|
z.string().endsWith(".com", { message: "Only .com domains allowed" });
|
|
621
691
|
z.string().datetime({ message: "Invalid datetime string! Must be UTC." });
|
|
692
|
+
z.string().ip({ message: "Invalid IP address" });
|
|
622
693
|
```
|
|
623
694
|
|
|
624
|
-
###
|
|
695
|
+
### ISO datetimes
|
|
625
696
|
|
|
626
697
|
The `z.string().datetime()` method defaults to UTC validation: no timezone offsets with arbitrary sub-second decimal precision.
|
|
627
698
|
|
|
@@ -656,6 +727,31 @@ datetime.parse("2020-01-01T00:00:00Z"); // fail
|
|
|
656
727
|
datetime.parse("2020-01-01T00:00:00.123456Z"); // fail
|
|
657
728
|
```
|
|
658
729
|
|
|
730
|
+
### IP addresses
|
|
731
|
+
|
|
732
|
+
The `z.string().ip()` method by default validate IPv4 and IPv6.
|
|
733
|
+
|
|
734
|
+
```ts
|
|
735
|
+
const ip = z.string().ip();
|
|
736
|
+
|
|
737
|
+
ip.parse("192.168.1.1"); // pass
|
|
738
|
+
ip.parse("84d5:51a0:9114:1855:4cfa:f2d7:1f12:7003"); // pass
|
|
739
|
+
ip.parse("84d5:51a0:9114:1855:4cfa:f2d7:1f12:192.168.1.1"); // pass
|
|
740
|
+
|
|
741
|
+
ip.parse("256.1.1.1"); // fail
|
|
742
|
+
ip.parse("84d5:51a0:9114:gggg:4cfa:f2d7:1f12:7003"); // fail
|
|
743
|
+
```
|
|
744
|
+
|
|
745
|
+
You can additionally set the IP `version`.
|
|
746
|
+
|
|
747
|
+
```ts
|
|
748
|
+
const ipv4 = z.string().ip({ version: "v4" });
|
|
749
|
+
ipv4.parse("84d5:51a0:9114:1855:4cfa:f2d7:1f12:7003"); // fail
|
|
750
|
+
|
|
751
|
+
const ipv6 = z.string().ip({ version: "v6" });
|
|
752
|
+
ipv6.parse("192.168.1.1"); // fail
|
|
753
|
+
```
|
|
754
|
+
|
|
659
755
|
## Numbers
|
|
660
756
|
|
|
661
757
|
You can customize certain error messages when creating a number schema.
|
|
@@ -685,6 +781,7 @@ z.number().nonpositive(); // <= 0
|
|
|
685
781
|
z.number().multipleOf(5); // Evenly divisible by 5. Alias .step(5)
|
|
686
782
|
|
|
687
783
|
z.number().finite(); // value must be finite, not Infinity or -Infinity
|
|
784
|
+
z.number().safe(); // value must be between Number.MIN_SAFE_INTEGER and Number.MAX_SAFE_INTEGER
|
|
688
785
|
```
|
|
689
786
|
|
|
690
787
|
Optionally, you can pass in a second argument to provide a custom error message.
|
|
@@ -693,6 +790,24 @@ Optionally, you can pass in a second argument to provide a custom error message.
|
|
|
693
790
|
z.number().lte(5, { message: "this👏is👏too👏big" });
|
|
694
791
|
```
|
|
695
792
|
|
|
793
|
+
## BigInts
|
|
794
|
+
|
|
795
|
+
Zod includes a handful of bigint-specific validations.
|
|
796
|
+
|
|
797
|
+
```ts
|
|
798
|
+
z.bigint().gt(5n);
|
|
799
|
+
z.bigint().gte(5n); // alias `.min(5n)`
|
|
800
|
+
z.bigint().lt(5n);
|
|
801
|
+
z.bigint().lte(5n); // alias `.max(5n)`
|
|
802
|
+
|
|
803
|
+
z.bigint().positive(); // > 0n
|
|
804
|
+
z.bigint().nonnegative(); // >= 0n
|
|
805
|
+
z.bigint().negative(); // < 0n
|
|
806
|
+
z.bigint().nonpositive(); // <= 0n
|
|
807
|
+
|
|
808
|
+
z.bigint().multipleOf(5n); // Evenly divisible by 5n.
|
|
809
|
+
```
|
|
810
|
+
|
|
696
811
|
## NaNs
|
|
697
812
|
|
|
698
813
|
You can customize certain error messages when creating a nan schema.
|
|
@@ -1409,7 +1524,7 @@ type NumberSet = z.infer<typeof numberSet>;
|
|
|
1409
1524
|
// type NumberSet = Set<number>
|
|
1410
1525
|
```
|
|
1411
1526
|
|
|
1412
|
-
Set schemas can be further
|
|
1527
|
+
Set schemas can be further constrained with the following utility methods.
|
|
1413
1528
|
|
|
1414
1529
|
```ts
|
|
1415
1530
|
z.set(z.string()).nonempty(); // must contain at least one item
|
|
@@ -1703,9 +1818,14 @@ This returns a `ZodEffects` instance. `ZodEffects` is a wrapper class that conta
|
|
|
1703
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.
|
|
1704
1819
|
|
|
1705
1820
|
```ts
|
|
1706
|
-
const px = z.custom<`${number}px`>((val) =>
|
|
1707
|
-
px
|
|
1708
|
-
|
|
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;
|
|
1709
1829
|
```
|
|
1710
1830
|
|
|
1711
1831
|
If you don't provide a validation function, Zod will allow any value. This can be dangerous!
|
|
@@ -1714,6 +1834,12 @@ If you don't provide a validation function, Zod will allow any value. This can b
|
|
|
1714
1834
|
z.custom<{ arg: string }>(); // performs no validation
|
|
1715
1835
|
```
|
|
1716
1836
|
|
|
1837
|
+
You can customize the error message and other options by passing a second argument. This parameter works the same way as the params parameter of [`.refine`](#refine).
|
|
1838
|
+
|
|
1839
|
+
```ts
|
|
1840
|
+
z.custom<...>((val) => ..., "custom error message");
|
|
1841
|
+
```
|
|
1842
|
+
|
|
1717
1843
|
## Schema methods
|
|
1718
1844
|
|
|
1719
1845
|
All Zod schemas contain certain methods.
|
|
@@ -2118,14 +2244,17 @@ numberWithCatch.parse(5); // => 5
|
|
|
2118
2244
|
numberWithCatch.parse("tuna"); // => 42
|
|
2119
2245
|
```
|
|
2120
2246
|
|
|
2121
|
-
Optionally, you can pass a function into `.catch` that will be re-executed whenever a default value needs to be generated
|
|
2247
|
+
Optionally, you can pass a function into `.catch` that will be re-executed whenever a default value needs to be generated. A `ctx` object containing the caught error will be passed into this function.
|
|
2122
2248
|
|
|
2123
2249
|
```ts
|
|
2124
|
-
const numberWithRandomCatch = z.number().catch(
|
|
2250
|
+
const numberWithRandomCatch = z.number().catch((ctx) => {
|
|
2251
|
+
ctx.error; // the caught ZodError
|
|
2252
|
+
return Math.random();
|
|
2253
|
+
});
|
|
2125
2254
|
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2255
|
+
numberWithRandomCatch.parse("sup"); // => 0.4413456736055323
|
|
2256
|
+
numberWithRandomCatch.parse("sup"); // => 0.1871840107401901
|
|
2257
|
+
numberWithRandomCatch.parse("sup"); // => 0.7223408162401552
|
|
2129
2258
|
```
|
|
2130
2259
|
|
|
2131
2260
|
Conceptually, this is how Zod processes "catch values":
|
|
@@ -2253,6 +2382,18 @@ type Cat = z.infer<typeof Cat>;
|
|
|
2253
2382
|
|
|
2254
2383
|
Note that branded types do not affect the runtime result of `.parse`. It is a static-only construct.
|
|
2255
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
|
+
|
|
2256
2397
|
## Guides and concepts
|
|
2257
2398
|
|
|
2258
2399
|
### Type inference
|
|
@@ -2349,14 +2490,14 @@ makeSchemaOptional(z.number());
|
|
|
2349
2490
|
Zod provides a subclass of Error called `ZodError`. ZodErrors contain an `issues` array containing detailed information about the validation problems.
|
|
2350
2491
|
|
|
2351
2492
|
```ts
|
|
2352
|
-
const
|
|
2493
|
+
const result = z
|
|
2353
2494
|
.object({
|
|
2354
2495
|
name: z.string(),
|
|
2355
2496
|
})
|
|
2356
2497
|
.safeParse({ name: 12 });
|
|
2357
2498
|
|
|
2358
|
-
if (!
|
|
2359
|
-
|
|
2499
|
+
if (!result.success) {
|
|
2500
|
+
result.error.issues;
|
|
2360
2501
|
/* [
|
|
2361
2502
|
{
|
|
2362
2503
|
"code": "invalid_type",
|
|
@@ -2378,14 +2519,14 @@ Zod's error reporting emphasizes _completeness_ and _correctness_. If you are lo
|
|
|
2378
2519
|
You can use the `.format()` method to convert this error into a nested object.
|
|
2379
2520
|
|
|
2380
2521
|
```ts
|
|
2381
|
-
const
|
|
2522
|
+
const result = z
|
|
2382
2523
|
.object({
|
|
2383
2524
|
name: z.string(),
|
|
2384
2525
|
})
|
|
2385
2526
|
.safeParse({ name: 12 });
|
|
2386
2527
|
|
|
2387
|
-
if (!
|
|
2388
|
-
const formatted =
|
|
2528
|
+
if (!result.success) {
|
|
2529
|
+
const formatted = result.error.format();
|
|
2389
2530
|
/* {
|
|
2390
2531
|
name: { _errors: [ 'Expected string, received number' ] }
|
|
2391
2532
|
} */
|
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" | "uuid" | "regex" | "cuid" | "cuid2" | "datetime" | {
|
|
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;
|
|
@@ -81,24 +84,24 @@ export interface ZodInvalidStringIssue extends ZodIssueBase {
|
|
|
81
84
|
}
|
|
82
85
|
export interface ZodTooSmallIssue extends ZodIssueBase {
|
|
83
86
|
code: typeof ZodIssueCode.too_small;
|
|
84
|
-
minimum: number;
|
|
87
|
+
minimum: number | bigint;
|
|
85
88
|
inclusive: boolean;
|
|
86
89
|
exact?: boolean;
|
|
87
|
-
type: "array" | "string" | "number" | "set" | "date";
|
|
90
|
+
type: "array" | "string" | "number" | "set" | "date" | "bigint";
|
|
88
91
|
}
|
|
89
92
|
export interface ZodTooBigIssue extends ZodIssueBase {
|
|
90
93
|
code: typeof ZodIssueCode.too_big;
|
|
91
|
-
maximum: number;
|
|
94
|
+
maximum: number | bigint;
|
|
92
95
|
inclusive: boolean;
|
|
93
96
|
exact?: boolean;
|
|
94
|
-
type: "array" | "string" | "number" | "set" | "date";
|
|
97
|
+
type: "array" | "string" | "number" | "set" | "date" | "bigint";
|
|
95
98
|
}
|
|
96
99
|
export interface ZodInvalidIntersectionTypesIssue extends ZodIssueBase {
|
|
97
100
|
code: typeof ZodIssueCode.invalid_intersection_types;
|
|
98
101
|
}
|
|
99
102
|
export interface ZodNotMultipleOfIssue extends ZodIssueBase {
|
|
100
103
|
code: typeof ZodIssueCode.not_multiple_of;
|
|
101
|
-
multipleOf: number;
|
|
104
|
+
multipleOf: number | bigint;
|
|
102
105
|
}
|
|
103
106
|
export interface ZodNotFiniteIssue extends ZodIssueBase {
|
|
104
107
|
code: typeof ZodIssueCode.not_finite;
|
|
@@ -118,15 +121,16 @@ export declare type ZodIssue = ZodIssueOptionalMessage & {
|
|
|
118
121
|
message: string;
|
|
119
122
|
};
|
|
120
123
|
export declare const quotelessJson: (obj: any) => string;
|
|
124
|
+
declare type recursiveZodFormattedError<T> = T extends [any, ...any[]] ? {
|
|
125
|
+
[K in keyof T]?: ZodFormattedError<T[K]>;
|
|
126
|
+
} : T extends any[] ? {
|
|
127
|
+
[k: number]: ZodFormattedError<T[number]>;
|
|
128
|
+
} : T extends object ? {
|
|
129
|
+
[K in keyof T]?: ZodFormattedError<T[K]>;
|
|
130
|
+
} : unknown;
|
|
121
131
|
export declare type ZodFormattedError<T, U = string> = {
|
|
122
132
|
_errors: U[];
|
|
123
|
-
} &
|
|
124
|
-
[K in keyof NonNullable<T>]?: ZodFormattedError<NonNullable<T>[K], U>;
|
|
125
|
-
} : NonNullable<T> extends any[] ? {
|
|
126
|
-
[k: number]: ZodFormattedError<NonNullable<T>[number], U>;
|
|
127
|
-
} : NonNullable<T> extends object ? {
|
|
128
|
-
[K in keyof NonNullable<T>]?: ZodFormattedError<NonNullable<T>[K], U>;
|
|
129
|
-
} : unknown);
|
|
133
|
+
} & recursiveZodFormattedError<NonNullable<T>>;
|
|
130
134
|
export declare type inferFormattedError<T extends ZodType<any, any, any>, U = string> = ZodFormattedError<TypeOf<T>, U>;
|
|
131
135
|
export declare class ZodError<T = any> extends Error {
|
|
132
136
|
issues: ZodIssue[];
|
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/benchmarks/index.js
CHANGED
|
@@ -9,13 +9,38 @@ const primitives_1 = __importDefault(require("./primitives"));
|
|
|
9
9
|
const realworld_1 = __importDefault(require("./realworld"));
|
|
10
10
|
const string_1 = __importDefault(require("./string"));
|
|
11
11
|
const union_1 = __importDefault(require("./union"));
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
12
|
+
const argv = process.argv.slice(2);
|
|
13
|
+
let suites = [];
|
|
14
|
+
if (!argv.length) {
|
|
15
|
+
suites = [
|
|
16
|
+
...realworld_1.default.suites,
|
|
17
|
+
...primitives_1.default.suites,
|
|
18
|
+
...string_1.default.suites,
|
|
19
|
+
...object_1.default.suites,
|
|
20
|
+
...union_1.default.suites,
|
|
21
|
+
...discriminatedUnion_1.default.suites,
|
|
22
|
+
];
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
if (argv.includes("--realworld")) {
|
|
26
|
+
suites.push(...realworld_1.default.suites);
|
|
27
|
+
}
|
|
28
|
+
if (argv.includes("--primitives")) {
|
|
29
|
+
suites.push(...primitives_1.default.suites);
|
|
30
|
+
}
|
|
31
|
+
if (argv.includes("--string")) {
|
|
32
|
+
suites.push(...string_1.default.suites);
|
|
33
|
+
}
|
|
34
|
+
if (argv.includes("--object")) {
|
|
35
|
+
suites.push(...object_1.default.suites);
|
|
36
|
+
}
|
|
37
|
+
if (argv.includes("--union")) {
|
|
38
|
+
suites.push(...union_1.default.suites);
|
|
39
|
+
}
|
|
40
|
+
if (argv.includes("--discriminatedUnion")) {
|
|
41
|
+
suites.push(...discriminatedUnion_1.default.suites);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
for (const suite of suites) {
|
|
20
45
|
suite.run();
|
|
21
46
|
}
|
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);
|
package/lib/helpers/util.d.ts
CHANGED
|
@@ -11,16 +11,39 @@ export declare namespace util {
|
|
|
11
11
|
export const objectValues: (obj: any) => any[];
|
|
12
12
|
export const objectKeys: ObjectConstructor["keys"];
|
|
13
13
|
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
|
-
}>;
|
|
14
|
+
export type identity<T> = objectUtil.identity<T>;
|
|
15
|
+
export type flatten<T> = objectUtil.flatten<T>;
|
|
18
16
|
export type noUndefined<T> = T extends undefined ? never : T;
|
|
19
17
|
export const isInteger: NumberConstructor["isInteger"];
|
|
20
18
|
export function joinValues<T extends any[]>(array: T, separator?: string): string;
|
|
21
19
|
export const jsonStringifyReplacer: (_: string, value: any) => any;
|
|
22
20
|
export {};
|
|
23
21
|
}
|
|
22
|
+
export declare namespace objectUtil {
|
|
23
|
+
export type MergeShapes<U, V> = {
|
|
24
|
+
[k in Exclude<keyof U, keyof V>]: U[k];
|
|
25
|
+
} & V;
|
|
26
|
+
type optionalKeys<T extends object> = {
|
|
27
|
+
[k in keyof T]: undefined extends T[k] ? k : never;
|
|
28
|
+
}[keyof T];
|
|
29
|
+
type requiredKeys<T extends object> = {
|
|
30
|
+
[k in keyof T]: undefined extends T[k] ? never : k;
|
|
31
|
+
}[keyof T];
|
|
32
|
+
export type addQuestionMarks<T extends object> = Partial<Pick<T, optionalKeys<T>>> & Pick<T, requiredKeys<T>>;
|
|
33
|
+
export type identity<T> = T;
|
|
34
|
+
export type flatten<T> = identity<{
|
|
35
|
+
[k in keyof T]: T[k];
|
|
36
|
+
}>;
|
|
37
|
+
export type noNeverKeys<T> = {
|
|
38
|
+
[k in keyof T]: [T[k]] extends [never] ? never : k;
|
|
39
|
+
}[keyof T];
|
|
40
|
+
export type noNever<T> = identity<{
|
|
41
|
+
[k in noNeverKeys<T>]: k extends keyof T ? T[k] : never;
|
|
42
|
+
}>;
|
|
43
|
+
export const mergeShapes: <U, T>(first: U, second: T) => T & U;
|
|
44
|
+
export type extendShape<A, B> = flatten<Omit<A, keyof B> & B>;
|
|
45
|
+
export {};
|
|
46
|
+
}
|
|
24
47
|
export declare const ZodParsedType: {
|
|
25
48
|
function: "function";
|
|
26
49
|
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.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as z from "./external";
|
|
2
2
|
export * from "./external";
|
|
3
|
-
export {
|
|
4
|
-
export default
|
|
3
|
+
export { z };
|
|
4
|
+
export default z;
|
package/lib/index.js
CHANGED
|
@@ -23,7 +23,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
23
23
|
};
|
|
24
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
25
|
exports.z = void 0;
|
|
26
|
-
const
|
|
27
|
-
exports.z =
|
|
26
|
+
const z = __importStar(require("./external"));
|
|
27
|
+
exports.z = z;
|
|
28
28
|
__exportStar(require("./external"), exports);
|
|
29
|
-
exports.default =
|
|
29
|
+
exports.default = z;
|