Class: Committee::ResponseValidator

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

Instance Method Summary collapse

Constructor Details

#initialize(data, schema, link_schema, type_schema) ⇒ ResponseValidator

Returns a new instance of ResponseValidator.



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

def initialize(data, schema, link_schema, type_schema)
  @data = data
  @schema = schema
  @link_schema = link_schema
  @type_schema = type_schema
end

Instance Method Details

#callObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/committee/response_validator.rb', line 10

def call
  data = if @link_schema["title"] == "List"
    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[0]
  else
    @data
  end

  data_keys = build_data_keys(data)
  schema_keys = build_schema_keys

  extra = data_keys - schema_keys
  missing = schema_keys - data_keys

  errors = []

  if extra.count > 0
    errors << "Extra keys in response: #{extra.join(', ')}."
  end

  if missing.count > 0
    errors << "Missing keys in response: #{missing.join(', ')}."
  end

  unless errors.empty?
    raise InvalidResponse, ["`#{@link_schema['method']} #{@link_schema['href']}` deviates from schema.", *errors].join(' ')
  end

  check_data!(@type_schema, data, [])
end

#check_data!(schema, data, path) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/committee/response_validator.rb', line 44

def check_data!(schema, data, path)
  schema["properties"].each do |key, value|
    if value["properties"]
      check_data!(value, data[key], path + [key])
    else
      definition = @schema.find(value["$ref"])
      check_type!(definition["type"], data[key], path + [key])
      unless definition["type"].include?("null") && data[key].nil?
        check_format!(definition["format"], data[key], path + [key])
        check_pattern!(definition["pattern"], data[key], path + [key])
      end
    end
  end
end

#check_format!(format, value, path) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/committee/response_validator.rb', line 59

def check_format!(format, value, path)
  return if !format
  valid = case format
  when "date-time"
    value =~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2})\:(\d{2})\:(\d{2})(Z|[+-](\d{2})\:(\d{2}))$/
  when "email"
    value =~ /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i
  when "uuid"
    value =~ /^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/
  else
    true
  end
  unless valid
    raise InvalidResponse,
      %{Invalid format at "#{path.join(":")}": expected "#{value}" to be "#{format}".}
  end
end

#check_pattern!(pattern, value, path) ⇒ Object



77
78
79
80
81
82
# File 'lib/committee/response_validator.rb', line 77

def check_pattern!(pattern, value, path)
  if pattern && !(value =~ pattern)
    raise InvalidResponse,
      %{Invalid pattern at "#{path.join(":")}": expected #{value} to match "#{pattern}".}
  end
end

#check_type!(allowed_types, value, path) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/committee/response_validator.rb', line 84

def check_type!(allowed_types, value, path)
  types = case value
  when NilClass
    ["null"]
  when TrueClass, FalseClass
    ["boolean"]
  when Bignum, Fixnum
    ["integer", "number"]
  when Float
    ["number"]
  when Hash
    ["object"]
  when String
    ["string"]
  else
    ["unknown"]
  end
  if (allowed_types & types).empty?
    raise InvalidResponse,
      %{Invalid type at "#{path.join(":")}": expected #{value} to be #{allowed_types} (was: #{types}).}
  end
end