@@ -52,9 +52,9 @@ schema.parse(null); // => "null"
52
52
53
53
| Zod API | Coercion |
54
54
| --------------------------| ----------------------------|
55
- | ` z.coerce.string() ` | ` new String(value)` |
56
- | ` z.coerce.number() ` | ` new Number(value)` |
57
- | ` z.coerce.boolean() ` | ` new Boolean(value)` |
55
+ | ` z.coerce.string() ` | ` String(value) ` |
56
+ | ` z.coerce.number() ` | ` Number(value) ` |
57
+ | ` z.coerce.boolean() ` | ` Boolean(value) ` |
58
58
| ` z.coerce.bigint() ` | ` BigInt(value) ` |
59
59
| ` z.coerce.date() ` | ` new Date(value) ` |
60
60
@@ -308,7 +308,7 @@ schema.parse("mailto:noreply@zod.dev"); // ✅
308
308
schema .parse (" sup" ); // ✅
309
309
```
310
310
311
- As you can see this is quite permissive. Internally this uses the ` new URL() ` constructor to valid inputs; this behavior may differ across platforms and runtimes but it's the mostly rigorous way to validate URIs/URLs on any given JS runtime/engine.
311
+ As you can see this is quite permissive. Internally this uses the ` new URL() ` constructor to validate inputs; this behavior may differ across platforms and runtimes but it's the mostly rigorous way to validate URIs/URLs on any given JS runtime/engine.
312
312
313
313
To validate the hostname against a specific regex:
314
314
@@ -718,7 +718,7 @@ strbool.parse("1") // => true
718
718
strbool .parse (" yes" ) // => true
719
719
strbool .parse (" on" ) // => true
720
720
strbool .parse (" y" ) // => true
721
- strbool .parse (" enable " ) // => true
721
+ strbool .parse (" enabled " ) // => true
722
722
723
723
strbool .parse (" false" ); // => false
724
724
strbool .parse (" 0" ); // => false
@@ -861,7 +861,7 @@ const hello = z.templateLiteral(["hello, ", z.string()]);
861
861
// `hello, ${string}`
862
862
863
863
const cssUnits = z .enum ([" px" , " em" , " rem" , " %" ]);
864
- const css = z .templateLiteral ([z .number (), cssUnits ]);
864
+ const css = z .templateLiteral ([z .number (), cssUnits ]);
865
865
// `${number}px` | `${number}em` | `${number}rem` | `${number}%`
866
866
867
867
const email = z .templateLiteral ([
@@ -950,7 +950,7 @@ Dog.parse({ name: "Yeller", extraKey: true });
950
950
951
951
To define a * catchall schema* that will be used to validate any unrecognized keys:
952
952
953
- ```
953
+ ``` ts z.object
954
954
const DogWithStrings = z .object ({
955
955
name: z .string (),
956
956
age: z .number ().optional (),
@@ -1507,10 +1507,10 @@ type c = z.infer<typeof c>; // => number
1507
1507
This can be useful for intersecting two object types.
1508
1508
1509
1509
``` ts
1510
- const Person = z .intersection ({ name: z .string () });
1510
+ const Person = z .object ({ name: z .string () });
1511
1511
type Person = z .infer <typeof Person >;
1512
1512
1513
- const Employee = z .intersection ({ role: z .string () });
1513
+ const Employee = z .object ({ role: z .string () });
1514
1514
type Employee = z .infer <typeof Employee >;
1515
1515
1516
1516
const EmployedPerson = z .intersection (Person , Employee );
@@ -1766,8 +1766,8 @@ By default, validation issues from checks are considered *continuable*; that is,
1766
1766
<Tab value = "
10000
; Zod" >
1767
1767
``` ts
1768
1768
const myString = z .string ()
1769
- .refine ((val ) => val .length > 8 )
1770
- .refine ((val ) => val === val .toLowerCase ());
1769
+ .refine ((val ) => val .length > 8 , { error: " Too short! " } )
1770
+ .refine ((val ) => val === val .toLowerCase (), { error: " Must be lowercase " } );
1771
1771
1772
1772
1773
1773
const result = myString .safeParse (" OH NO" );
@@ -1781,8 +1781,8 @@ result.error.issues;
1781
1781
<Tab value = " Zod Mini" >
1782
1782
``` ts zod/v4-mini
1783
1783
const myString = z .string ().check (
1784
- z .refine ((val ) => val .length > 8 ),
1785
- z .refine ((val ) => val === val .toLowerCase ())
1784
+ z .refine ((val ) => val .length > 8 , { error: " Too short! " } ),
1785
+ z .refine ((val ) => val === val .toLowerCase (), { error: " Must be lowercase " } )
1786
1786
);
1787
1787
1788
1788
const result = z .safeParse (myString , " OH NO" );
@@ -1801,8 +1801,8 @@ To mark a particular refinement as *non-continuable*, use the `abort` parameter.
1801
1801
<Tab value = " Zod" >
1802
1802
``` ts
1803
1803
const myString = z .string ()
1804
- .refine ((val ) => val .length > 8 , { abort: true })
1805
- .refine ((val ) => val === val .toLowerCase ());
1804
+ .refine ((val ) => val .length > 8 , { error: " Too short! " , abort: true })
1805
+ .refine ((val ) => val === val .toLowerCase (), { error: " Must be lowercase " , abort: true } );
1806
1806
1807
1807
1808
1808
const result = myString .safeParse (" OH NO" );
@@ -1813,8 +1813,8 @@ result.error!.issues;
1813
1813
<Tab value = " Zod Mini" >
1814
1814
``` ts zod/v4-mini
1815
1815
const myString = z .string ().check (
1816
- z .refine ((val ) => val .length > 8 , { abort: true }),
1817
- z .refine ((val ) => val === val .toLowerCase (), { abort: true })
1816
+ z .refine ((val ) => val .length > 8 , { error: " Too short! " , abort: true }),
1817
+ z .refine ((val ) => val === val .toLowerCase (), { error: " Must be lowercase " , abort: true })
1818
1818
);
1819
1819
1820
1820
const result = z .safeParse (myString , " OH NO" );
@@ -2441,7 +2441,7 @@ The `z.templateLiteral` API can handle any number of string literals (e.g. `"hel
2441
2441
z .templateLiteral ([ " hi there" ]);
2442
2442
// `hi there`
2443
2443
2444
- z .templateLiteral ([ " email: " , z .string ()]);
2444
+ z .templateLiteral ([ " email: " , z .string () ]);
2445
2445
// `email: ${string}`
2446
2446
2447
2447
z .templateLiteral ([ " high" , z .literal (5 ) ]);
@@ -2524,7 +2524,7 @@ Function schemas have an `.implement()` method which accepts a function and retu
2524
2524
2525
2525
``` ts
2526
2526
const computeTrimmedLength = MyFunction .implement ((input ) => {
2527
- // TypeScript knows x is a string!
2527
+ // TypeScript knows input is a string!
2528
2528
return input .trim ().length ;
2529
2529
});
2530
2530
0 commit comments