A plug that validates incoming requests against OpenAPI schemas.
Reads the pre-compiled schemas embedded in conn.private.openapi.schemas. These are
resolved at compile time by Openapi.SchemaCompiler and attached to each route by the
OpenAPI router.
If the request was not matched by an OpenAPI route (no conn.private.openapi), it passes
through unchanged.
Options
:validate— list of parts to validate. Defaults to[:body, :query, :path].:on_error—fun/2called with(conn, errors)on validation failure. Must return aPlug.Conn. Defaults to a 400 JSON response with error details.
Usage
pipeline :api do
plug :accepts, ["json"]
plug Openapi.ValidatorPlug
end
# Only validate body, skip params
plug Openapi.ValidatorPlug, validate: [:body]
# Custom error handler
plug Openapi.ValidatorPlug, on_error: &MyApp.Errors.handle_validation/2Error format (default)
{
"errors": [
{"source": "body", "path": "#/name", "message": "Required property name was not present."},
{"source": "query", "param": "page", "message": "Type mismatch. Expected Integer but got String."}
]
}