zod 3.8.2 → 3.9.0
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/CHANGELOG.md +56 -0
- package/README.md +277 -251
- package/lib/ZodError.d.ts +10 -4
- package/lib/ZodError.d.ts.map +1 -1
- package/lib/ZodError.js +30 -29
- package/lib/ZodError.js.map +1 -1
- package/lib/benchmarks/index.js +4 -5
- package/lib/benchmarks/index.js.map +1 -1
- package/lib/helpers/errorUtil.d.ts +6 -2
- package/lib/helpers/errorUtil.d.ts.map +1 -1
- package/lib/helpers/parseUtil.d.ts +30 -14
- package/lib/helpers/parseUtil.d.ts.map +1 -1
- package/lib/helpers/parseUtil.js +91 -40
- package/lib/helpers/parseUtil.js.map +1 -1
- package/lib/index.mjs +1 -1
- package/lib/index.mjs.map +1 -1
- package/lib/types.d.ts +204 -145
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +772 -737
- package/lib/types.js.map +1 -1
- package/package.json +6 -3
- package/lib/PseudoPromise.d.ts +0 -7
- package/lib/PseudoPromise.d.ts.map +0 -1
- package/lib/PseudoPromise.js +0 -17
- package/lib/PseudoPromise.js.map +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,61 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
### 3.9
|
|
4
|
+
|
|
5
|
+
- Custom error messages in schemas
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
const name = z.string({
|
|
9
|
+
invalid_type_error: "Name must be string",
|
|
10
|
+
required_error: "Name is required",
|
|
11
|
+
});
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Under the hood, this creates a custom error map that's bound to the schema. You can also pass a custom error map explicitly.
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
const name = z.string({ errorMap: myErrorMap });
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
- Rest parameters for tuples
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
const myTuple = z.tuple([z.string(), z.number()]).rest(z.boolean());
|
|
24
|
+
type t1 = z.output<typeof myTuple>; // [string, number, ...boolean[]]
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
- Selective `.partial`
|
|
28
|
+
|
|
29
|
+
You can specify certain fields to make optional with the `ZodObject.partial` method.
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
const user = z.object({
|
|
33
|
+
name: z.string(),
|
|
34
|
+
age: z.number(),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const optionalNameUser = user.partial({ name: true });
|
|
38
|
+
// { name?: string; age: number; }
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
- Specify key schema in ZodRecord
|
|
42
|
+
|
|
43
|
+
Previously, `z.record` only accepted a single schema:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
z.record(z.boolean()); // Record<string, boolean>;
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Now `z.record` has been overloaded to support two schemas. The first validates the _keys_ of the record, and the second validates the _values_.
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
const schema = z.record(z.number(), z.boolean());
|
|
53
|
+
type schema = z.infer<typeof schema>; // Record<number, boolean>
|
|
54
|
+
|
|
55
|
+
const schema = z.record(z.enum(["Tuna", "Trout"]), z.boolean());
|
|
56
|
+
type schema = z.infer<typeof schema>; // Record<"Tuna" | "Trout", boolean>
|
|
57
|
+
```
|
|
58
|
+
|
|
3
59
|
### 3.8
|
|
4
60
|
|
|
5
61
|
- Add `z.preprocess`
|