Class: HttpClientGenerator::Plugs::ValidateRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/http_client_generator/plugs/validate_request.rb

Instance Method Summary collapse

Constructor Details

#initialize(schema_helper:, show_body_in_error: false) ⇒ ValidateRequest

Returns a new instance of ValidateRequest.



8
9
10
11
# File 'lib/http_client_generator/plugs/validate_request.rb', line 8

def initialize(schema_helper:, show_body_in_error: false)
  @schema_helper = schema_helper
  @show_body_in_error = show_body_in_error
end

Instance Method Details

#call(req) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/http_client_generator/plugs/validate_request.rb', line 13

def call(req)
  schema_name = :"#{req.verb}_#{req.name}"

  return req unless @schema_helper.respond_to?(schema_name)

  req.response_body =
    @schema_helper
    .public_send(schema_name, req.body)
    .value_or do |e|
      message =
        if @show_body_in_error
          "Unexpected #{e.inspect} in #{req.body}"
        else
          "Unexpected #{e.inspect}"
        end

      req.raise_message(message)
    end

  req
end