Openapi.ResponseValidatorPlug (Openapi v0.3.0)

Copy Markdown View Source

A plug that validates handler responses against the responses schemas defined in the OpenAPI document.

Registers a before_send callback that fires after the handler has produced its response. If the response body does not conform to the compiled schema for that HTTP status code, the configured on_error callback is called. The default is to log a warning and leave the response unchanged — this plug never halts or alters the response on its own, because the response body is already committed by the time validation runs.

Best suited for dev and test pipelines to catch spec drift early.

Options

  • :on_errorfun/2 called with (conn, errors) on a schema mismatch. Must return a Plug.Conn. Defaults to logging a warning at the :warning level and returning the conn unchanged.

Usage

# In dev/test only
pipeline :api do
  plug :accepts, ["json"]
  plug Openapi.ValidatorPlug
  plug Openapi.ResponseValidatorPlug
end

# Custom error handler (e.g. raise in tests)
plug Openapi.ResponseValidatorPlug, on_error: fn conn, errors ->
  raise "Response for #{conn.private.openapi.operation_id} is invalid: #{inspect(errors)}"
end