Strict DTO and Validation Boundary
Give every write path a strict request object that rejects unknown, mistyped and out-of-range input before construction, so the handler only ever sees declared shape
Use when: validation is loose, applied after construction, or varies by content type
Avoid when: a strict request object already governs every write path and a test confirms rejection
Procedure
- Define a request object per endpoint with declared fields, types, ranges and enumerations.
- Configure it to reject unknown fields rather than discard them, so a client error is visible instead of silent.
- Run it before object construction, so nothing unvalidated is ever built.
- Apply the same object regardless of which parser produced the request, since parser choice must not change policy.
- Return one uniform rejection for all validation failures with a correlation id.
- Re-run the coercion, range, duplicate-key and content-type probes against the change.
Signals
- Validation runs after an object is constructed from the parsed body
- Unknown fields are silently discarded rather than rejected
- Different parsers yield different validation behaviour for the same bytes
- Validation failures return several distinct error shapes
Patterns
1. Target order
parse -> validate (strict, reject unknown) -> construct -> handle; the validator runs before anything is built
2. Reject-not-strip
rejection tells the caller their request was wrong; silent stripping hides a client bug and makes support harder
3. Retest set
re-run coercion, range, duplicate-key and content-type probes; each must now produce the same uniform rejection
Pitfalls
- Adding validation after construction, which still allows transient invalid state
- Stripping unknown fields quietly, which fixes the exposure while hiding the client error
- Validating only the default parser path while a second content type reaches the handler unvalidated
Done when
- Each write path has a strict request object that rejects unknown input before construction
- The four original probes are re-run with results recorded