zod 3.20.6 → 3.21.4
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 +211 -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/partialUtil.d.ts +4 -4
- package/lib/helpers/util.d.ts +25 -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 +366 -56
- package/lib/index.umd.js +367 -57
- package/lib/locales/en.js +15 -3
- package/lib/types.d.ts +123 -86
- package/lib/types.js +329 -221
- 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,70 @@ 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
|
+
|
|
2397
|
+
#### You can use `.pipe()` to fix common issues with `z.coerce`.
|
|
2398
|
+
|
|
2399
|
+
You can constrain the input to types that work well with your chosen coercion. Then use `.pipe()` to apply the coercion.
|
|
2400
|
+
|
|
2401
|
+
without constrained input:
|
|
2402
|
+
```ts
|
|
2403
|
+
const toDate = z.coerce.date()
|
|
2404
|
+
|
|
2405
|
+
// works intuitively
|
|
2406
|
+
console.log(toDate.safeParse('2023-01-01').success) // true
|
|
2407
|
+
|
|
2408
|
+
// might not be what you want
|
|
2409
|
+
console.log(toDate.safeParse(null).success) // true
|
|
2410
|
+
```
|
|
2411
|
+
|
|
2412
|
+
with constrained input:
|
|
2413
|
+
```ts
|
|
2414
|
+
const datelike = z.union([z.number(), z.string(), z.date()])
|
|
2415
|
+
const datelikeToDate = datelike.pipe(z.coerce.date())
|
|
2416
|
+
|
|
2417
|
+
// still works intuitively
|
|
2418
|
+
console.log(datelikeToDate.safeParse('2023-01-01').success) // true
|
|
2419
|
+
|
|
2420
|
+
// more likely what you want
|
|
2421
|
+
console.log(datelikeToDate.safeParse(null).success) // false
|
|
2422
|
+
```
|
|
2423
|
+
|
|
2424
|
+
You can also use this technique to avoid coercions that throw uncaught errors.
|
|
2425
|
+
|
|
2426
|
+
without constrained input:
|
|
2427
|
+
```ts
|
|
2428
|
+
const toBigInt = z.coerce.bigint()
|
|
2429
|
+
|
|
2430
|
+
// works intuitively
|
|
2431
|
+
console.log( toBigInt.safeParse( '42' ) ) // true
|
|
2432
|
+
|
|
2433
|
+
// probably not what you want
|
|
2434
|
+
console.log( toBigInt.safeParse( null ) ) // throws uncaught error
|
|
2435
|
+
```
|
|
2436
|
+
|
|
2437
|
+
with constrained input:
|
|
2438
|
+
```ts
|
|
2439
|
+
const toNumber = z.number().or( z.string() ).pipe( z.coerce.number() )
|
|
2440
|
+
const toBigInt = z.bigint().or( toNumber ).pipe( z.coerce.bigint() )
|
|
2441
|
+
|
|
2442
|
+
// still works intuitively
|
|
2443
|
+
console.log( toBigInt.safeParse( '42' ).success ) // true
|
|
2444
|
+
|
|
2445
|
+
// error handled by zod, more likely what you want
|
|
2446
|
+
console.log( toBigInt.safeParse( null ).success ) // false
|
|
2447
|
+
```
|
|
2448
|
+
|
|
2256
2449
|
## Guides and concepts
|
|
2257
2450
|
|
|
2258
2451
|
### Type inference
|
|
@@ -2349,14 +2542,14 @@ makeSchemaOptional(z.number());
|
|
|
2349
2542
|
Zod provides a subclass of Error called `ZodError`. ZodErrors contain an `issues` array containing detailed information about the validation problems.
|
|
2350
2543
|
|
|
2351
2544
|
```ts
|
|
2352
|
-
const
|
|
2545
|
+
const result = z
|
|
2353
2546
|
.object({
|
|
2354
2547
|
name: z.string(),
|
|
2355
2548
|
})
|
|
2356
2549
|
.safeParse({ name: 12 });
|
|
2357
2550
|
|
|
2358
|
-
if (!
|
|
2359
|
-
|
|
2551
|
+
if (!result.success) {
|
|
2552
|
+
result.error.issues;
|
|
2360
2553
|
/* [
|
|
2361
2554
|
{
|
|
2362
2555
|
"code": "invalid_type",
|
|
@@ -2378,14 +2571,14 @@ Zod's error reporting emphasizes _completeness_ and _correctness_. If you are lo
|
|
|
2378
2571
|
You can use the `.format()` method to convert this error into a nested object.
|
|
2379
2572
|
|
|
2380
2573
|
```ts
|
|
2381
|
-
const
|
|
2574
|
+
const result = z
|
|
2382
2575
|
.object({
|
|
2383
2576
|
name: z.string(),
|
|
2384
2577
|
})
|
|
2385
2578
|
.safeParse({ name: 12 });
|
|
2386
2579
|
|
|
2387
|
-
if (!
|
|
2388
|
-
const formatted =
|
|
2580
|
+
if (!result.success) {
|
|
2581
|
+
const formatted = result.error.format();
|
|
2389
2582
|
/* {
|
|
2390
2583
|
name: { _errors: [ 'Expected string, received number' ] }
|
|
2391
2584
|
} */
|
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);
|
|
@@ -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<Required<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.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;
|