Recommendation
- Can Zod definitions be integrated without breaking Swagger/OpenAPI generation?
- YES, we can use Zod alongside JSON Schema for validation, but Swagger/OpenAPI must still rely on JSON Schema.
- Does Zod improve type safety across request/response objects?
- YES
- Is performance acceptable under expected load?
- YES, we tested a file with size of 10mb. We have not noticed any significant delay in the system.
- Zod is suitable for small payloads
- Zod is ideal for complex validations
- Zod supports custom error messages
- JSON Schema is performant
- JSON Schema is already integrated with request/response validation and OpenAPI generation
- JSON Schema is best for OpenAPI documentation or simple validation
Recommendation
-
Keep JSON Schema for OpenAPI/Swagger documentation.
-
Use Zod for request payload validation in cases where JSON Schema is limited (example is file size validation or complex validation, custom user-friendly error message)
-
Simplify payload handling using Zod for requests and responses while maintaining JSON Schema for API documentation
-
Adopt both simultaneously where appropriate. JSON Schema ensures documentation consistency, and Zod provides runtime validation with friendly errors