@@ -52,9 +52,9 @@ schema.parse(null); // => "null"
5252
5353 | Zod API | Coercion |
5454 | --------------------------| ----------------------------|
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) ` |
5858 | ` z.coerce.bigint() ` | ` BigInt(value) ` |
5959 | ` z.coerce.date() ` | ` new Date(value) ` |
6060
308308schema .parse (" sup" ); // ✅
309309```
310310
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.
312312
313313To validate the hostname against a specific regex:
314314
@@ -718,7 +718,7 @@ strbool.parse("1") // => true
718718strbool .parse (" yes" ) // => true
719719strbool .parse (" on" ) // => true
720720strbool .parse (" y" ) // => true
721- strbool .parse (" enable " ) // => true
721+ strbool .parse (" enabled " ) // => true
722722
723723strbool .parse (" false" ); // => false
724724strbool .parse (" 0" ); // => false
@@ -861,7 +861,7 @@ const hello = z.templateLiteral(["hello, ", z.string()]);
861861// `hello, ${string}`
862862
863863const cssUnits = z .enum ([" px" , " em" , " rem" , " %" ]);
864- const css = z .templateLiteral ([z .number (), cssUnits ]);
864+ const css = z .templateLiteral ([z .number (), cssUnits ]);
865865// `${number}px` | `${number}em` | `${number}rem` | `${number}%`
866866
867867const email = z .templateLiteral ([
@@ -950,7 +950,7 @@ Dog.parse({ name: "Yeller", extraKey: true });
950950
951951To define a * catchall schema* that will be used to validate any unrecognized keys:
952952
953- ```
953+ ``` ts z.object
954954const DogWithStrings = z .object ({
955955 name: z .string (),
956956 age: z .number ().optional (),
@@ -1507,10 +1507,10 @@ type c = z.infer<typeof c>; // => number
15071507This can be useful for intersecting two object types.
15081508
15091509``` ts
1510- const Person = z .intersection ({ name: z .string () });
1510+ const Person = z .object ({ name: z .string () });
15111511type Person = z .infer <typeof Person >;
15121512
1513- const Employee = z .intersection ({ role: z .string () });
1513+ const Employee = z .object ({ role: z .string () });
15141514type Employee = z .infer <typeof Employee >;
15151515
15161516const EmployedPerson = z .intersection (Person , Employee );
@@ -1766,8 +1766,8 @@ By default, validation issues from checks are considered *continuable*; that is,
17661766<Tab value = " Zod" >
17671767``` ts
17681768const 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 " } );
17711771
17721772
17731773const result = myString .safeParse (" OH NO" );
@@ -1781,8 +1781,8 @@ result.error.issues;
17811781<Tab value = " Zod Mini" >
17821782``` ts zod/v4-mini
17831783const 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 " } )
17861786);
17871787
17881788const result = z .safeParse (myString , " OH NO" );
@@ -1801,8 +1801,8 @@ To mark a particular refinement as *non-continuable*, use the `abort` parameter.
18011801<Tab value = " Zod" >
18021802``` ts
18031803const 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 } );
18061806
18071807
18081808const result = myString .safeParse (" OH NO" );
@@ -1813,8 +1813,8 @@ result.error!.issues;
18131813<Tab value = " Zod Mini" >
18141814``` ts zod/v4-mini
18151815const 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 })
18181818);
18191819
18201820const result = z .safeParse (myString , " OH NO" );
@@ -2441,7 +2441,7 @@ The `z.templateLiteral` API can handle any number of string literals (e.g. `"hel
24412441z .templateLiteral ([ " hi there" ]);
24422442// `hi there`
24432443
2444- z .templateLiteral ([ " email: " , z .string ()]);
2444+ z .templateLiteral ([ " email: " , z .string () ]);
24452445// `email: ${string}`
24462446
24472447z .templateLiteral ([ " high" , z .literal (5 ) ]);
@@ -2524,7 +2524,7 @@ Function schemas have an `.implement()` method which accepts a function and retu
25242524
25252525``` ts
25262526const computeTrimmedLength = MyFunction .implement ((input ) => {
2527- // TypeScript knows x is a string!
2527+ // TypeScript knows input is a string!
25282528 return input .trim ().length ;
25292529});
25302530
0 commit comments