Class: OpenapiFirst::Middlewares::RequestValidation
- Inherits:
-
Object
- Object
- OpenapiFirst::Middlewares::RequestValidation
- Defined in:
- lib/openapi_first/middlewares/request_validation.rb
Overview
A Rack middleware to validate requests against an OpenAPI API description
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ RequestValidation
constructor
A new instance of RequestValidation.
Constructor Details
#initialize(app, options = {}) ⇒ RequestValidation
Returns a new instance of RequestValidation.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/openapi_first/middlewares/request_validation.rb', line 14 def initialize(app, = {}) @app = app @raise = .fetch(:raise_error, OpenapiFirst.configuration.request_validation_raise_error) @error_response_class = error_response([:error_response]) 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
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/openapi_first/middlewares/request_validation.rb', line 25 def call(env) request = find_request(env) return @app.call(env) unless request failure = request.validate failure.raise! if failure && @raise return @error_response_class.new(failure:).render if failure @app.call(env) end |