What is Zod?

Zod is a TypeScript-first schema validation library that lets you define validation rules using schemas. It’s one of the libraries we can use to build schemas like the registerSchema example above. Instead of writing manual if statements like before, we describe what valid data looks like, and Zod handles the checking for us.

Installing Zod

Install Zod in your project:

npm install zod

Unlike many npm packages, Zod includes TypeScript types built in, so you don’t need a separate @types/zod package.

Note: Zod can validate form data in React (or other frameworks) applications. While we’re using it for back-end validation here, you can use the same schemas (or similar libraries like Yup) to validate user input in your front-end forms. If you combine your front-end and back-end code in a monorepo you can use the same validation schemas for the server and client code.



Repo link

Tags: