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
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ ResponseValidation
constructor
A new instance of ResponseValidation.
Constructor Details
#initialize(app, options = {}) ⇒ ResponseValidation
Returns a new instance of ResponseValidation.
11 12 13 14 15 16 17 18 |
# File 'lib/openapi_first/middlewares/response_validation.rb', line 11 def initialize(app, = {}) @app = app spec = .fetch(:spec) raise "You have to pass spec: when initializing #{self.class}" unless spec @definition = spec.is_a?(Definition) ? spec : OpenapiFirst.load(spec) end |
Instance Method Details
#call(env) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/openapi_first/middlewares/response_validation.rb', line 20 def call(env) request = find_request(env) status, headers, body = @app.call(env) body = body.to_ary if body.respond_to?(:to_ary) request.validate_response(Rack::Response[status, headers, body], raise_error: true) [status, headers, body] end |