Gathering detailed insights and metrics for zod
Gathering detailed insights and metrics for zod
Gathering detailed insights and metrics for zod
Gathering detailed insights and metrics for zod
TypeScript-first schema validation with static type inference
npm install zod
Typescript
Module System
Node Version
NPM Version
99.9
Supply Chain
99.7
Quality
95.4
Maintenance
100
Vulnerability
100
License
TypeScript (87.83%)
MDX (10.78%)
HTML (0.8%)
JavaScript (0.42%)
CSS (0.17%)
Shell (0.01%)
Total Downloads
1,490,874,688
Last Day
2,391,397
Last Week
35,916,674
Last Month
144,367,792
Last Year
989,355,572
MIT License
39,773 Stars
2,611 Commits
1,585 Forks
71 Watchers
83 Branches
462 Contributors
Updated on Aug 30, 2025
Latest Version
4.1.5
Package Id
zod@4.1.5
Unpacked Size
3.71 MB
Size
614.01 kB
File Count
626
NPM Version
10.8.2
Node Version
20.19.4
Published on
Aug 28, 2025
Cumulative downloads
Total Downloads
Last Day
6.5%
2,391,397
Compared to previous day
Last Week
2.5%
35,916,674
Compared to previous week
Last Month
4.6%
144,367,792
Compared to previous month
Last Year
168.8%
989,355,572
Compared to previous year
No dependencies detected.
TypeScript-first schema validation with static type inference
by @colinhacks
Zod is a TypeScript-first validation library. Define a schema and parse some data with it. You'll get back a strongly typed, validated result.
1import * as z from "zod"; 2 3const User = z.object({ 4 name: z.string(), 5}); 6 7// some untrusted data... 8const input = { 9 /* stuff */ 10}; 11 12// the parsed result is validated and type safe! 13const data = User.parse(input); 14 15// so you can use it with confidence :) 16console.log(data.name);
2kb
core bundle (gzipped)1npm install zod
Before you can do anything else, you need to define a schema. For the purposes of this guide, we'll use a simple object schema.
1import * as z from "zod"; 2 3const Player = z.object({ 4 username: z.string(), 5 xp: z.number(), 6});
Given any Zod schema, use .parse
to validate an input. If it's valid, Zod returns a strongly-typed deep clone of the input.
1Player.parse({ username: "billie", xp: 100 }); 2// => returns { username: "billie", xp: 100 }
Note — If your schema uses certain asynchronous APIs like async
refinements or transforms, you'll need to use the .parseAsync()
method instead.
1const schema = z.string().refine(async (val) => val.length <= 8); 2 3await schema.parseAsync("hello"); 4// => "hello"
When validation fails, the .parse()
method will throw a ZodError
instance with granular information about the validation issues.
1try { 2 Player.parse({ username: 42, xp: "100" }); 3} catch (err) { 4 if (err instanceof z.ZodError) { 5 err.issues; 6 /* [ 7 { 8 expected: 'string', 9 code: 'invalid_type', 10 path: [ 'username' ], 11 message: 'Invalid input: expected string' 12 }, 13 { 14 expected: 'number', 15 code: 'invalid_type', 16 path: [ 'xp' ], 17 message: 'Invalid input: expected number' 18 } 19 ] */ 20 } 21}
To avoid a try/catch
block, you can use the .safeParse()
method to get back a plain result object containing either the successfully parsed data or a ZodError
. The result type is a discriminated union, so you can handle both cases conveniently.
1const result = Player.safeParse({ username: 42, xp: "100" }); 2if (!result.success) { 3 result.error; // ZodError instance 4} else { 5 result.data; // { username: string; xp: number } 6}
Note — If your schema uses certain asynchronous APIs like async
refinements or transforms, you'll need to use the .safeParseAsync()
method instead.
1const schema = z.string().refine(async (val) => val.length <= 8); 2 3await schema.safeParseAsync("hello"); 4// => { success: true; data: "hello" }
Zod infers a static type from your schema definitions. You can extract this type with the z.infer<>
utility and use it however you like.
1const Player = z.object({ 2 username: z.string(), 3 xp: z.number(), 4}); 5 6// extract the inferred type 7type Player = z.infer<typeof Player>; 8 9// use it in your code 10const player: Player = { username: "billie", xp: 100 };
In some cases, the input & output types of a schema can diverge. For instance, the .transform()
API can convert the input from one type to another. In these cases, you can extract the input and output types independently:
1const mySchema = z.string().transform((val) => val.length); 2 3type MySchemaIn = z.input<typeof mySchema>; 4// => string 5 6type MySchemaOut = z.output<typeof mySchema>; // equivalent to z.infer<typeof mySchema> 7// number
No vulnerabilities found.