Class: OpenapiFirst::Middlewares::ResponseValidation
- Inherits:
-
Object
- Object
- OpenapiFirst::Middlewares::ResponseValidation
- Defined in:
- lib/openapi_first/middlewares/response_validation.rb
Overview
A Rack middleware to validate requests against an OpenAPI API description. You probably want to use OpenapiFirst::Test instead of this middleware.
Instance Attribute Summary collapse
- #app ⇒ Object readonly
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, spec = nil, options = {}) ⇒ ResponseValidation
constructor
A new instance of ResponseValidation.
Constructor Details
#initialize(app, spec = nil, options = {}) ⇒ ResponseValidation
Returns a new instance of ResponseValidation.
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/openapi_first/middlewares/response_validation.rb', line 17 def initialize(app, spec = nil, = {}) @app = app if spec.is_a?(Hash) = spec spec = [:spec] end @raise = .fetch(:raise_error, OpenapiFirst.configuration.response_validation_raise_error) spec ||= :default spec = OpenapiFirst[spec] if spec.is_a?(Symbol) @definition = spec.is_a?(Definition) ? spec : OpenapiFirst.load(spec) end |
Instance Attribute Details
#app ⇒ Object (readonly)
32 33 34 |
# File 'lib/openapi_first/middlewares/response_validation.rb', line 32 def app @app end |
Instance Method Details
#call(env) ⇒ Object
34 35 36 37 38 39 |
# File 'lib/openapi_first/middlewares/response_validation.rb', line 34 def call(env) status, headers, body = @app.call(env) @definition.validate_response(Rack::Request.new(env), Rack::Response[status, headers, body], raise_error: @raise) [status, headers, body] end |