Zod is a TypeScript-first schema declaration and validation library that provides static type inference, ensuring runtime data matches compile-time types. The current stable version is 4.3.6, with frequent patch and minor releases addressing bug fixes, performance improvements, and new features like `z.fromJSONSchema()`.
npm install zodVerified import paths — ran on the pinned version, not inferred.
This example defines a schema for a `User` object, infers its TypeScript type, and demonstrates how to parse valid and invalid data, handling validation errors.
For custom messages, use `.refine(val => condition, { message: 'Your custom message' })` or define a global custom error map with `z.setErrorMap()`.Use `z.coerce.date()` to automatically convert date strings to `Date` objects, or explicitly convert the string to a `Date` object before parsing.
Use `z.object(...).passthrough()` to allow unknown keys, or `z.object(...).strip()` to remove them without erroring.
For asynchronous validation, use `z.superRefine(async (value, ctx) => { /* ... */ })` and ensure you await any async operations within it.Change the schema to `z.coerce.date()` to enable automatic string-to-Date conversion upon parsing.
If extra keys should be allowed, modify your schema to `z.object({ /* ... */ }).passthrough()`. If they should be ignored, use `z.object({ /* ... */ }).strip()`.Verify that `import * as z from 'zod';` is correctly placed and that your schema variable is defined and accessible where `.parse()` is called.
Always call `.parse(data)` or `.safeParse(data)` on your schema instance to validate and extract the typed value.
No dependency data recorded yet.