Skip to main content
Express Zod API uses Zod schemas to validate both incoming request data and outgoing response data. This ensures type safety throughout your API and prevents common bugs caused by invalid data.

How Validation Works

The framework validates:
  • Input data: Combined from request properties (query, body, params, files, headers)
  • Output data: The object returned by your endpoint handler
All validation happens automatically before your handler executes (for input) and before the response is sent (for output).

Defining Input Schemas

Input schemas describe the shape of data your endpoint expects to receive:

Defining Output Schemas

Output schemas ensure your endpoint returns consistent, validated responses:

Validation Errors

When validation fails, the framework automatically responds with a 400 Bad Request status and detailed error information:

Input Sources

By default, input is combined from different request properties based on the HTTP method: You can customize this in your configuration:

Type Safety

One of the key benefits of schema validation is automatic TypeScript type inference:

Common Validation Patterns

Optional Fields

Default Values

Arrays

Nested Objects

Best Practices

Keep Schemas Focused

Define only the fields your endpoint needs. Don’t include fields that won’t be used.

Add Descriptions

Use .describe() to document your schemas - these descriptions appear in generated API documentation.

Reuse Common Schemas

Extract common validation patterns into reusable schema constants.

Use Refinements for Complex Validation

For advanced validation logic, use refinements.

Next Steps

Refinements

Add custom validation logic

Transformations

Transform input data during validation