zod 3.12.1 → 3.13.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 +44 -33
- package/lib/ZodError.d.ts +0 -1
- package/lib/ZodError.js +0 -1
- package/lib/benchmarks/discriminatedUnion.d.ts +0 -1
- package/lib/benchmarks/discriminatedUnion.js +0 -1
- package/lib/benchmarks/index.d.ts +0 -1
- package/lib/benchmarks/index.js +0 -1
- package/lib/benchmarks/object.d.ts +0 -1
- package/lib/benchmarks/object.js +0 -1
- package/lib/benchmarks/string.d.ts +0 -1
- package/lib/benchmarks/string.js +0 -1
- package/lib/benchmarks/union.d.ts +0 -1
- package/lib/benchmarks/union.js +0 -1
- package/lib/external.d.ts +0 -1
- package/lib/external.js +5 -2
- package/lib/helpers/errorUtil.d.ts +0 -1
- package/lib/helpers/errorUtil.js +0 -1
- package/lib/helpers/parseUtil.d.ts +0 -1
- package/lib/helpers/parseUtil.js +0 -1
- package/lib/helpers/partialUtil.d.ts +0 -1
- package/lib/helpers/partialUtil.js +0 -1
- package/lib/helpers/typeAliases.d.ts +0 -1
- package/lib/helpers/typeAliases.js +0 -1
- package/lib/helpers/util.d.ts +0 -1
- package/lib/helpers/util.js +0 -1
- package/lib/index.d.ts +0 -1
- package/lib/index.js +5 -2
- package/lib/index.mjs +3415 -2
- package/lib/types.d.ts +19 -7
- package/lib/types.js +37 -6
- package/package.json +4 -4
- package/lib/ZodError.d.ts.map +0 -1
- package/lib/ZodError.js.map +0 -1
- package/lib/benchmarks/discriminatedUnion.d.ts.map +0 -1
- package/lib/benchmarks/discriminatedUnion.js.map +0 -1
- package/lib/benchmarks/index.d.ts.map +0 -1
- package/lib/benchmarks/index.js.map +0 -1
- package/lib/benchmarks/object.d.ts.map +0 -1
- package/lib/benchmarks/object.js.map +0 -1
- package/lib/benchmarks/string.d.ts.map +0 -1
- package/lib/benchmarks/string.js.map +0 -1
- package/lib/benchmarks/union.d.ts.map +0 -1
- package/lib/benchmarks/union.js.map +0 -1
- package/lib/external.d.ts.map +0 -1
- package/lib/external.js.map +0 -1
- package/lib/helpers/errorUtil.d.ts.map +0 -1
- package/lib/helpers/errorUtil.js.map +0 -1
- package/lib/helpers/parseUtil.d.ts.map +0 -1
- package/lib/helpers/parseUtil.js.map +0 -1
- package/lib/helpers/partialUtil.d.ts.map +0 -1
- package/lib/helpers/partialUtil.js.map +0 -1
- package/lib/helpers/typeAliases.d.ts.map +0 -1
- package/lib/helpers/typeAliases.js.map +0 -1
- package/lib/helpers/util.d.ts.map +0 -1
- package/lib/helpers/util.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/index.mjs.map +0 -1
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js.map +0 -1
package/README.md
CHANGED
|
@@ -10,23 +10,19 @@
|
|
|
10
10
|
<a href="./src/__tests__" rel="nofollow"><img src="./coverage.svg" alt="coverage"></a>
|
|
11
11
|
<a href="https://discord.gg/KaSRdyX2vc" rel="nofollow"><img src="https://img.shields.io/discord/893487829802418277?label=Discord&logo=discord&logoColor=white" alt="discord server"></a>
|
|
12
12
|
</p>
|
|
13
|
-
<p align="center">
|
|
14
|
-
by <a href="https://twitter.com/colinhacks">@colinhacks</a>
|
|
15
|
-
</p>
|
|
16
13
|
|
|
17
|
-
>
|
|
18
|
-
>
|
|
19
|
-
>
|
|
20
|
-
>
|
|
21
|
-
>
|
|
22
|
-
|
|
23
|
-
<
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
</h3>
|
|
14
|
+
<div align="center">
|
|
15
|
+
<a href="https://discord.gg/RcG33DQJdf">Discord</a>
|
|
16
|
+
<span> • </span>
|
|
17
|
+
<a href="https://www.npmjs.com/package/zod">NPM</a>
|
|
18
|
+
<span> • </span>
|
|
19
|
+
<a href="https://github.com/colinhacks/zod/issues/new">Issues</a>
|
|
20
|
+
<span> • </span>
|
|
21
|
+
<a href="https://twitter.com/colinhacks">@colinhacks</a>
|
|
22
|
+
<span> • </span>
|
|
23
|
+
<a href="https://trpc.io">tRPC</a>
|
|
24
|
+
<br />
|
|
25
|
+
</div>
|
|
30
26
|
|
|
31
27
|
<br/>
|
|
32
28
|
|
|
@@ -34,6 +30,10 @@ These docs have been translated into [Chinese](./README_ZH.md).
|
|
|
34
30
|
|
|
35
31
|
# Table of contents
|
|
36
32
|
|
|
33
|
+
The full documentation is available both on the [official documentation site](https://zod.js.org/) (recommended) and in `README.md`.
|
|
34
|
+
|
|
35
|
+
### Go to [zod.js.org](https://zod.js.org) >>
|
|
36
|
+
|
|
37
37
|
- [What is Zod](#what-is-zod)
|
|
38
38
|
- [Installation](#installation)
|
|
39
39
|
- [Ecosystem](#ecosystem)
|
|
@@ -43,6 +43,7 @@ These docs have been translated into [Chinese](./README_ZH.md).
|
|
|
43
43
|
- [Literals](#literals)
|
|
44
44
|
- [Strings](#strings)
|
|
45
45
|
- [Numbers](#numbers)
|
|
46
|
+
- [NaNs](#nans)
|
|
46
47
|
- [Booleans](#booleans)
|
|
47
48
|
- [Dates](#dates)
|
|
48
49
|
- [Zod enums](#zod-enums)
|
|
@@ -244,7 +245,7 @@ There are a growing number of tools that are built atop or support Zod natively!
|
|
|
244
245
|
### Form integrations
|
|
245
246
|
|
|
246
247
|
- [`react-hook-form`](https://github.com/react-hook-form/resolvers#zod): A first-party Zod resolver for React Hook Form
|
|
247
|
-
- [`formik`](https://github.com/robertLichtnow/zod-formik-adapter): A community-maintained Formik adapter for Zod
|
|
248
|
+
- [`zod-formik-adapter`](https://github.com/robertLichtnow/zod-formik-adapter): A community-maintained Formik adapter for Zod
|
|
248
249
|
|
|
249
250
|
# Basic usage
|
|
250
251
|
|
|
@@ -403,6 +404,17 @@ Optionally, you can pass in a second argument to provide a custom error message.
|
|
|
403
404
|
z.number().lte(5, { message: "this👏is👏too👏big" });
|
|
404
405
|
```
|
|
405
406
|
|
|
407
|
+
## NaNs
|
|
408
|
+
|
|
409
|
+
You can customize certain error messages when creating a nan schema.
|
|
410
|
+
|
|
411
|
+
```ts
|
|
412
|
+
const isNaN = z.nan({
|
|
413
|
+
required_error: "isNaN is required",
|
|
414
|
+
invalid_type_error: "isNaN must be not a number",
|
|
415
|
+
});
|
|
416
|
+
```
|
|
417
|
+
|
|
406
418
|
## Booleans
|
|
407
419
|
|
|
408
420
|
You can customize certain error messages when creating a boolean schema.
|
|
@@ -415,26 +427,25 @@ const isActive = z.boolean({
|
|
|
415
427
|
```
|
|
416
428
|
|
|
417
429
|
## Dates
|
|
430
|
+
|
|
418
431
|
z.date() accepts a date, not a date string
|
|
432
|
+
|
|
419
433
|
```ts
|
|
420
|
-
z.date().safeParse(
|
|
421
|
-
z.date().safeParse(
|
|
434
|
+
z.date().safeParse(new Date()); // success: true
|
|
435
|
+
z.date().safeParse("2022-01-12T00:00:00.000Z"); // success: false
|
|
422
436
|
```
|
|
423
437
|
|
|
424
438
|
To allow for dates or date strings, you can use preprocess
|
|
439
|
+
|
|
425
440
|
```ts
|
|
426
|
-
const dateSchema = z.preprocess(
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
},
|
|
431
|
-
z.date()
|
|
432
|
-
)
|
|
433
|
-
type DateSchema = z.infer<typeof dateSchema>
|
|
441
|
+
const dateSchema = z.preprocess((arg) => {
|
|
442
|
+
if (typeof arg == "string" || arg instanceof Date) return new Date(arg);
|
|
443
|
+
}, z.date());
|
|
444
|
+
type DateSchema = z.infer<typeof dateSchema>;
|
|
434
445
|
// type DateSchema = Date
|
|
435
446
|
|
|
436
|
-
dateSchema.safeParse(
|
|
437
|
-
dateSchema.safeParse(
|
|
447
|
+
dateSchema.safeParse(new Date("1/12/22")); // success: true
|
|
448
|
+
dateSchema.safeParse("2022-01-12T00:00:00.000Z"); // success: true
|
|
438
449
|
```
|
|
439
450
|
|
|
440
451
|
## Zod enums
|
|
@@ -1066,8 +1077,8 @@ interface Category {
|
|
|
1066
1077
|
subcategories: Category[];
|
|
1067
1078
|
}
|
|
1068
1079
|
|
|
1069
|
-
// cast to z.
|
|
1070
|
-
const Category: z.
|
|
1080
|
+
// cast to z.ZodType<Category>
|
|
1081
|
+
const Category: z.ZodType<Category> = z.lazy(() =>
|
|
1071
1082
|
z.object({
|
|
1072
1083
|
name: z.string(),
|
|
1073
1084
|
subcategories: z.array(Category),
|
|
@@ -1104,7 +1115,7 @@ interface Category extends z.infer<typeof BaseCategory> {
|
|
|
1104
1115
|
|
|
1105
1116
|
// merge the base schema with
|
|
1106
1117
|
// a new Zod schema containing relations
|
|
1107
|
-
const Category: z.
|
|
1118
|
+
const Category: z.ZodType<Category> = BaseCategory.merge(
|
|
1108
1119
|
z.object({
|
|
1109
1120
|
subcategories: z.lazy(() => z.array(Category)),
|
|
1110
1121
|
})
|
|
@@ -1119,7 +1130,7 @@ If you want to validate any JSON value, you can use the snippet below.
|
|
|
1119
1130
|
type Literal = boolean | null | number | string;
|
|
1120
1131
|
type Json = Literal | { [key: string]: Json } | Json[];
|
|
1121
1132
|
const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]);
|
|
1122
|
-
const jsonSchema: z.
|
|
1133
|
+
const jsonSchema: z.ZodType<Json> = z.lazy(() =>
|
|
1123
1134
|
z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)])
|
|
1124
1135
|
);
|
|
1125
1136
|
|
package/lib/ZodError.d.ts
CHANGED
package/lib/ZodError.js
CHANGED
package/lib/benchmarks/index.js
CHANGED
package/lib/benchmarks/object.js
CHANGED
package/lib/benchmarks/string.js
CHANGED
package/lib/benchmarks/union.js
CHANGED
package/lib/external.d.ts
CHANGED
package/lib/external.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -14,4 +18,3 @@ __exportStar(require("./helpers/parseUtil"), exports);
|
|
|
14
18
|
__exportStar(require("./helpers/typeAliases"), exports);
|
|
15
19
|
__exportStar(require("./types"), exports);
|
|
16
20
|
__exportStar(require("./ZodError"), exports);
|
|
17
|
-
//# sourceMappingURL=external.js.map
|
package/lib/helpers/errorUtil.js
CHANGED
|
@@ -98,4 +98,3 @@ export declare const isAborted: (x: ParseReturnType<any>) => x is INVALID;
|
|
|
98
98
|
export declare const isDirty: <T>(x: ParseReturnType<T>) => x is OK<T> | DIRTY<T>;
|
|
99
99
|
export declare const isValid: <T>(x: ParseReturnType<T>) => x is OK<T> | DIRTY<T>;
|
|
100
100
|
export declare const isAsync: <T>(x: ParseReturnType<T>) => x is AsyncParseReturnType<T>;
|
|
101
|
-
//# sourceMappingURL=parseUtil.d.ts.map
|
package/lib/helpers/parseUtil.js
CHANGED
package/lib/helpers/util.d.ts
CHANGED
package/lib/helpers/util.js
CHANGED
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -27,4 +31,3 @@ var mod = __importStar(require("./external"));
|
|
|
27
31
|
exports.z = mod;
|
|
28
32
|
__exportStar(require("./external"), exports);
|
|
29
33
|
exports.default = mod;
|
|
30
|
-
//# sourceMappingURL=index.js.map
|