Class: Committee::ResponseValidator

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

Instance Method Summary collapse

Constructor Details

#initialize(link) ⇒ ResponseValidator

Returns a new instance of ResponseValidator.



3
4
5
6
7
8
9
10
# File 'lib/committee/response_validator.rb', line 3

def initialize(link)
  @link = link

  # we should eventually move off of validating against parent schema too
  # ... this is a Herokuism and not in the specification
  schema = link.target_schema || link.parent
  @validator = JsonSchema::Validator.new(schema)
end

Instance Method Details

#call(headers, data) ⇒ Object



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

def call(headers, data)
  check_content_type!(headers)

  if @link.rel == "instances"
    if !data.is_a?(Array)
      raise InvalidResponse, "List endpoints must return an array of objects."
    end
    # only consider the first object during the validation from here on
    data = data[0]
  end

  if !@validator.validate(data)
    errors = JsonSchema::SchemaError.aggregate(@validator.errors).join("\n")
    raise InvalidResponse, "Invalid response.\n\n#{errors}"
  end
end