Zod is a TypeScript/JavaScript library for validating data.
In simple terms:
Zod checks whether data has the shape and types you expect.
Example
Suppose your API expects a user:
{
name: "Reyhaneh",
age: 25,
email: "rey@example.com"
}You can define a Zod schema:
import { z } from "zod";
const userSchema = z.object({
name: z.string(),
age: z.number(),
email: z.string().email()
});Then validate data:
userSchema.parse(user);If the data is:
{
name: "Reyhaneh",
age: "25",
email: "wrong"
}Zod rejects it because:
-
ageshould be a number, but it’s a string -
emailisn’t a valid email
Where is Zod commonly used?
Especially in frontend + API applications:
Frontend
↓
Form data
↓
Zod validation
↓
API request
↓
Backend
↓
Zod validation
↓
DatabaseFor example, with a login form:
const loginSchema = z.object({
email: z.string().email(),
password: z.string().min(8)
});This lets you catch invalid input before sending it to the API.
Zod vs TypeScript
A very important distinction:
TypeScript checks types at development/compile time.
type User = {
name: string;
age: number;
};But data coming from an API is runtime data. TypeScript cannot guarantee that the API actually sent what you expect.
Zod can check it at runtime:
TypeScript
↓
"According to my code, this should be a User."
Zod
↓
"Let me check whether the actual data really is a User."So a common combination is:
TypeScript = type safety during development
Zod = runtime validation of actual data