committee 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -26,25 +26,23 @@ module Committee
26
26
  # don't try to check this unless it was actually specificed
27
27
  next unless @params.key?(key)
28
28
 
29
- match = false
30
- definitions = find_definitions(value["$ref"])
31
-
32
- # try to match data against any possible definition
33
- definitions.each do |definition|
34
- if check_type(definition["type"], @params[key], key) &&
35
- check_format(definition["format"], @params[key], key) &&
36
- check_pattern(definition["pattern"], @params[key], key)
37
- match = true
38
- next
29
+ if value["type"] != ["array"]
30
+ definitions = find_definitions(value["$ref"])
31
+ try_match(key, @params[key], definitions)
32
+ else
33
+ # only assume one possible array definition for now
34
+ array_definition = find_definitions(value["items"]["$ref"])[0]
35
+ @params[key].each do |item|
36
+ array_definition["properties"].each do |array_key, array_value|
37
+ return unless item.key?(array_key)
38
+
39
+ # @todo: this should really be recursive; only one array level is
40
+ # supported for now
41
+ definitions = find_definitions(array_value["$ref"])
42
+ try_match(array_key, item[array_key], definitions)
43
+ end
39
44
  end
40
45
  end
41
-
42
- # if nothing was matched, throw error according to first definition
43
- if !match && definition = definitions.first
44
- check_type!(definition["type"], @params[key], key)
45
- check_format!(definition["format"], @params[key], key)
46
- check_pattern!(definition["pattern"], @params[key], key)
47
- end
48
46
  end
49
47
  end
50
48
 
@@ -89,6 +87,8 @@ module Committee
89
87
  ["integer", "number"]
90
88
  when Float
91
89
  ["number"]
90
+ when Hash
91
+ ["object"]
92
92
  when String
93
93
  ["string"]
94
94
  else
@@ -130,5 +130,26 @@ module Committee
130
130
  def required_keys
131
131
  (@link_schema["schema"] && @link_schema["schema"]["required"]) || []
132
132
  end
133
+
134
+ def try_match(key, value, definitions)
135
+ match = false
136
+
137
+ # try to match data against any possible definition
138
+ definitions.each do |definition|
139
+ if check_type(definition["type"], value, key) &&
140
+ check_format(definition["format"], value, key) &&
141
+ check_pattern(definition["pattern"], value, key)
142
+ match = true
143
+ next
144
+ end
145
+ end
146
+
147
+ # if nothing was matched, throw error according to first definition
148
+ if !match && definition = definitions.first
149
+ check_type!(definition["type"], value, key)
150
+ check_format!(definition["format"], value, key)
151
+ check_pattern!(definition["pattern"], value, key)
152
+ end
153
+ end
133
154
  end
134
155
  end
@@ -91,6 +91,8 @@ module Committee
91
91
  ["integer", "number"]
92
92
  when Float
93
93
  ["number"]
94
+ when Hash
95
+ ["object"]
94
96
  when String
95
97
  ["string"]
96
98
  else
@@ -71,4 +71,29 @@ describe Committee::ParamValidator do
71
71
  message = %{Invalid pattern for key "name": expected %@! to match "(?-mix:^[a-z][a-z0-9-]{3,30}$)".}
72
72
  assert_equal message, e.message
73
73
  end
74
+
75
+ it "passes an array parameter" do
76
+ params = {
77
+ "updates" => [
78
+ { "name" => "bamboo", "state" => "private" },
79
+ { "name" => "cedar", "state" => "public" },
80
+ ]
81
+ }
82
+ link_schema = @schema["stack"]["links"][2]
83
+ Committee::ParamValidator.new(params, @schema, link_schema).call
84
+ end
85
+
86
+ it "detects an array item with a parameter of the wrong type" do
87
+ params = {
88
+ "updates" => [
89
+ { "name" => "bamboo", "state" => 123 },
90
+ ]
91
+ }
92
+ link_schema = @schema["stack"]["links"][2]
93
+ e = assert_raises(Committee::InvalidParams) do
94
+ Committee::ParamValidator.new(params, @schema, link_schema).call
95
+ end
96
+ message = %{Invalid type for key "state": expected 123 to be ["string"].}
97
+ assert_equal message, e.message
98
+ end
74
99
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: committee
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-02-05 00:00:00.000000000 Z
13
+ date: 2014-02-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: multi_json