Open navigation menu

Zod Server Validation

Server-side schema validation with Zod for API request bodies, query params, and environment variables.

Version: v3.23

Official Documentation ↗

Installation

npmnpm install zod
pnpmpnpm add zod

Methods

FAQ

Why validate on the server when the client already validates?
Client validation can be bypassed (curl, Postman, direct API calls). Server-side validation is the source of truth for data integrity. Using the same Zod schema on both client and server ensures consistency and eliminates drift.
How do I format ZodError for API responses?
Map result.error.issues to { field: path.join('.'), message } objects. Or use zod-to-openapi to generate OpenAPI error schemas. Always return 422 or 400 with structured error details.
Can I reuse schemas between client and server?
Yes. Define schemas in a shared package (monorepo) or shared directory. Import the same schema in React Hook Form (client) and Express middleware (server). This guarantees end-to-end type safety.