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 Attribute Summary collapse
- #app ⇒ Object readonly
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.
12 13 14 15 16 17 18 19 |
# File 'lib/openapi_first/middlewares/response_validation.rb', line 12 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 Attribute Details
#app ⇒ Object (readonly)
22 23 24 |
# File 'lib/openapi_first/middlewares/response_validation.rb', line 22 def app @app end |
Instance Method Details
#call(env) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/openapi_first/middlewares/response_validation.rb', line 24 def call(env) status, headers, body = @app.call(env) body = read_body(body) @definition.validate_response(Rack::Request.new(env), Rack::Response[status, headers, body], raise_error: true) env[REQUEST] ||= @definition.request(Rack::Request.new(env)) [status, headers, body] end |