Class: Committee::RequestValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/committee/request_validator.rb

Instance Method Summary collapse

Constructor Details

#initialize(link, options = {}) ⇒ RequestValidator

Returns a new instance of RequestValidator.



3
4
5
6
7
# File 'lib/committee/request_validator.rb', line 3

def initialize(link, options = {})
  @link = link
  @check_content_type = options.fetch(:check_content_type, true)
  @check_header = options.fetch(:check_header, true)
end

Instance Method Details

#call(request, params, headers) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/committee/request_validator.rb', line 9

def call(request, params, headers)
  check_content_type!(request, params) if @check_content_type
  if @link.schema
    valid, errors = @link.schema.validate(params)
    if !valid
      errors = JsonSchema::SchemaError.aggregate(errors).join("\n")
      raise InvalidRequest, "Invalid request.\n\n#{errors}"
    end
  end

  if @check_header && @link.respond_to?(:header_schema) && @link.header_schema
    valid, errors = @link.header_schema.validate(headers)
    if !valid
      errors = JsonSchema::SchemaError.aggregate(errors).join("\n")
      raise InvalidRequest, "Invalid request.\n\n#{errors}"
    end
  end
end