How do you approach validating incoming requests in a REST API, and where should validation happen?
Last updated
Do schema validation (types, required fields, formats, enums) at the boundary using declarative models like
Pydanticor JSON Schema, rejecting bad input early with a 4xx.Put business validation needing state (e.g. "email already exists", "balance sufficient") in the service layer, not the controller.
Never trust the client: re-validate body, path/query params, and headers server-side, and return
422/400with structured per-field errors.
- https://cheatsheetseries.owasp.org/cheatsheets/Input_Validation_Cheat_Sheet.html
- https://dev.to/mattiacarcione/where-should-request-validation-live-in-a-typescript-application-2dd2
- https://medium.com/@kiana.proudmoore/where-should-you-put-your-validations-in-a-spring-boot-microservice-b8dc1219c0b7
- https://medium.com/@kandalkaramit123/validation-in-spring-boot-what-happens-before-your-controller-even-runs-6abb71c21f15
- https://raxxo.shop/blogs/lab/request-validation-at-the-edge-a-minimal-pattern-i-reuse