Class: OpenapiFirst::RequestValidation

Inherits:
Object
  • Object
show all
Includes:
RouterRequired
Defined in:
lib/openapi_first/request_validation.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Constructor Details

#initialize(app, raise_error: false) ⇒ RequestValidation

Returns a new instance of RequestValidation.



14
15
16
17
# File 'lib/openapi_first/request_validation.rb', line 14

def initialize(app, raise_error: false)
  @app = app
  @raise = raise_error
end

Instance Method Details

#call(env) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/openapi_first/request_validation.rb', line 19

def call(env) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  operation = env[OpenapiFirst::OPERATION]
  return @app.call(env) unless operation

  env[INBOX] = Inbox.new(env)
  catch(:halt) do
    validate_query_parameters!(env, operation, env[PARAMETERS])
    req = Rack::Request.new(env)
    content_type = req.content_type
    return @app.call(env) unless operation.request_body

    validate_request_content_type!(content_type, operation)
    body = req.body.read
    req.body.rewind
    parse_and_validate_request_body!(env, content_type, body, operation)
    @app.call(env)
  end
end