zod 3.14.2 → 3.14.5
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 +163 -53
- package/index.d.ts +2 -0
- package/lib/ZodError.d.ts +23 -22
- package/lib/ZodError.js +53 -45
- package/lib/helpers/parseUtil.d.ts +0 -1
- package/lib/index.mjs +83 -95
- package/lib/index.umd.js +2983 -0
- package/lib/types.d.ts +13 -37
- package/lib/types.js +30 -50
- package/package.json +27 -33
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
<h1 align="center">Zod</h1>
|
|
4
4
|
</p>
|
|
5
5
|
<p align="center">
|
|
6
|
-
<a href="https://github.com/
|
|
6
|
+
<a href="https://github.com/colinhacks/zod/actions?query=branch%3Amaster"><img src="https://github.com/colinhacks/zod/actions/workflows/test.yml/badge.svg?event=push&branch=master" alt="Zod CI status" /></a>
|
|
7
7
|
<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>
|
|
8
8
|
<a href="https://opensource.org/licenses/MIT" rel="nofollow"><img src="https://img.shields.io/github/license/colinhacks/zod" alt="License"></a>
|
|
9
9
|
<a href="https://www.npmjs.com/package/zod" rel="nofollow"><img src="https://img.shields.io/npm/dw/zod.svg" alt="npm"></a>
|
|
@@ -94,8 +94,10 @@ These docs have been translated into [Chinese](./README_ZH.md).
|
|
|
94
94
|
- [.promise](#promise)
|
|
95
95
|
- [.or](#or)
|
|
96
96
|
- [.and](#and)
|
|
97
|
-
- [
|
|
98
|
-
- [
|
|
97
|
+
- [Guides and concepts](#guides-and-concepts)
|
|
98
|
+
- [Type inference](#type-inference)
|
|
99
|
+
- [Writing generic functions](#writing-generic-functions)
|
|
100
|
+
- [Error handling](#error-handling)
|
|
99
101
|
- [Comparison](#comparison)
|
|
100
102
|
- [Joi](#joi)
|
|
101
103
|
- [Yup](#yup)
|
|
@@ -123,76 +125,123 @@ Some other great aspects:
|
|
|
123
125
|
|
|
124
126
|
# Sponsorship
|
|
125
127
|
|
|
126
|
-
Sponsorship at any level is appreciated and encouraged.
|
|
128
|
+
Sponsorship at any level is appreciated and encouraged. For individual developers, consider the [Cup of Coffee tier](https://github.com/sponsors/colinhacks). If you built a paid product using Zod, consider one of the [podium tiers](https://github.com/sponsors/colinhacks).
|
|
127
129
|
|
|
128
|
-
###
|
|
130
|
+
### Gold
|
|
129
131
|
|
|
130
132
|
<table>
|
|
131
133
|
<tr>
|
|
132
|
-
|
|
134
|
+
<td align="center">
|
|
135
|
+
<a href="https://astro.build/">
|
|
136
|
+
<img src="https://avatars.githubusercontent.com/u/44914786?s=200&v=4" width="200px;" alt="" />
|
|
137
|
+
</a>
|
|
138
|
+
<br />
|
|
139
|
+
<b>Astro</b>
|
|
140
|
+
<br />
|
|
141
|
+
<a href="https://astro.build">astro.build</a>
|
|
142
|
+
<br />
|
|
143
|
+
<p width="200px">
|
|
144
|
+
Astro is a new kind of static <br/>
|
|
145
|
+
site builder for the modern web. <br/>
|
|
146
|
+
Powerful developer experience meets <br/>
|
|
147
|
+
lightweight output.</p>
|
|
148
|
+
</td>
|
|
149
|
+
<td align="center">
|
|
150
|
+
<a href="https://glow.app/">
|
|
151
|
+
<img src="https://i.imgur.com/R0R43S2.jpg" width="200px;" alt="" />
|
|
152
|
+
</a>
|
|
153
|
+
<br />
|
|
154
|
+
<b>Glow Wallet</b>
|
|
155
|
+
<br />
|
|
156
|
+
<a href="https://glow.app/">glow.app</a>
|
|
157
|
+
<br />
|
|
158
|
+
<p width="200px">Your new favorite
|
|
159
|
+
<br/>
|
|
160
|
+
Solana wallet.</p>
|
|
161
|
+
</td>
|
|
162
|
+
</tr>
|
|
163
|
+
<tr>
|
|
164
|
+
<td align="center">
|
|
133
165
|
<a href="https://deletype.com/">
|
|
134
|
-
<img src="https://avatars0.githubusercontent.com/u/15068039?s=200&v=4" width="
|
|
166
|
+
<img src="https://avatars0.githubusercontent.com/u/15068039?s=200&v=4" width="200px;" alt="" />
|
|
135
167
|
</a>
|
|
136
|
-
<br
|
|
168
|
+
<br />
|
|
137
169
|
<b>Deletype</b>
|
|
138
|
-
<br
|
|
139
|
-
<a href="https://deletype.com
|
|
170
|
+
<br />
|
|
171
|
+
<a href="https://deletype.com">deletype.com</a>
|
|
140
172
|
</td>
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
173
|
+
</tr>
|
|
174
|
+
</table>
|
|
175
|
+
|
|
176
|
+
### Silver
|
|
177
|
+
|
|
178
|
+
<table>
|
|
179
|
+
<tr>
|
|
180
|
+
<td align="center">
|
|
181
|
+
<a href="https://snaplet.dev">
|
|
182
|
+
<img src="https://avatars.githubusercontent.com/u/69029941?s=200&v=4" width="150px;" alt="" />
|
|
144
183
|
</a>
|
|
145
|
-
<br
|
|
146
|
-
<b>
|
|
147
|
-
<br
|
|
148
|
-
<a href="https://
|
|
184
|
+
<br />
|
|
185
|
+
<b>Snaplet</b>
|
|
186
|
+
<br />
|
|
187
|
+
<a href="https://snaplet.dev">snaplet.dev</a>
|
|
188
|
+
</td>
|
|
189
|
+
<td align="center">
|
|
190
|
+
<a href="https://marcatopartners.com/">
|
|
191
|
+
<img src="https://avatars.githubusercontent.com/u/84106192?s=200&v=4" width="150px;" alt="Marcato Partners" />
|
|
192
|
+
</a>
|
|
193
|
+
<br />
|
|
194
|
+
<b>Marcato Partners</b>
|
|
195
|
+
<br />
|
|
196
|
+
<a href="https://marcatopartners.com/">marcatopartners.com</a>
|
|
197
|
+
</td>
|
|
198
|
+
<td align="center">
|
|
199
|
+
<a href="https://github.com/macandcheese-spaghetticode">
|
|
200
|
+
<img src="https://avatars.githubusercontent.com/u/76997592?v=4" width="150px;" alt="Trip" />
|
|
201
|
+
</a>
|
|
202
|
+
<br />
|
|
203
|
+
<b>Trip</b>
|
|
149
204
|
</td>
|
|
205
|
+
</tr>
|
|
206
|
+
</table>
|
|
207
|
+
|
|
208
|
+
### Bronze
|
|
209
|
+
|
|
210
|
+
<table>
|
|
211
|
+
<tr>
|
|
150
212
|
<td align="center">
|
|
151
213
|
<a href="https://twitter.com/flybayer">
|
|
152
214
|
<img src="https://avatars2.githubusercontent.com/u/8813276?s=460&u=4ff8beb9a67b173015c4b426a92d89cab960af1b&v=4" width="100px;" alt=""/>
|
|
153
215
|
</a>
|
|
154
|
-
<br
|
|
216
|
+
<br />
|
|
155
217
|
<b>Brandon Bayer</b>
|
|
156
218
|
<br/>
|
|
157
219
|
<a href="https://twitter.com/flybayer">@flybayer</a>,
|
|
158
220
|
<span>creator of <a href="https://blitzjs.com">Blitz.js</a></span>
|
|
159
221
|
<br />
|
|
160
222
|
</td>
|
|
161
|
-
|
|
162
|
-
</tr>
|
|
163
|
-
<tr>
|
|
164
|
-
<td align="center">
|
|
165
|
-
<a href="https://www.bamboocreative.nz/">
|
|
166
|
-
<img src="https://avatars1.githubusercontent.com/u/41406870?s=460&v=4" width="100px;" alt="" />
|
|
167
|
-
</a>
|
|
168
|
-
<br>
|
|
169
|
-
<b>Bamboo Creative</b>
|
|
170
|
-
<br>
|
|
171
|
-
<a href="https://www.bamboocreative.nz/">bamboocreative.nz</a>
|
|
172
|
-
</td>
|
|
173
223
|
<td align="center">
|
|
174
|
-
<a href="https://github.com/
|
|
175
|
-
<img src="https://avatars.githubusercontent.com/u/
|
|
224
|
+
<a href="https://github.com/brabeji">
|
|
225
|
+
<img src="https://avatars.githubusercontent.com/u/2237954?v=4" width="100px;" alt=""/>
|
|
176
226
|
</a>
|
|
177
|
-
<br
|
|
178
|
-
<b>
|
|
179
|
-
<br
|
|
180
|
-
<a href="https://github.com/
|
|
227
|
+
<br />
|
|
228
|
+
<b>Jiří Brabec</b>
|
|
229
|
+
<br/>
|
|
230
|
+
<a href="https://github.com/brabeji">@brabeji</a>
|
|
231
|
+
<br />
|
|
181
232
|
</td>
|
|
182
233
|
<td align="center">
|
|
183
|
-
<a href="https://
|
|
184
|
-
<img src="https://avatars.githubusercontent.com/u/
|
|
234
|
+
<a href="https://twitter.com/alexdotjs">
|
|
235
|
+
<img src="https://avatars.githubusercontent.com/u/459267?v=4" width="100px;" alt="" />
|
|
185
236
|
</a>
|
|
186
|
-
<br
|
|
187
|
-
<b>
|
|
188
|
-
<br
|
|
189
|
-
<a href="https://
|
|
237
|
+
<br />
|
|
238
|
+
<b>Alex Johansson</b>
|
|
239
|
+
<br />
|
|
240
|
+
<a href="https://twitter.com/alexdotjs">@alexdotjs</a>
|
|
190
241
|
</td>
|
|
191
242
|
</tr>
|
|
192
243
|
</table>
|
|
193
244
|
|
|
194
|
-
_To get your name + Twitter + website here, sponsor Zod at the [Freelancer](https://github.com/sponsors/colinhacks) or [Consultancy](https://github.com/sponsors/colinhacks) tier._
|
|
195
|
-
|
|
196
245
|
# Installation
|
|
197
246
|
|
|
198
247
|
To install Zod v3:
|
|
@@ -241,6 +290,9 @@ There are a growing number of tools that are built atop or support Zod natively!
|
|
|
241
290
|
- [`soly`](https://github.com/mdbetancourt/soly): Create CLI applications with zod.
|
|
242
291
|
- [`graphql-codegen-typescript-validation-schema`](https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema): GraphQL Code Generator plugin to generate form validation schema from your GraphQL schema
|
|
243
292
|
- [`zod-prisma`](https://github.com/CarterGrimmeisen/zod-prisma): Generate Zod schemas from your Prisma schema.
|
|
293
|
+
- [`fastify-type-provider-zod`](https://github.com/turkerdev/fastify-type-provider-zod): Create Fastify type providers from Zod schemas
|
|
294
|
+
- [`Supervillain`](https://github.com/Southclaws/supervillain): Generate Zod schemas from your Go structs
|
|
295
|
+
- [`zod-to-openapi`](https://github.com/asteasolutions/zod-to-openapi): Generate full OpenAPI (Swagger) docs from Zod, including schemas, endpoints & parameters
|
|
244
296
|
|
|
245
297
|
### Form integrations
|
|
246
298
|
|
|
@@ -1134,9 +1186,9 @@ const Category: z.ZodType<Category> = BaseCategory.merge(
|
|
|
1134
1186
|
If you want to validate any JSON value, you can use the snippet below.
|
|
1135
1187
|
|
|
1136
1188
|
```ts
|
|
1137
|
-
type Literal = boolean | null | number | string;
|
|
1138
|
-
type Json = Literal | { [key: string]: Json } | Json[];
|
|
1139
1189
|
const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()]);
|
|
1190
|
+
type Literal = z.infer<typeof literalSchema>;
|
|
1191
|
+
type Json = Literal | { [key: string]: Json } | Json[];
|
|
1140
1192
|
const jsonSchema: z.ZodType<Json> = z.lazy(() =>
|
|
1141
1193
|
z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)])
|
|
1142
1194
|
);
|
|
@@ -1687,7 +1739,9 @@ z.object({ name: z.string() }).and(z.object({ age: z.number() })); // { name: st
|
|
|
1687
1739
|
z.intersection(z.object({ name: z.string() }), z.object({ age: z.number() }));
|
|
1688
1740
|
```
|
|
1689
1741
|
|
|
1690
|
-
#
|
|
1742
|
+
# Guides and concepts
|
|
1743
|
+
|
|
1744
|
+
## Type inference
|
|
1691
1745
|
|
|
1692
1746
|
You can extract the TypeScript type of any schema with `z.infer<typeof mySchema>` .
|
|
1693
1747
|
|
|
@@ -1716,16 +1770,72 @@ type output = z.output<typeof stringToNumber>; // number
|
|
|
1716
1770
|
type inferred = z.infer<typeof stringToNumber>; // number
|
|
1717
1771
|
```
|
|
1718
1772
|
|
|
1719
|
-
|
|
1773
|
+
## Writing generic functions
|
|
1774
|
+
|
|
1775
|
+
When attempting to write a functions that accepts a Zod schemas as an input, it's common to try something like this:
|
|
1776
|
+
|
|
1777
|
+
```ts
|
|
1778
|
+
function makeSchemaOptional<T>(schema: z.ZodType<T>) {
|
|
1779
|
+
return schema.optional();
|
|
1780
|
+
}
|
|
1781
|
+
```
|
|
1782
|
+
|
|
1783
|
+
This approach has some issues. The `schema` variable in this function is typed as an instance of `ZodType`, which is an abstract class that all Zod schemas inherit from. This approach loses type information, namely _which subclass_ the input actually is.
|
|
1784
|
+
|
|
1785
|
+
```ts
|
|
1786
|
+
const arg = makeSchemaOptional(z.string());
|
|
1787
|
+
arg.unwrap();
|
|
1788
|
+
```
|
|
1789
|
+
|
|
1790
|
+
A better approach is for the generate parameter to refer to _the schema as a whole_.
|
|
1791
|
+
|
|
1792
|
+
```ts
|
|
1793
|
+
function makeSchemaOptional<T extends z.ZodTypeAny>(schema: T) {
|
|
1794
|
+
return schema.optional();
|
|
1795
|
+
}
|
|
1796
|
+
```
|
|
1797
|
+
|
|
1798
|
+
> `ZodTypeAny` is just a shorthand for `ZodType<any, any, any>`, a type that is broad enough to match any Zod schema.
|
|
1799
|
+
|
|
1800
|
+
As you can see, `schema` is now fully and properly typed.
|
|
1801
|
+
|
|
1802
|
+
```ts
|
|
1803
|
+
const arg = makeSchemaOptional(z.string());
|
|
1804
|
+
arg.unwrap(); // ZodString
|
|
1805
|
+
```
|
|
1806
|
+
|
|
1807
|
+
### Restricting valid schemas
|
|
1808
|
+
|
|
1809
|
+
The `ZodType` class has three generic parameters.
|
|
1810
|
+
|
|
1811
|
+
```ts
|
|
1812
|
+
class ZodType<
|
|
1813
|
+
Output,
|
|
1814
|
+
Def extends ZodTypeDef = ZodTypeDef,
|
|
1815
|
+
Input = Output
|
|
1816
|
+
> { ... }
|
|
1817
|
+
```
|
|
1818
|
+
|
|
1819
|
+
By constraining these in your generic input, you can limit what schemas are allowable as inputs to your function:
|
|
1820
|
+
|
|
1821
|
+
```ts
|
|
1822
|
+
function makeSchemaOptional<T extends z.ZodType<string>>(schema: T) {
|
|
1823
|
+
return schema.optional();
|
|
1824
|
+
}
|
|
1825
|
+
|
|
1826
|
+
makeSchemaOptional(z.string());
|
|
1827
|
+
// works fine
|
|
1828
|
+
|
|
1829
|
+
makeSchemaOptional(z.number());
|
|
1830
|
+
// Error: 'ZodNumber' is not assignable to parameter of type 'ZodType<string, ZodTypeDef, string>'
|
|
1831
|
+
```
|
|
1832
|
+
|
|
1833
|
+
## Error handling
|
|
1720
1834
|
|
|
1721
1835
|
Zod provides a subclass of Error called `ZodError`. ZodErrors contain an `issues` array containing detailed information about the validation problems.
|
|
1722
1836
|
|
|
1723
1837
|
```ts
|
|
1724
|
-
const data = z
|
|
1725
|
-
.object({
|
|
1726
|
-
name: z.string(),
|
|
1727
|
-
})
|
|
1728
|
-
.safeParse({ name: 12 });
|
|
1838
|
+
const data = z.object({ name: z.string() }).safeParse({ name: 12 });
|
|
1729
1839
|
|
|
1730
1840
|
if (!data.success) {
|
|
1731
1841
|
data.error.issues;
|
package/index.d.ts
ADDED
package/lib/ZodError.d.ts
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
|
+
import type { TypeOf, ZodType } from ".";
|
|
1
2
|
import { ZodParsedType } from "./helpers/parseUtil";
|
|
2
3
|
import { Primitive } from "./helpers/typeAliases";
|
|
3
4
|
import { util } from "./helpers/util";
|
|
5
|
+
declare type allKeys<T> = T extends any ? keyof T : never;
|
|
6
|
+
export declare type inferFlattenedErrors<T extends ZodType<any, any, any>, U = string> = typeToFlattenedError<TypeOf<T>, U>;
|
|
7
|
+
export declare type typeToFlattenedError<T, U = string> = {
|
|
8
|
+
formErrors: U[];
|
|
9
|
+
fieldErrors: {
|
|
10
|
+
[P in allKeys<T>]?: U[];
|
|
11
|
+
};
|
|
12
|
+
};
|
|
4
13
|
export declare const ZodIssueCode: {
|
|
5
14
|
invalid_type: "invalid_type";
|
|
15
|
+
invalid_literal: "invalid_literal";
|
|
6
16
|
custom: "custom";
|
|
7
17
|
invalid_union: "invalid_union";
|
|
8
18
|
invalid_union_discriminator: "invalid_union_discriminator";
|
|
@@ -27,6 +37,10 @@ export interface ZodInvalidTypeIssue extends ZodIssueBase {
|
|
|
27
37
|
expected: ZodParsedType;
|
|
28
38
|
received: ZodParsedType;
|
|
29
39
|
}
|
|
40
|
+
export interface ZodInvalidLiteralIssue extends ZodIssueBase {
|
|
41
|
+
code: typeof ZodIssueCode.invalid_literal;
|
|
42
|
+
expected: unknown;
|
|
43
|
+
}
|
|
30
44
|
export interface ZodUnrecognizedKeysIssue extends ZodIssueBase {
|
|
31
45
|
code: typeof ZodIssueCode.unrecognized_keys;
|
|
32
46
|
keys: string[];
|
|
@@ -87,47 +101,34 @@ export interface ZodCustomIssue extends ZodIssueBase {
|
|
|
87
101
|
export declare type DenormalizedError = {
|
|
88
102
|
[k: string]: DenormalizedError | string[];
|
|
89
103
|
};
|
|
90
|
-
export declare type ZodIssueOptionalMessage = ZodInvalidTypeIssue | ZodUnrecognizedKeysIssue | ZodInvalidUnionIssue | ZodInvalidUnionDiscriminatorIssue | ZodInvalidEnumValueIssue | ZodInvalidArgumentsIssue | ZodInvalidReturnTypeIssue | ZodInvalidDateIssue | ZodInvalidStringIssue | ZodTooSmallIssue | ZodTooBigIssue | ZodInvalidIntersectionTypesIssue | ZodNotMultipleOfIssue | ZodCustomIssue;
|
|
104
|
+
export declare type ZodIssueOptionalMessage = ZodInvalidTypeIssue | ZodInvalidLiteralIssue | ZodUnrecognizedKeysIssue | ZodInvalidUnionIssue | ZodInvalidUnionDiscriminatorIssue | ZodInvalidEnumValueIssue | ZodInvalidArgumentsIssue | ZodInvalidReturnTypeIssue | ZodInvalidDateIssue | ZodInvalidStringIssue | ZodTooSmallIssue | ZodTooBigIssue | ZodInvalidIntersectionTypesIssue | ZodNotMultipleOfIssue | ZodCustomIssue;
|
|
91
105
|
export declare type ZodIssue = ZodIssueOptionalMessage & {
|
|
92
106
|
message: string;
|
|
93
107
|
};
|
|
94
108
|
export declare const quotelessJson: (obj: any) => string;
|
|
95
|
-
export declare type ZodFormattedError<T> = {
|
|
96
|
-
_errors:
|
|
109
|
+
export declare type ZodFormattedError<T, U = string> = {
|
|
110
|
+
_errors: U[];
|
|
97
111
|
} & (T extends [any, ...any[]] ? {
|
|
98
112
|
[K in keyof T]?: ZodFormattedError<T[K]>;
|
|
99
113
|
} : T extends any[] ? ZodFormattedError<T[number]>[] : T extends object ? {
|
|
100
114
|
[K in keyof T]?: ZodFormattedError<T[K]>;
|
|
101
115
|
} : unknown);
|
|
116
|
+
export declare type inferFormattedError<T extends ZodType<any, any, any>, U = string> = ZodFormattedError<TypeOf<T>, U>;
|
|
102
117
|
export declare class ZodError<T = any> extends Error {
|
|
103
118
|
issues: ZodIssue[];
|
|
104
119
|
get errors(): ZodIssue[];
|
|
105
120
|
constructor(issues: ZodIssue[]);
|
|
106
|
-
format
|
|
121
|
+
format(): ZodFormattedError<T>;
|
|
122
|
+
format<U>(mapper: (issue: ZodIssue) => U): ZodFormattedError<T, U>;
|
|
107
123
|
static create: (issues: ZodIssue[]) => ZodError<any>;
|
|
108
124
|
toString(): string;
|
|
109
125
|
get message(): string;
|
|
110
126
|
get isEmpty(): boolean;
|
|
111
127
|
addIssue: (sub: ZodIssue) => void;
|
|
112
128
|
addIssues: (subs?: ZodIssue[]) => void;
|
|
113
|
-
flatten(
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
[k: string]: string[];
|
|
117
|
-
};
|
|
118
|
-
};
|
|
119
|
-
flatten<U>(mapper?: (issue: ZodIssue) => U): {
|
|
120
|
-
formErrors: U[];
|
|
121
|
-
fieldErrors: {
|
|
122
|
-
[k: string]: U[];
|
|
123
|
-
};
|
|
124
|
-
};
|
|
125
|
-
get formErrors(): {
|
|
126
|
-
formErrors: string[];
|
|
127
|
-
fieldErrors: {
|
|
128
|
-
[k: string]: string[];
|
|
129
|
-
};
|
|
130
|
-
};
|
|
129
|
+
flatten(): typeToFlattenedError<T>;
|
|
130
|
+
flatten<U>(mapper?: (issue: ZodIssue) => U): typeToFlattenedError<T, U>;
|
|
131
|
+
get formErrors(): typeToFlattenedError<T, string>;
|
|
131
132
|
}
|
|
132
133
|
declare type stripPath<T extends object> = T extends any ? util.OmitKeys<T, "path"> : never;
|
|
133
134
|
export declare type IssueData = stripPath<ZodIssueOptionalMessage> & {
|
package/lib/ZodError.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.setErrorMap = exports.overrideErrorMap = exports.defaultErrorMap = expor
|
|
|
4
4
|
const util_1 = require("./helpers/util");
|
|
5
5
|
exports.ZodIssueCode = util_1.util.arrayToEnum([
|
|
6
6
|
"invalid_type",
|
|
7
|
+
"invalid_literal",
|
|
7
8
|
"custom",
|
|
8
9
|
"invalid_union",
|
|
9
10
|
"invalid_union_discriminator",
|
|
@@ -27,51 +28,6 @@ class ZodError extends Error {
|
|
|
27
28
|
constructor(issues) {
|
|
28
29
|
super();
|
|
29
30
|
this.issues = [];
|
|
30
|
-
this.format = () => {
|
|
31
|
-
const fieldErrors = { _errors: [] };
|
|
32
|
-
const processError = (error) => {
|
|
33
|
-
for (const issue of error.issues) {
|
|
34
|
-
if (issue.code === "invalid_union") {
|
|
35
|
-
issue.unionErrors.map(processError);
|
|
36
|
-
}
|
|
37
|
-
else if (issue.code === "invalid_return_type") {
|
|
38
|
-
processError(issue.returnTypeError);
|
|
39
|
-
}
|
|
40
|
-
else if (issue.code === "invalid_arguments") {
|
|
41
|
-
processError(issue.argumentsError);
|
|
42
|
-
}
|
|
43
|
-
else if (issue.path.length === 0) {
|
|
44
|
-
fieldErrors._errors.push(issue.message);
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
let curr = fieldErrors;
|
|
48
|
-
let i = 0;
|
|
49
|
-
while (i < issue.path.length) {
|
|
50
|
-
const el = issue.path[i];
|
|
51
|
-
const terminal = i === issue.path.length - 1;
|
|
52
|
-
if (!terminal) {
|
|
53
|
-
if (typeof el === "string") {
|
|
54
|
-
curr[el] = curr[el] || { _errors: [] };
|
|
55
|
-
}
|
|
56
|
-
else if (typeof el === "number") {
|
|
57
|
-
const errorArray = [];
|
|
58
|
-
errorArray._errors = [];
|
|
59
|
-
curr[el] = curr[el] || errorArray;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
curr[el] = curr[el] || { _errors: [] };
|
|
64
|
-
curr[el]._errors.push(issue.message);
|
|
65
|
-
}
|
|
66
|
-
curr = curr[el];
|
|
67
|
-
i++;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
processError(this);
|
|
73
|
-
return fieldErrors;
|
|
74
|
-
};
|
|
75
31
|
this.addIssue = (sub) => {
|
|
76
32
|
this.issues = [...this.issues, sub];
|
|
77
33
|
};
|
|
@@ -92,6 +48,55 @@ class ZodError extends Error {
|
|
|
92
48
|
get errors() {
|
|
93
49
|
return this.issues;
|
|
94
50
|
}
|
|
51
|
+
format(_mapper) {
|
|
52
|
+
const mapper = _mapper ||
|
|
53
|
+
function (issue) {
|
|
54
|
+
return issue.message;
|
|
55
|
+
};
|
|
56
|
+
const fieldErrors = { _errors: [] };
|
|
57
|
+
const processError = (error) => {
|
|
58
|
+
for (const issue of error.issues) {
|
|
59
|
+
if (issue.code === "invalid_union") {
|
|
60
|
+
issue.unionErrors.map(processError);
|
|
61
|
+
}
|
|
62
|
+
else if (issue.code === "invalid_return_type") {
|
|
63
|
+
processError(issue.returnTypeError);
|
|
64
|
+
}
|
|
65
|
+
else if (issue.code === "invalid_arguments") {
|
|
66
|
+
processError(issue.argumentsError);
|
|
67
|
+
}
|
|
68
|
+
else if (issue.path.length === 0) {
|
|
69
|
+
fieldErrors._errors.push(mapper(issue));
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
let curr = fieldErrors;
|
|
73
|
+
let i = 0;
|
|
74
|
+
while (i < issue.path.length) {
|
|
75
|
+
const el = issue.path[i];
|
|
76
|
+
const terminal = i === issue.path.length - 1;
|
|
77
|
+
if (!terminal) {
|
|
78
|
+
if (typeof el === "string") {
|
|
79
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
80
|
+
}
|
|
81
|
+
else if (typeof el === "number") {
|
|
82
|
+
const errorArray = [];
|
|
83
|
+
errorArray._errors = [];
|
|
84
|
+
curr[el] = curr[el] || errorArray;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
89
|
+
curr[el]._errors.push(mapper(issue));
|
|
90
|
+
}
|
|
91
|
+
curr = curr[el];
|
|
92
|
+
i++;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
processError(this);
|
|
98
|
+
return fieldErrors;
|
|
99
|
+
}
|
|
95
100
|
toString() {
|
|
96
101
|
return this.message;
|
|
97
102
|
}
|
|
@@ -135,6 +140,9 @@ const defaultErrorMap = (issue, _ctx) => {
|
|
|
135
140
|
message = `Expected ${issue.expected}, received ${issue.received}`;
|
|
136
141
|
}
|
|
137
142
|
break;
|
|
143
|
+
case exports.ZodIssueCode.invalid_literal:
|
|
144
|
+
message = `Invalid literal value, expected ${JSON.stringify(issue.expected)}`;
|
|
145
|
+
break;
|
|
138
146
|
case exports.ZodIssueCode.unrecognized_keys:
|
|
139
147
|
message = `Unrecognized key(s) in object: ${issue.keys
|
|
140
148
|
.map((k) => `'${k}'`)
|
|
@@ -42,7 +42,6 @@ export interface ParseContext {
|
|
|
42
42
|
readonly issues: ZodIssue[];
|
|
43
43
|
readonly contextualErrorMap?: ZodErrorMap;
|
|
44
44
|
readonly async: boolean;
|
|
45
|
-
readonly typeCache: Map<any, ZodParsedType> | undefined;
|
|
46
45
|
};
|
|
47
46
|
readonly path: ParsePath;
|
|
48
47
|
readonly schemaErrorMap?: ZodErrorMap;
|