json_schema 0.12.5 → 0.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/json_schema/parser.rb +16 -0
- data/lib/json_schema/validator.rb +6 -0
- data/test/json_schema/parser_test.rb +18 -0
- data/test/json_schema/validator_test.rb +84 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 509ee330f57ccc1fc0ad812a0be82cceeeab7461
|
4
|
+
data.tar.gz: 7c284cc6d026a7191df85776b0c6913ea6daf23a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 419c0c1045a265e0cea0a32483d14cd15f7d8b7deceba263a4647b7f33450056b599cf87fb886fc24d696dda306c7c873106d92156e45175cff079ddef85bf64
|
7
|
+
data.tar.gz: 89df42d2dfdfcd8849dd74470634485b19d4554061bb9a4c119d92bc854d0f887ab6e04c6697f0684236eaabe5bceac97aed7d55cc05997ad95f4403ebb8fb96
|
data/lib/json_schema/parser.rb
CHANGED
@@ -70,6 +70,21 @@ module JsonSchema
|
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
|
+
def parse_additional_items(schema)
|
74
|
+
if schema.additional_items
|
75
|
+
# an object indicates a schema that will be used to parse any
|
76
|
+
# items not listed in `items`
|
77
|
+
if schema.additional_items.is_a?(Hash)
|
78
|
+
schema.additional_items = parse_data(
|
79
|
+
schema.additional_items,
|
80
|
+
schema,
|
81
|
+
"additionalItems"
|
82
|
+
)
|
83
|
+
end
|
84
|
+
# otherwise, leave as boolean
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
73
88
|
def parse_additional_properties(schema)
|
74
89
|
if schema.additional_properties
|
75
90
|
# an object indicates a schema that will be used to parse any
|
@@ -308,6 +323,7 @@ module JsonSchema
|
|
308
323
|
schema.path_start = validate_type(schema, [String], "pathStart")
|
309
324
|
schema.read_only = validate_type(schema, BOOLEAN, "readOnly")
|
310
325
|
|
326
|
+
parse_additional_items(schema)
|
311
327
|
parse_additional_properties(schema)
|
312
328
|
parse_all_of(schema)
|
313
329
|
parse_any_of(schema)
|
@@ -249,6 +249,12 @@ module JsonSchema
|
|
249
249
|
false
|
250
250
|
else
|
251
251
|
valid = true
|
252
|
+
if data.size > schema.items.count && schema.additional_items.is_a?(Schema)
|
253
|
+
(schema.items.count..data.count - 1).each do |i|
|
254
|
+
valid = strict_and valid,
|
255
|
+
validate_data(schema.additional_items, data[i], errors, path + [i])
|
256
|
+
end
|
257
|
+
end
|
252
258
|
schema.items.each_with_index do |subschema, i|
|
253
259
|
valid = strict_and valid,
|
254
260
|
validate_data(subschema, data[i], errors, path + [i])
|
@@ -70,6 +70,24 @@ describe JsonSchema::Parser do
|
|
70
70
|
assert_equal ["http", "https"], schema.items[1].enum
|
71
71
|
end
|
72
72
|
|
73
|
+
it "parses array additionalItems object validation as boolean" do
|
74
|
+
pointer("#/definitions/app/definitions/flags").merge!(
|
75
|
+
"additionalItems" => false
|
76
|
+
)
|
77
|
+
schema = parse.definitions["app"].definitions["flags"]
|
78
|
+
assert_equal false, schema.additional_items
|
79
|
+
end
|
80
|
+
|
81
|
+
it "parses array additionalItems object validation as schema" do
|
82
|
+
pointer("#/definitions/app/definitions/flags").merge!(
|
83
|
+
"additionalItems" => {
|
84
|
+
"type" => "boolean"
|
85
|
+
}
|
86
|
+
)
|
87
|
+
schema = parse.definitions["app"].definitions["flags"].additional_items
|
88
|
+
assert_equal ["boolean"], schema.type
|
89
|
+
end
|
90
|
+
|
73
91
|
it "parses integer validations" do
|
74
92
|
schema = parse.definitions["app"].definitions["id"]
|
75
93
|
assert_equal 0, schema.min
|
@@ -110,7 +110,7 @@ describe JsonSchema::Validator do
|
|
110
110
|
assert validate
|
111
111
|
end
|
112
112
|
|
113
|
-
it "validates items with tuple
|
113
|
+
it "validates items with tuple with additionalItems boolean successfully" do
|
114
114
|
pointer("#/definitions/app/definitions/flags").merge!(
|
115
115
|
"additionalItems" => true,
|
116
116
|
"items" => [
|
@@ -122,6 +122,89 @@ describe JsonSchema::Validator do
|
|
122
122
|
assert validate
|
123
123
|
end
|
124
124
|
|
125
|
+
it "validates items with tuple with additionalItems boolean unsuccessfully" do
|
126
|
+
pointer("#/definitions/app/definitions/flags").merge!(
|
127
|
+
"additionalItems" => false,
|
128
|
+
"items" => [
|
129
|
+
{ "enum" => ["bamboo", "cedar"] },
|
130
|
+
{ "enum" => ["http", "https"] }
|
131
|
+
]
|
132
|
+
)
|
133
|
+
data_sample["flags"] = ["cedar", "https", "websockets"]
|
134
|
+
refute validate
|
135
|
+
assert_includes error_messages, %{No more than 2 items are allowed; 3 were supplied.}
|
136
|
+
assert_includes error_types, :max_items_failed
|
137
|
+
assert_includes error_data, ["cedar", "https", "websockets"]
|
138
|
+
end
|
139
|
+
|
140
|
+
it "validates items with tuple with additionalItems schema successfully" do
|
141
|
+
pointer("#/definitions/app/definitions/flags").merge!(
|
142
|
+
"additionalItems" => { "enum" => [ "foo", "websockets" ] },
|
143
|
+
"items" => [
|
144
|
+
{ "enum" => ["bamboo", "cedar"] },
|
145
|
+
{ "enum" => ["http", "https"] }
|
146
|
+
]
|
147
|
+
)
|
148
|
+
data_sample["flags"] = ["cedar", "https", "websockets"]
|
149
|
+
assert validate
|
150
|
+
end
|
151
|
+
|
152
|
+
it "validates items with tuple with additionalItems schema unsuccessfully for non-conforming additional item" do
|
153
|
+
pointer("#/definitions/app/definitions/flags").merge!(
|
154
|
+
"additionalItems" => { "enum" => [ "foo", "bar" ] },
|
155
|
+
"items" => [
|
156
|
+
{ "enum" => ["bamboo", "cedar"] },
|
157
|
+
{ "enum" => ["http", "https"] }
|
158
|
+
]
|
159
|
+
)
|
160
|
+
data_sample["flags"] = ["cedar", "https", "websockets"]
|
161
|
+
refute validate
|
162
|
+
assert_includes error_messages,
|
163
|
+
%{websockets is not a member of ["foo", "bar"].}
|
164
|
+
assert_includes error_types, :invalid_type
|
165
|
+
assert_includes error_data, "websockets"
|
166
|
+
end
|
167
|
+
|
168
|
+
it "validates items with tuple with additionalItems schema unsuccessfully with multiple failures" do
|
169
|
+
pointer("#/definitions/app/definitions/flags").merge!(
|
170
|
+
"additionalItems" => { "enum" => [ "foo", "bar" ] },
|
171
|
+
"items" => [
|
172
|
+
{ "enum" => ["bamboo", "cedar"] },
|
173
|
+
{ "enum" => ["http", "https"] }
|
174
|
+
]
|
175
|
+
)
|
176
|
+
data_sample["flags"] = ["cedar", "https", "websockets", "1337"]
|
177
|
+
refute validate
|
178
|
+
assert_includes error_messages,
|
179
|
+
%{websockets is not a member of ["foo", "bar"].}
|
180
|
+
assert_includes error_types, :invalid_type
|
181
|
+
assert_includes error_data, "websockets"
|
182
|
+
assert_includes error_messages,
|
183
|
+
%{1337 is not a member of ["foo", "bar"].}
|
184
|
+
assert_includes error_types, :invalid_type
|
185
|
+
assert_includes error_data, "1337"
|
186
|
+
end
|
187
|
+
|
188
|
+
it "validates items with tuple with additionalItems schema unsuccessfully with non-conforming items and additional items" do
|
189
|
+
pointer("#/definitions/app/definitions/flags").merge!(
|
190
|
+
"additionalItems" => { "enum" => [ "foo", "bar" ] },
|
191
|
+
"items" => [
|
192
|
+
{ "enum" => ["bamboo", "cedar"] },
|
193
|
+
{ "enum" => ["http", "https"] }
|
194
|
+
]
|
195
|
+
)
|
196
|
+
data_sample["flags"] = ["cedar", "1337", "websockets"]
|
197
|
+
refute validate
|
198
|
+
assert_includes error_messages,
|
199
|
+
%{websockets is not a member of ["foo", "bar"].}
|
200
|
+
assert_includes error_types, :invalid_type
|
201
|
+
assert_includes error_data, "websockets"
|
202
|
+
assert_includes error_messages,
|
203
|
+
%{1337 is not a member of ["http", "https"].}
|
204
|
+
assert_includes error_types, :invalid_type
|
205
|
+
assert_includes error_data, "1337"
|
206
|
+
end
|
207
|
+
|
125
208
|
it "validates items with tuple unsuccessfully for not enough items" do
|
126
209
|
pointer("#/definitions/app/definitions/flags").merge!(
|
127
210
|
"items" => [
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.13.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandur
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ecma-re-validator
|