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
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 a400 Bad Request status and detailed error information:
Input Sources
By default, input is combined from different request properties based on the HTTP method:| Method | Sources (priority order) |
|---|---|
| GET | query, params |
| POST | body, params, files |
| PUT | body, params |
| PATCH | body, params |
| DELETE | query, params |
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.