zod 3.24.2 → 3.24.3
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 +107 -96
- package/lib/helpers/util.d.ts +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
<p align="center">
|
|
2
|
-
<img src="logo.svg" width="200px" align="center" alt="Zod logo" />
|
|
2
|
+
<img src="https://raw.githubusercontent.com/colinhacks/zod/main/logo.svg" width="200px" align="center" alt="Zod logo" />
|
|
3
3
|
<h1 align="center">Zod</h1>
|
|
4
4
|
<p align="center">
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
<a href="https://zod.dev">zod.dev</a>
|
|
6
|
+
<br/>
|
|
7
7
|
TypeScript-first schema validation with static type inference
|
|
8
8
|
</p>
|
|
9
9
|
</p>
|
|
10
|
-
<br/>
|
|
11
10
|
<p align="center">
|
|
12
11
|
<a href="https://github.com/colinhacks/zod/actions?query=branch%3Amain"><img src="https://github.com/colinhacks/zod/actions/workflows/test.yml/badge.svg?event=push&branch=main" alt="Zod CI status" /></a>
|
|
13
12
|
<a href="https://twitter.com/colinhacks" rel="nofollow"><img src="https://img.shields.io/badge/created%20by-@colinhacks-4BBAAB.svg" alt="Created by Colin McDonnell"></a>
|
|
@@ -17,31 +16,44 @@
|
|
|
17
16
|
</p>
|
|
18
17
|
|
|
19
18
|
<div align="center">
|
|
20
|
-
<a href="https://zod.dev">
|
|
19
|
+
<a href="https://zod.dev">Website</a>
|
|
21
20
|
<span> • </span>
|
|
22
21
|
<a href="https://discord.gg/RcG33DQJdf">Discord</a>
|
|
23
22
|
<span> • </span>
|
|
24
|
-
<a href="https://
|
|
25
|
-
<span> • </span>
|
|
26
|
-
<a href="https://deno.land/x/zod">deno</a>
|
|
27
|
-
<span> • </span>
|
|
28
|
-
<a href="https://github.com/colinhacks/zod/issues/new">Issues</a>
|
|
23
|
+
<a href="https://twitter.com/colinhacks">𝕏</a>
|
|
29
24
|
<span> • </span>
|
|
30
|
-
<a href="https://
|
|
31
|
-
<span> • </span>
|
|
32
|
-
<a href="https://trpc.io">tRPC</a>
|
|
25
|
+
<a href="https://bsky.app/profile/zod.dev">Bluesky</a>
|
|
33
26
|
<br />
|
|
34
27
|
</div>
|
|
35
28
|
|
|
29
|
+
<br/><br/>
|
|
30
|
+
|
|
31
|
+
<table align="center" style="justify-content:center;align-items:center;display:flex;"><td>
|
|
32
|
+
<p align="center">Zod 4 is now in beta!
|
|
33
|
+
<br/>
|
|
34
|
+
<a target="_blank" rel="noopener noreferrer" href="https://v4.zod.dev/v4">Read the announcement 👉</a></p></td>
|
|
35
|
+
</table>
|
|
36
|
+
|
|
36
37
|
<br/>
|
|
37
38
|
<br/>
|
|
38
39
|
|
|
39
|
-
|
|
40
|
+
<h2 align="center">Featured sponsor: Fern</h2>
|
|
40
41
|
|
|
41
|
-
|
|
42
|
+
<div align="center">
|
|
43
|
+
<a href="https://link.buildwithfern.com/zod-partnership">
|
|
44
|
+
<picture width="95%" >
|
|
45
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://i.imgur.com/ntvK08h.png">
|
|
46
|
+
<img alt="fern logo" src="https://i.imgur.com/pqyEkg5.png" width="95%">
|
|
47
|
+
</picture>
|
|
48
|
+
</a>
|
|
49
|
+
<br/>
|
|
50
|
+
<p><sub>Learn more about <a target="_blank" rel="noopener noreferrer" href="mailto:sponsorship@colinhacks.com">featured sponsorships</a></sub></p>
|
|
51
|
+
</div>
|
|
42
52
|
|
|
53
|
+
<!-- <hr/> -->
|
|
43
54
|
<br/>
|
|
44
|
-
|
|
55
|
+
<br/>
|
|
56
|
+
|
|
45
57
|
## Table of contents
|
|
46
58
|
|
|
47
59
|
> These docs have been translated into [Chinese](./README_ZH.md) and [Korean](./README_KO.md).
|
|
@@ -165,6 +177,8 @@
|
|
|
165
177
|
- [Ow](#ow)
|
|
166
178
|
- [Changelog](#changelog)
|
|
167
179
|
|
|
180
|
+
<br/>
|
|
181
|
+
|
|
168
182
|
## Introduction
|
|
169
183
|
|
|
170
184
|
Zod is a TypeScript-first schema declaration and validation library. I'm using the term "schema" to broadly refer to any data type, from a simple `string` to a complex nested object.
|
|
@@ -181,32 +195,12 @@ Some other great aspects:
|
|
|
181
195
|
- Functional approach: [parse, don't validate](https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/)
|
|
182
196
|
- Works with plain JavaScript too! You don't need to use TypeScript.
|
|
183
197
|
|
|
198
|
+
<br/>
|
|
199
|
+
|
|
184
200
|
## Sponsors
|
|
185
201
|
|
|
186
202
|
Sponsorship at any level is appreciated and encouraged. If you built a paid product using Zod, consider one of the [corporate tiers](https://github.com/sponsors/colinhacks).
|
|
187
203
|
|
|
188
|
-
<br/>
|
|
189
|
-
<h3 align="center">Diamond</h3>
|
|
190
|
-
|
|
191
|
-
<br/>
|
|
192
|
-
|
|
193
|
-
<div align="center">
|
|
194
|
-
<a href="https://go.clerk.com/PKHrcwh">
|
|
195
|
-
<picture width="100%">
|
|
196
|
-
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/colinhacks/zod/assets/3084745/15c8c8be-189d-44ed-b3db-59bf2a21cbe3">
|
|
197
|
-
<img alt="clerk logo" src="https://github.com/colinhacks/zod/assets/3084745/15c8c8be-189d-44ed-b3db-59bf2a21cbe3">
|
|
198
|
-
</picture>
|
|
199
|
-
</a>
|
|
200
|
-
<br/>
|
|
201
|
-
<br/>
|
|
202
|
-
<p>
|
|
203
|
-
The most comprehensive User Management Platform
|
|
204
|
-
<br/>
|
|
205
|
-
<a style="text-decoration:none;" href="https://go.clerk.com/PKHrcwh" target="_blank">clerk.com</a>
|
|
206
|
-
</p>
|
|
207
|
-
</div>
|
|
208
|
-
|
|
209
|
-
<br/>
|
|
210
204
|
<br/>
|
|
211
205
|
|
|
212
206
|
<h3 align="center">Platinum</h3>
|
|
@@ -294,7 +288,7 @@ Sponsorship at any level is appreciated and encouraged. If you built a paid prod
|
|
|
294
288
|
<a href="https://retool.com/?utm_source=github&utm_medium=referral&utm_campaign=zod">
|
|
295
289
|
<picture height="45px">
|
|
296
290
|
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/colinhacks/zod/assets/3084745/ac65013f-aeb4-48dd-a2ee-41040b69cbe6">
|
|
297
|
-
<img alt="
|
|
291
|
+
<img alt="Retool" height="45px" src="https://github.com/colinhacks/zod/assets/3084745/5ef4c11b-efeb-4495-90a8-41b83f798600">
|
|
298
292
|
</picture>
|
|
299
293
|
</a>
|
|
300
294
|
<br />
|
|
@@ -309,7 +303,7 @@ Sponsorship at any level is appreciated and encouraged. If you built a paid prod
|
|
|
309
303
|
<td align="center">
|
|
310
304
|
<p></p>
|
|
311
305
|
<p>
|
|
312
|
-
<a href="https://
|
|
306
|
+
<a href="https://stainless.com">
|
|
313
307
|
<picture height="45px">
|
|
314
308
|
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/colinhacks/zod/assets/3084745/f20759c1-3e51-49d0-a31e-bbc43abec665">
|
|
315
309
|
<img alt="stainless" height="45px" src="https://github.com/colinhacks/zod/assets/3084745/e9444e44-d991-4bba-a697-dbcfad608e47">
|
|
@@ -318,7 +312,7 @@ Sponsorship at any level is appreciated and encouraged. If you built a paid prod
|
|
|
318
312
|
<br />
|
|
319
313
|
Generate best-in-class SDKs
|
|
320
314
|
<br/>
|
|
321
|
-
<a href="https://
|
|
315
|
+
<a href="https://stainless.com" style="text-decoration:none;">stainless.com</a>
|
|
322
316
|
</p>
|
|
323
317
|
<p></p>
|
|
324
318
|
</td>
|
|
@@ -536,10 +530,13 @@ There are a growing number of tools that are built atop or support Zod natively!
|
|
|
536
530
|
- [`koa-zod-router`](https://github.com/JakeFenley/koa-zod-router): Create typesafe routes in Koa with I/O validation using Zod.
|
|
537
531
|
- [`zod-sockets`](https://github.com/RobinTail/zod-sockets): Zod-powered Socket.IO microframework with I/O validation and built-in AsyncAPI specs
|
|
538
532
|
- [`oas-tszod-gen`](https://github.com/inkognitro/oas-tszod-gen): Client SDK code generator to convert OpenApi v3 specifications into TS endpoint caller functions with Zod types.
|
|
533
|
+
- [`GQLoom`](https://github.com/modevol-com/gqloom): Weave GraphQL schema and resolvers using Zod.
|
|
534
|
+
- [`oRPC`](https://github.com/unnoq/orpc): Typesafe APIs Made Simple
|
|
539
535
|
|
|
540
536
|
#### Form integrations
|
|
541
537
|
|
|
542
538
|
- [`react-hook-form`](https://github.com/react-hook-form/resolvers#zod): A first-party Zod resolver for React Hook Form.
|
|
539
|
+
- [`TanStack Form`](https://github.com/TanStack/form): Headless, performant, and type-safe form state management for TS/JS, React, Vue, Angular, Solid, and Lit
|
|
543
540
|
- [`zod-validation-error`](https://github.com/causaly/zod-validation-error): Generate user-friendly error messages from `ZodError`s.
|
|
544
541
|
- [`zod-formik-adapter`](https://github.com/robertLichtnow/zod-formik-adapter): A community-maintained Formik adapter for Zod.
|
|
545
542
|
- [`react-zorm`](https://github.com/esamattis/react-zorm): Standalone `<form>` generation and validation for React using Zod.
|
|
@@ -612,6 +609,8 @@ There are a growing number of tools that are built atop or support Zod natively!
|
|
|
612
609
|
- [`zod-config`](https://github.com/alexmarqs/zod-config): Load configurations across multiple sources with flexible adapters, ensuring type safety with Zod.
|
|
613
610
|
- [`unplugin-environment`](https://github.com/r17x/js/tree/main/packages/unplugin-environment#readme): A plugin for loading enviroment variables safely with schema validation, simple with virtual module, type-safe with intellisense, and better DX 🔥 🚀 👷. Powered by Zod.
|
|
614
611
|
- [`zod-struct`](https://codeberg.org/reesericci/zod-struct): Create runtime-checked structs with Zod.
|
|
612
|
+
- [`zod-csv`](https://github.com/bartoszgolebiowski/zod-csv): Validation helpers for zod for parsing CSV data.
|
|
613
|
+
- [`fullproduct.dev`](https://fullproduct.dev?identity=freelancers&v=z3): Universal Expo + Next.js App Starter that uses Zod schemas as the single source of truth to keep generated MDX docs, GraphQL, database models, forms, and fetcher functions in sync.
|
|
615
614
|
|
|
616
615
|
#### Utilities for Zod
|
|
617
616
|
|
|
@@ -621,6 +620,8 @@ There are a growing number of tools that are built atop or support Zod natively!
|
|
|
621
620
|
- [`zod-dev`](https://github.com/schalkventer/zod-dev): Conditionally disables Zod runtime parsing in production.
|
|
622
621
|
- [`zod-accelerator`](https://github.com/duplojs/duplojs-zod-accelerator): Accelerates Zod's throughput up to ~100x.
|
|
623
622
|
|
|
623
|
+
<br/>
|
|
624
|
+
|
|
624
625
|
## Installation
|
|
625
626
|
|
|
626
627
|
### Requirements
|
|
@@ -661,6 +662,8 @@ pnpm add zod@canary # pnpm
|
|
|
661
662
|
|
|
662
663
|
> The rest of this README assumes you are using npm and importing directly from the `"zod"` package.
|
|
663
664
|
|
|
665
|
+
<br/>
|
|
666
|
+
|
|
664
667
|
## Basic usage
|
|
665
668
|
|
|
666
669
|
Creating a simple string schema
|
|
@@ -696,6 +699,8 @@ type User = z.infer<typeof User>;
|
|
|
696
699
|
// { username: string }
|
|
697
700
|
```
|
|
698
701
|
|
|
702
|
+
<br/>
|
|
703
|
+
|
|
699
704
|
## Primitives
|
|
700
705
|
|
|
701
706
|
```ts
|
|
@@ -724,6 +729,8 @@ z.unknown();
|
|
|
724
729
|
z.never();
|
|
725
730
|
```
|
|
726
731
|
|
|
732
|
+
<br/>
|
|
733
|
+
|
|
727
734
|
## Coercion for primitives
|
|
728
735
|
|
|
729
736
|
Zod now provides a more convenient way to coerce primitive values.
|
|
@@ -780,6 +787,8 @@ schema.parse(null); // => false
|
|
|
780
787
|
|
|
781
788
|
For more control over coercion logic, consider using [`z.preprocess`](#preprocess) or [`z.pipe()`](#pipe).
|
|
782
789
|
|
|
790
|
+
<br/>
|
|
791
|
+
|
|
783
792
|
## Literals
|
|
784
793
|
|
|
785
794
|
Literal schemas represent a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types), like `"hello world"` or `5`.
|
|
@@ -799,6 +808,8 @@ tuna.value; // "tuna"
|
|
|
799
808
|
|
|
800
809
|
> Currently there is no support for Date literals in Zod. If you have a use case for this feature, please file an issue.
|
|
801
810
|
|
|
811
|
+
<br/>
|
|
812
|
+
|
|
802
813
|
## Strings
|
|
803
814
|
|
|
804
815
|
Zod includes a handful of string-specific validations.
|
|
@@ -997,6 +1008,8 @@ const ipv6Cidr = z.string().cidr({ version: "v6" });
|
|
|
997
1008
|
ipv6Cidr.parse("192.168.1.1"); // fail
|
|
998
1009
|
```
|
|
999
1010
|
|
|
1011
|
+
<br/>
|
|
1012
|
+
|
|
1000
1013
|
## Numbers
|
|
1001
1014
|
|
|
1002
1015
|
You can customize certain error messages when creating a number schema.
|
|
@@ -1035,6 +1048,8 @@ Optionally, you can pass in a second argument to provide a custom error message.
|
|
|
1035
1048
|
z.number().lte(5, { message: "this👏is👏too👏big" });
|
|
1036
1049
|
```
|
|
1037
1050
|
|
|
1051
|
+
<br/>
|
|
1052
|
+
|
|
1038
1053
|
## BigInts
|
|
1039
1054
|
|
|
1040
1055
|
Zod includes a handful of bigint-specific validations.
|
|
@@ -1053,6 +1068,8 @@ z.bigint().nonpositive(); // <= 0n
|
|
|
1053
1068
|
z.bigint().multipleOf(5n); // Evenly divisible by 5n.
|
|
1054
1069
|
```
|
|
1055
1070
|
|
|
1071
|
+
<br/>
|
|
1072
|
+
|
|
1056
1073
|
## NaNs
|
|
1057
1074
|
|
|
1058
1075
|
You can customize certain error messages when creating a nan schema.
|
|
@@ -1064,6 +1081,8 @@ const isNaN = z.nan({
|
|
|
1064
1081
|
});
|
|
1065
1082
|
```
|
|
1066
1083
|
|
|
1084
|
+
<br/>
|
|
1085
|
+
|
|
1067
1086
|
## Booleans
|
|
1068
1087
|
|
|
1069
1088
|
You can customize certain error messages when creating a boolean schema.
|
|
@@ -1075,6 +1094,8 @@ const isActive = z.boolean({
|
|
|
1075
1094
|
});
|
|
1076
1095
|
```
|
|
1077
1096
|
|
|
1097
|
+
<br/>
|
|
1098
|
+
|
|
1078
1099
|
## Dates
|
|
1079
1100
|
|
|
1080
1101
|
Use z.date() to validate `Date` instances.
|
|
@@ -1122,6 +1143,8 @@ console.log(dateSchema.safeParse("0000-00-00").success); // false
|
|
|
1122
1143
|
|
|
1123
1144
|
For older zod versions, use [`z.preprocess`](#preprocess) like [described in this thread](https://github.com/colinhacks/zod/discussions/879#discussioncomment-2036276).
|
|
1124
1145
|
|
|
1146
|
+
<br/>
|
|
1147
|
+
|
|
1125
1148
|
## Zod enums
|
|
1126
1149
|
|
|
1127
1150
|
```ts
|
|
@@ -1177,6 +1200,8 @@ const SalmonAndTrout = FishEnum.extract(["Salmon", "Trout"]);
|
|
|
1177
1200
|
const TunaOnly = FishEnum.exclude(["Salmon", "Trout"]);
|
|
1178
1201
|
```
|
|
1179
1202
|
|
|
1203
|
+
<br/>
|
|
1204
|
+
|
|
1180
1205
|
## Native enums
|
|
1181
1206
|
|
|
1182
1207
|
Zod enums are the recommended approach to defining and validating enums. But if you need to validate against an enum from a third-party library (or you don't want to rewrite your existing enums) you can use `z.nativeEnum()`.
|
|
@@ -1245,6 +1270,8 @@ You can access the underlying object with the `.enum` property:
|
|
|
1245
1270
|
FruitEnum.enum.Apple; // "apple"
|
|
1246
1271
|
```
|
|
1247
1272
|
|
|
1273
|
+
<br/>
|
|
1274
|
+
|
|
1248
1275
|
## Optionals
|
|
1249
1276
|
|
|
1250
1277
|
You can make any schema optional with `z.optional()`. This wraps the schema in a `ZodOptional` instance and returns the result.
|
|
@@ -1273,6 +1300,8 @@ const optionalString = stringSchema.optional();
|
|
|
1273
1300
|
optionalString.unwrap() === stringSchema; // true
|
|
1274
1301
|
```
|
|
1275
1302
|
|
|
1303
|
+
<br/>
|
|
1304
|
+
|
|
1276
1305
|
## Nullables
|
|
1277
1306
|
|
|
1278
1307
|
Similarly, you can create nullable types with `z.nullable()`.
|
|
@@ -1298,6 +1327,8 @@ const nullableString = stringSchema.nullable();
|
|
|
1298
1327
|
nullableString.unwrap() === stringSchema; // true
|
|
1299
1328
|
```
|
|
1300
1329
|
|
|
1330
|
+
<br/>
|
|
1331
|
+
|
|
1301
1332
|
## Objects
|
|
1302
1333
|
|
|
1303
1334
|
```ts
|
|
@@ -1566,6 +1597,8 @@ person.parse({
|
|
|
1566
1597
|
|
|
1567
1598
|
Using `.catchall()` obviates `.passthrough()` , `.strip()` , or `.strict()`. All keys are now considered "known".
|
|
1568
1599
|
|
|
1600
|
+
<br/>
|
|
1601
|
+
|
|
1569
1602
|
## Arrays
|
|
1570
1603
|
|
|
1571
1604
|
```ts
|
|
@@ -1622,6 +1655,8 @@ z.string().array().length(5); // must contain 5 items exactly
|
|
|
1622
1655
|
|
|
1623
1656
|
Unlike `.nonempty()` these methods do not change the inferred type.
|
|
1624
1657
|
|
|
1658
|
+
<br/>
|
|
1659
|
+
|
|
1625
1660
|
## Tuples
|
|
1626
1661
|
|
|
1627
1662
|
Unlike arrays, tuples have a fixed number of elements and each element can have a different type.
|
|
@@ -1647,6 +1682,8 @@ const result = variadicTuple.parse(["hello", 1, 2, 3]);
|
|
|
1647
1682
|
// => [string, ...number[]];
|
|
1648
1683
|
```
|
|
1649
1684
|
|
|
1685
|
+
<br/>
|
|
1686
|
+
|
|
1650
1687
|
## Unions
|
|
1651
1688
|
|
|
1652
1689
|
Zod includes a built-in `z.union` method for composing "OR" types.
|
|
@@ -1682,6 +1719,8 @@ console.log(optionalUrl.safeParse("https://zod.dev").success); // true
|
|
|
1682
1719
|
console.log(optionalUrl.safeParse("not a valid url").success); // false
|
|
1683
1720
|
```
|
|
1684
1721
|
|
|
1722
|
+
<br/>
|
|
1723
|
+
|
|
1685
1724
|
## Discriminated unions
|
|
1686
1725
|
|
|
1687
1726
|
A discriminated union is a union of object schemas that all share a particular key.
|
|
@@ -1724,6 +1763,8 @@ const B = z.discriminatedUnion("status", [
|
|
|
1724
1763
|
const AB = z.discriminatedUnion("status", [...A.options, ...B.options]);
|
|
1725
1764
|
```
|
|
1726
1765
|
|
|
1766
|
+
<br/>
|
|
1767
|
+
|
|
1727
1768
|
## Records
|
|
1728
1769
|
|
|
1729
1770
|
Record schemas are used to validate types such as `Record<string, number>`. This is particularly useful for storing or caching items by ID.
|
|
@@ -1778,6 +1819,8 @@ for (const key in testMap) {
|
|
|
1778
1819
|
|
|
1779
1820
|
As you can see, JavaScript automatically casts all object keys to strings under the hood. Since Zod is trying to bridge the gap between static and runtime types, it doesn't make sense to provide a way of creating a record schema with numerical keys, since there's no such thing as a numerical key in runtime JavaScript.
|
|
1780
1821
|
|
|
1822
|
+
<br/>
|
|
1823
|
+
|
|
1781
1824
|
## Maps
|
|
1782
1825
|
|
|
1783
1826
|
```ts
|
|
@@ -1787,6 +1830,8 @@ type StringNumberMap = z.infer<typeof stringNumberMap>;
|
|
|
1787
1830
|
// type StringNumberMap = Map<string, number>
|
|
1788
1831
|
```
|
|
1789
1832
|
|
|
1833
|
+
<br/>
|
|
1834
|
+
|
|
1790
1835
|
## Sets
|
|
1791
1836
|
|
|
1792
1837
|
```ts
|
|
@@ -1804,6 +1849,8 @@ z.set(z.string()).max(5); // must contain 5 or fewer items
|
|
|
1804
1849
|
z.set(z.string()).size(5); // must contain 5 items exactly
|
|
1805
1850
|
```
|
|
1806
1851
|
|
|
1852
|
+
<br/>
|
|
1853
|
+
|
|
1807
1854
|
## Intersections
|
|
1808
1855
|
|
|
1809
1856
|
Intersections are useful for creating "logical AND" types. This is useful for intersecting two object types.
|
|
@@ -1852,6 +1899,8 @@ type Teacher = z.infer<typeof Teacher>;
|
|
|
1852
1899
|
// { id:string; name:string };
|
|
1853
1900
|
``` -->
|
|
1854
1901
|
|
|
1902
|
+
<br/>
|
|
1903
|
+
|
|
1855
1904
|
## Recursive types
|
|
1856
1905
|
|
|
1857
1906
|
You can define a recursive schema in Zod, but because of a limitation of TypeScript, their type can't be statically inferred. Instead you'll need to define the type definition manually, and provide it to Zod as a "type hint".
|
|
@@ -1942,6 +1991,8 @@ Despite supporting recursive schemas, passing cyclical data into Zod will cause
|
|
|
1942
1991
|
|
|
1943
1992
|
> To detect cyclical objects before they cause problems, consider [this approach](https://gist.github.com/colinhacks/d35825e505e635df27cc950776c5500b).
|
|
1944
1993
|
|
|
1994
|
+
<br/>
|
|
1995
|
+
|
|
1945
1996
|
## Promises
|
|
1946
1997
|
|
|
1947
1998
|
```ts
|
|
@@ -1973,6 +2024,8 @@ const test = async () => {
|
|
|
1973
2024
|
|
|
1974
2025
|
When "parsing" a promise, Zod checks that the passed value is an object with `.then` and `.catch` methods — that's it. So you should be able to pass non-native Promises (Bluebird, etc) into `z.promise(...).parse` with no trouble. One gotcha: the return type of the parse function will be a _native_ `Promise` , so if you have downstream logic that uses non-standard Promise methods, this won't work. -->
|
|
1975
2026
|
|
|
2027
|
+
<br/>
|
|
2028
|
+
|
|
1976
2029
|
## Instanceof
|
|
1977
2030
|
|
|
1978
2031
|
You can use `z.instanceof` to check that the input is an instance of a class. This is useful to validate inputs against classes that are exported from third-party libraries.
|
|
@@ -1989,6 +2042,8 @@ TestSchema.parse(new Test()); // passes
|
|
|
1989
2042
|
TestSchema.parse(blob); // throws
|
|
1990
2043
|
```
|
|
1991
2044
|
|
|
2045
|
+
<br/>
|
|
2046
|
+
|
|
1992
2047
|
## Functions
|
|
1993
2048
|
|
|
1994
2049
|
Zod also lets you define "function schemas". This makes it easy to validate the inputs and outputs of a function without intermixing your validation code and "business logic".
|
|
@@ -2072,6 +2127,8 @@ myFunction.returnType();
|
|
|
2072
2127
|
* `args: ZodTuple` The first argument is a tuple (created with `z.tuple([...])` and defines the schema of the arguments to your function. If the function doesn't accept arguments, you can pass an empty tuple (`z.tuple([])`).
|
|
2073
2128
|
* `returnType: any Zod schema` The second argument is the function's return type. This can be any Zod schema. -->
|
|
2074
2129
|
|
|
2130
|
+
<br/>
|
|
2131
|
+
|
|
2075
2132
|
## Preprocess
|
|
2076
2133
|
|
|
2077
2134
|
> Zod now supports primitive coercion without the need for `.preprocess()`. See the [coercion docs](#coercion-for-primitives) for more information.
|
|
@@ -2086,6 +2143,8 @@ const castToString = z.preprocess((val) => String(val), z.string());
|
|
|
2086
2143
|
|
|
2087
2144
|
This returns a `ZodEffects` instance. `ZodEffects` is a wrapper class that contains all logic pertaining to preprocessing, refinements, and transforms.
|
|
2088
2145
|
|
|
2146
|
+
<br/>
|
|
2147
|
+
|
|
2089
2148
|
## Custom schemas
|
|
2090
2149
|
|
|
2091
2150
|
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.
|
|
@@ -2113,6 +2172,8 @@ You can customize the error message and other options by passing a second argume
|
|
|
2113
2172
|
z.custom<...>((val) => ..., "custom error message");
|
|
2114
2173
|
```
|
|
2115
2174
|
|
|
2175
|
+
<br/>
|
|
2176
|
+
|
|
2116
2177
|
## Schema methods
|
|
2117
2178
|
|
|
2118
2179
|
All Zod schemas contain certain methods.
|
|
@@ -2698,61 +2759,7 @@ z.string()
|
|
|
2698
2759
|
|
|
2699
2760
|
The `.pipe()` method returns a `ZodPipeline` instance.
|
|
2700
2761
|
|
|
2701
|
-
|
|
2702
|
-
|
|
2703
|
-
You can constrain the input to types that work well with your chosen coercion. Then use `.pipe()` to apply the coercion.
|
|
2704
|
-
|
|
2705
|
-
without constrained input:
|
|
2706
|
-
|
|
2707
|
-
```ts
|
|
2708
|
-
const toDate = z.coerce.date();
|
|
2709
|
-
|
|
2710
|
-
// works intuitively
|
|
2711
|
-
console.log(toDate.safeParse("2023-01-01").success); // true
|
|
2712
|
-
|
|
2713
|
-
// might not be what you want
|
|
2714
|
-
console.log(toDate.safeParse(null).success); // true
|
|
2715
|
-
```
|
|
2716
|
-
|
|
2717
|
-
with constrained input:
|
|
2718
|
-
|
|
2719
|
-
```ts
|
|
2720
|
-
const datelike = z.union([z.number(), z.string(), z.date()]);
|
|
2721
|
-
const datelikeToDate = datelike.pipe(z.coerce.date());
|
|
2722
|
-
|
|
2723
|
-
// still works intuitively
|
|
2724
|
-
console.log(datelikeToDate.safeParse("2023-01-01").success); // true
|
|
2725
|
-
|
|
2726
|
-
// more likely what you want
|
|
2727
|
-
console.log(datelikeToDate.safeParse(null).success); // false
|
|
2728
|
-
```
|
|
2729
|
-
|
|
2730
|
-
You can also use this technique to avoid coercions that throw uncaught errors.
|
|
2731
|
-
|
|
2732
|
-
without constrained input:
|
|
2733
|
-
|
|
2734
|
-
```ts
|
|
2735
|
-
const toBigInt = z.coerce.bigint();
|
|
2736
|
-
|
|
2737
|
-
// works intuitively
|
|
2738
|
-
console.log(toBigInt.safeParse("42")); // true
|
|
2739
|
-
|
|
2740
|
-
// probably not what you want
|
|
2741
|
-
console.log(toBigInt.safeParse(null)); // throws uncaught error
|
|
2742
|
-
```
|
|
2743
|
-
|
|
2744
|
-
with constrained input:
|
|
2745
|
-
|
|
2746
|
-
```ts
|
|
2747
|
-
const toNumber = z.number().or(z.string()).pipe(z.coerce.number());
|
|
2748
|
-
const toBigInt = z.bigint().or(toNumber).pipe(z.coerce.bigint());
|
|
2749
|
-
|
|
2750
|
-
// still works intuitively
|
|
2751
|
-
console.log(toBigInt.safeParse("42").success); // true
|
|
2752
|
-
|
|
2753
|
-
// error handled by zod, more likely what you want
|
|
2754
|
-
console.log(toBigInt.safeParse(null).success); // false
|
|
2755
|
-
```
|
|
2762
|
+
<br/>
|
|
2756
2763
|
|
|
2757
2764
|
## Guides and concepts
|
|
2758
2765
|
|
|
@@ -2923,6 +2930,8 @@ if (!result.success) {
|
|
|
2923
2930
|
}
|
|
2924
2931
|
```
|
|
2925
2932
|
|
|
2933
|
+
<br/>
|
|
2934
|
+
|
|
2926
2935
|
## Comparison
|
|
2927
2936
|
|
|
2928
2937
|
There are a handful of other widely-used validation libraries, but all of them have certain design limitations that make for a non-ideal developer experience.
|
|
@@ -3069,6 +3078,8 @@ Ow is focused on function input validation. It's a library that makes it easy to
|
|
|
3069
3078
|
|
|
3070
3079
|
If you want to validate function inputs, use function schemas in Zod! It's a much simpler approach that lets you reuse a function type declaration without repeating yourself (namely, copy-pasting a bunch of ow assertions at the beginning of every function). Also Zod lets you validate your return types as well, so you can be sure there won't be any unexpected data passed downstream.
|
|
3071
3080
|
|
|
3081
|
+
<br/>
|
|
3082
|
+
|
|
3072
3083
|
## Changelog
|
|
3073
3084
|
|
|
3074
3085
|
View the changelog at [CHANGELOG.md](CHANGELOG.md)
|
package/lib/helpers/util.d.ts
CHANGED
|
@@ -22,7 +22,7 @@ export declare namespace util {
|
|
|
22
22
|
export {};
|
|
23
23
|
}
|
|
24
24
|
export declare namespace objectUtil {
|
|
25
|
-
export type MergeShapes<U, V> = {
|
|
25
|
+
export type MergeShapes<U, V> = keyof U & keyof V extends never ? U & V : {
|
|
26
26
|
[k in Exclude<keyof U, keyof V>]: U[k];
|
|
27
27
|
} & V;
|
|
28
28
|
type optionalKeys<T extends object> = {
|
|
@@ -49,7 +49,7 @@ export declare namespace objectUtil {
|
|
|
49
49
|
[k in noNeverKeys<T>]: k extends keyof T ? T[k] : never;
|
|
50
50
|
}>;
|
|
51
51
|
export const mergeShapes: <U, T>(first: U, second: T) => T & U;
|
|
52
|
-
export type extendShape<A extends object, B extends object> = {
|
|
52
|
+
export type extendShape<A extends object, B extends object> = keyof A & keyof B extends never ? A & B : {
|
|
53
53
|
[K in keyof A as K extends keyof B ? never : K]: A[K];
|
|
54
54
|
} & {
|
|
55
55
|
[K in keyof B]: B[K];
|