ADR-0008: Automatic validation middleware via Echo context wrapping
Status
AcceptedTags
http, echo, validation, middleware, handlersDecision
Use anAutoValidate() Echo middleware that wraps the context and overrides Bind() to call Validate() automatically. Handlers call c.Bind(&req) once — parsing and validation both happen. No manual c.Validate() calls in handlers.
Why
Callingc.Bind() then c.Validate() in every handler is repetitive and easy to forget. Missing validation silently allows malformed input through to business logic.
A middleware wrapper intercepts Bind() at the framework level — validation is guaranteed for every bound request without touching individual handlers.
How it works
Exceptions — skip AutoValidate for
- File uploads (
multipart/form-data) — Bind semantics differ - Webhooks with custom body parsing
- Routes that intentionally accept partial/unvalidated input
Rules for agents
- Never add
c.Validate(&req)afterc.Bind(&req)in handlers — the middleware handles it - Register
AutoValidate()globally inmain.goafterecho.Validatoris set - For file upload handlers, skip binding through
c.Bind()and handle multipart directly - The middleware must be registered after
e.Validator = validator.New()— middleware reads the validator from the context