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 Attribute Summary collapse
- #app ⇒ Object readonly
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, spec = nil, options = {}) ⇒ RequestValidation
constructor
A new instance of RequestValidation.
Constructor Details
#initialize(app, spec = nil, options = {}) ⇒ RequestValidation
Returns a new instance of RequestValidation.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/openapi_first/middlewares/request_validation.rb', line 19 def initialize(app, spec = nil, = {}) @app = app if spec.is_a?(Hash) = spec spec = .fetch(:spec) end @raise = .fetch(:raise_error, OpenapiFirst.configuration.request_validation_raise_error) @error_response_class = error_response_option([: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 Attribute Details
#app ⇒ Object (readonly)
35 36 37 |
# File 'lib/openapi_first/middlewares/request_validation.rb', line 35 def app @app end |
Instance Method Details
#call(env) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/openapi_first/middlewares/request_validation.rb', line 37 def call(env) validated = @definition.validate_request(Rack::Request.new(env), raise_error: @raise) env[REQUEST] = validated failure = validated.error return @error_response_class.new(failure:).render if failure && @error_response_class @app.call(env) end |