json_schema 0.8.0 → 0.9.0
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.
- checksums.yaml +4 -4
- data/README.md +3 -0
- data/lib/json_schema/schema_error.rb +3 -2
- data/lib/json_schema/validator.rb +21 -21
- data/test/json_schema/validator_test.rb +25 -2
- 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: b4d9fa48c12abc2fe791ec053a7069ed2dd3dbdf
|
4
|
+
data.tar.gz: 285f81709fce34c3422adbe16929dcdb9932fe1f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 859c229811d914bf648667291c952a87d3a19af4daafcf76f9e03844c511b3c818cdeb117c34673c12031a45b5e0c1e0997a2b52090ad585f8137ba972c300f3
|
7
|
+
data.tar.gz: 1757e0c6ef2e29ac529221f758d90ed06561614ea6c6f3539f8d4d75789c303d62f7a1445efcebbc439d3f9f86381d5d35284a7314cf25c0b59d8bdaaab94976
|
data/README.md
CHANGED
@@ -19,6 +19,9 @@ require "json_schema"
|
|
19
19
|
schema_data = JSON.parse(File.read("schema.json"))
|
20
20
|
schema = JsonSchema.parse!(schema_data)
|
21
21
|
|
22
|
+
# expand $ref nodes - raise SchemaError if unable to resolve
|
23
|
+
schema.expand_references!
|
24
|
+
|
22
25
|
# validate some data - raise ValidationError if it doesn't conform
|
23
26
|
data = JSON.parse(File.read("data.json"))
|
24
27
|
schema.validate!(data)
|
@@ -18,12 +18,13 @@ module JsonSchema
|
|
18
18
|
end
|
19
19
|
|
20
20
|
class ValidationError < SchemaError
|
21
|
-
attr_accessor :path, :sub_errors
|
21
|
+
attr_accessor :data, :path, :sub_errors
|
22
22
|
|
23
|
-
def initialize(schema, path, message, type, sub_errors = nil)
|
23
|
+
def initialize(schema, path, message, type, sub_errors = nil, data: nil)
|
24
24
|
super(schema, message, type)
|
25
25
|
@path = path
|
26
26
|
@sub_errors = sub_errors
|
27
|
+
@data = data
|
27
28
|
end
|
28
29
|
|
29
30
|
def pointer
|
@@ -144,7 +144,7 @@ module JsonSchema
|
|
144
144
|
validate_data(subschema, data, errors, path)
|
145
145
|
end
|
146
146
|
message = %{Not all subschemas of "allOf" matched.}
|
147
|
-
errors << ValidationError.new(schema, path, message, :all_of_failed) if !valid
|
147
|
+
errors << ValidationError.new(schema, path, message, :all_of_failed, data: data) if !valid
|
148
148
|
valid
|
149
149
|
end
|
150
150
|
|
@@ -161,7 +161,7 @@ module JsonSchema
|
|
161
161
|
|
162
162
|
unless subschemata_validity.any? { |valid| valid == true }
|
163
163
|
message = %{No subschema in "anyOf" matched.}
|
164
|
-
errors << ValidationError.new(schema, path, message, :any_of_failed, sub_errors)
|
164
|
+
errors << ValidationError.new(schema, path, message, :any_of_failed, sub_errors, data: data)
|
165
165
|
return false
|
166
166
|
end
|
167
167
|
|
@@ -208,7 +208,7 @@ module JsonSchema
|
|
208
208
|
true
|
209
209
|
else
|
210
210
|
message = %{#{data} is not a valid #{schema.format}.}
|
211
|
-
errors << ValidationError.new(schema, path, message, :invalid_format)
|
211
|
+
errors << ValidationError.new(schema, path, message, :invalid_format, data: data)
|
212
212
|
false
|
213
213
|
end
|
214
214
|
end
|
@@ -219,7 +219,7 @@ module JsonSchema
|
|
219
219
|
true
|
220
220
|
else
|
221
221
|
message = %{#{data} is not a member of #{schema.enum}.}
|
222
|
-
errors << ValidationError.new(schema, path, message, :invalid_type)
|
222
|
+
errors << ValidationError.new(schema, path, message, :invalid_type, data: data)
|
223
223
|
false
|
224
224
|
end
|
225
225
|
end
|
@@ -248,7 +248,7 @@ module JsonSchema
|
|
248
248
|
%{ required; only #{data.size} } +
|
249
249
|
(data.size == 1 ? "was" : "were") +
|
250
250
|
%{ supplied.}
|
251
|
-
errors << ValidationError.new(schema, path, message, :min_items_failed)
|
251
|
+
errors << ValidationError.new(schema, path, message, :min_items_failed, data: data)
|
252
252
|
false
|
253
253
|
elsif data.size > schema.items.count && !schema.additional_items?
|
254
254
|
message = %{No more than #{schema.items.count} item} +
|
@@ -256,7 +256,7 @@ module JsonSchema
|
|
256
256
|
%{ allowed; #{data.size} } +
|
257
257
|
(data.size > 1 ? "were" : "was") +
|
258
258
|
%{ supplied.}
|
259
|
-
errors << ValidationError.new(schema, path, message, :max_items_failed)
|
259
|
+
errors << ValidationError.new(schema, path, message, :max_items_failed, data: data)
|
260
260
|
false
|
261
261
|
else
|
262
262
|
valid = true
|
@@ -286,7 +286,7 @@ module JsonSchema
|
|
286
286
|
message = %{#{data} must be less than} +
|
287
287
|
(schema.max_exclusive? ? "" : " or equal to") +
|
288
288
|
%{ #{schema.max}.}
|
289
|
-
errors << ValidationError.new(schema, path, message, :max_failed)
|
289
|
+
errors << ValidationError.new(schema, path, message, :max_failed, data: data)
|
290
290
|
false
|
291
291
|
end
|
292
292
|
end
|
@@ -301,7 +301,7 @@ module JsonSchema
|
|
301
301
|
%{ allowed; #{data.size} } +
|
302
302
|
(data.size == 1 ? "was" : "were")+
|
303
303
|
%{ supplied.}
|
304
|
-
errors << ValidationError.new(schema, path, message, :max_items_failed)
|
304
|
+
errors << ValidationError.new(schema, path, message, :max_items_failed, data: data)
|
305
305
|
false
|
306
306
|
end
|
307
307
|
end
|
@@ -316,7 +316,7 @@ module JsonSchema
|
|
316
316
|
%{ allowed; #{data.length} } +
|
317
317
|
(data.length == 1 ? "was" : "were") +
|
318
318
|
%{ supplied.}
|
319
|
-
errors << ValidationError.new(schema, path, message, :max_length_failed)
|
319
|
+
errors << ValidationError.new(schema, path, message, :max_length_failed, data: data)
|
320
320
|
false
|
321
321
|
end
|
322
322
|
end
|
@@ -331,7 +331,7 @@ module JsonSchema
|
|
331
331
|
%{ allowed; #{data.keys.size} } +
|
332
332
|
(data.keys.size == 1 ? "was" : "were") +
|
333
333
|
%{ supplied.}
|
334
|
-
errors << ValidationError.new(schema, path, message, :max_properties_failed)
|
334
|
+
errors << ValidationError.new(schema, path, message, :max_properties_failed, data: data)
|
335
335
|
false
|
336
336
|
end
|
337
337
|
end
|
@@ -346,7 +346,7 @@ module JsonSchema
|
|
346
346
|
message = %{#{data} must be greater than} +
|
347
347
|
(schema.min_exclusive? ? "" : " or equal to") +
|
348
348
|
%{ #{schema.min}.}
|
349
|
-
errors << ValidationError.new(schema, path, message, :min_failed)
|
349
|
+
errors << ValidationError.new(schema, path, message, :min_failed, data: data)
|
350
350
|
false
|
351
351
|
end
|
352
352
|
end
|
@@ -361,7 +361,7 @@ module JsonSchema
|
|
361
361
|
%{ required; only #{data.size} } +
|
362
362
|
(data.size == 1 ? "was" : "were") +
|
363
363
|
%{ supplied.}
|
364
|
-
errors << ValidationError.new(schema, path, message, :min_items_failed)
|
364
|
+
errors << ValidationError.new(schema, path, message, :min_items_failed, data: data)
|
365
365
|
false
|
366
366
|
end
|
367
367
|
end
|
@@ -376,7 +376,7 @@ module JsonSchema
|
|
376
376
|
%{ required; only #{data.length} } +
|
377
377
|
(data.length == 1 ? "was" : "were") +
|
378
378
|
%{ supplied.}
|
379
|
-
errors << ValidationError.new(schema, path, message, :min_length_failed)
|
379
|
+
errors << ValidationError.new(schema, path, message, :min_length_failed, data: data)
|
380
380
|
false
|
381
381
|
end
|
382
382
|
end
|
@@ -391,7 +391,7 @@ module JsonSchema
|
|
391
391
|
%{ required; #{data.keys.size} }+
|
392
392
|
(data.keys.size == 1 ? "was" : "were") +
|
393
393
|
%{ supplied.}
|
394
|
-
errors << ValidationError.new(schema, path, message, :min_properties_failed)
|
394
|
+
errors << ValidationError.new(schema, path, message, :min_properties_failed, data: data)
|
395
395
|
false
|
396
396
|
end
|
397
397
|
end
|
@@ -402,7 +402,7 @@ module JsonSchema
|
|
402
402
|
true
|
403
403
|
else
|
404
404
|
message = %{#{data} is not a multiple of #{schema.multiple_of}.}
|
405
|
-
errors << ValidationError.new(schema, path, message, :multiple_of_failed)
|
405
|
+
errors << ValidationError.new(schema, path, message, :multiple_of_failed, data: data)
|
406
406
|
false
|
407
407
|
end
|
408
408
|
end
|
@@ -425,7 +425,7 @@ module JsonSchema
|
|
425
425
|
else
|
426
426
|
%{More than one subschema in "oneOf" matched.}
|
427
427
|
end
|
428
|
-
errors << ValidationError.new(schema, path, message, :one_of_failed, sub_errors)
|
428
|
+
errors << ValidationError.new(schema, path, message, :one_of_failed, sub_errors, data: data)
|
429
429
|
|
430
430
|
false
|
431
431
|
end
|
@@ -437,7 +437,7 @@ module JsonSchema
|
|
437
437
|
valid = !validate_data(schema.not, data, [], path)
|
438
438
|
if !valid
|
439
439
|
message = %{Matched "not" subschema.}
|
440
|
-
errors << ValidationError.new(schema, path, message, :not_failed)
|
440
|
+
errors << ValidationError.new(schema, path, message, :not_failed, data: data)
|
441
441
|
end
|
442
442
|
valid
|
443
443
|
end
|
@@ -449,7 +449,7 @@ module JsonSchema
|
|
449
449
|
true
|
450
450
|
else
|
451
451
|
message = %{#{data} does not match #{schema.pattern.inspect}.}
|
452
|
-
errors << ValidationError.new(schema, path, message, :pattern_failed)
|
452
|
+
errors << ValidationError.new(schema, path, message, :pattern_failed, data: data)
|
453
453
|
false
|
454
454
|
end
|
455
455
|
end
|
@@ -508,7 +508,7 @@ module JsonSchema
|
|
508
508
|
else
|
509
509
|
key = find_parent(schema)
|
510
510
|
message = %{For '#{key}', #{data.inspect} is not #{ErrorFormatter.to_list(schema.type)}.}
|
511
|
-
errors << ValidationError.new(schema, path, message, :invalid_type)
|
511
|
+
errors << ValidationError.new(schema, path, message, :invalid_type, data: data)
|
512
512
|
false
|
513
513
|
end
|
514
514
|
end
|
@@ -519,7 +519,7 @@ module JsonSchema
|
|
519
519
|
true
|
520
520
|
else
|
521
521
|
message = %{Duplicate items are not allowed.}
|
522
|
-
errors << ValidationError.new(schema, path, message, :unique_items_failed)
|
522
|
+
errors << ValidationError.new(schema, path, message, :unique_items_failed, data: data)
|
523
523
|
false
|
524
524
|
end
|
525
525
|
end
|
@@ -542,7 +542,7 @@ module JsonSchema
|
|
542
542
|
end
|
543
543
|
key || fragment
|
544
544
|
end
|
545
|
-
|
545
|
+
|
546
546
|
EMAIL_PATTERN = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]+$/i
|
547
547
|
|
548
548
|
HOSTNAME_PATTERN = /^(?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|-){0,61}[0-9A-Za-z])?)*\.?$/
|
@@ -42,6 +42,7 @@ describe JsonSchema::Validator do
|
|
42
42
|
refute validate
|
43
43
|
assert_includes error_messages, %{For 'definitions/app', 4 is not an object.}
|
44
44
|
assert_includes error_types, :invalid_type
|
45
|
+
assert_includes error_data, 4
|
45
46
|
end
|
46
47
|
|
47
48
|
it "provides accurate error messages for multiple type errors" do
|
@@ -91,6 +92,7 @@ describe JsonSchema::Validator do
|
|
91
92
|
assert_includes error_messages,
|
92
93
|
%{1337 does not match /^[a-z][a-z\\-]*[a-z]$/.}
|
93
94
|
assert_includes error_types, :pattern_failed
|
95
|
+
assert_includes error_data, "1337"
|
94
96
|
end
|
95
97
|
|
96
98
|
it "validates items with tuple successfully" do
|
@@ -128,6 +130,7 @@ describe JsonSchema::Validator do
|
|
128
130
|
assert_includes error_messages,
|
129
131
|
%{2 items required; only 1 was supplied.}
|
130
132
|
assert_includes error_types, :min_items_failed
|
133
|
+
assert_includes error_data, ["cedar"]
|
131
134
|
end
|
132
135
|
|
133
136
|
it "validates items with tuple unsuccessfully for too many items" do
|
@@ -143,6 +146,7 @@ describe JsonSchema::Validator do
|
|
143
146
|
assert_includes error_messages,
|
144
147
|
%{No more than 2 items are allowed; 3 were supplied.}
|
145
148
|
assert_includes error_types, :max_items_failed
|
149
|
+
assert_includes error_data, ["cedar", "https", "websockets"]
|
146
150
|
end
|
147
151
|
|
148
152
|
it "validates items with tuple unsuccessfully for non-conforming items" do
|
@@ -158,6 +162,7 @@ describe JsonSchema::Validator do
|
|
158
162
|
assert_includes error_messages,
|
159
163
|
%{1337 is not a member of ["http", "https"].}
|
160
164
|
assert_includes error_types, :invalid_type
|
165
|
+
assert_includes error_data, "1337"
|
161
166
|
end
|
162
167
|
|
163
168
|
it "validates maxItems successfully" do
|
@@ -177,6 +182,7 @@ describe JsonSchema::Validator do
|
|
177
182
|
assert_includes error_messages,
|
178
183
|
%{No more than 10 items are allowed; 11 were supplied.}
|
179
184
|
assert_includes error_types, :max_items_failed
|
185
|
+
assert_includes error_data, (0...11).to_a
|
180
186
|
end
|
181
187
|
|
182
188
|
it "validates minItems successfully" do
|
@@ -195,6 +201,7 @@ describe JsonSchema::Validator do
|
|
195
201
|
refute validate
|
196
202
|
assert_includes error_messages, %{1 item required; only 0 were supplied.}
|
197
203
|
assert_includes error_types, :min_items_failed
|
204
|
+
assert_includes error_data, []
|
198
205
|
end
|
199
206
|
|
200
207
|
it "validates uniqueItems successfully" do
|
@@ -213,6 +220,7 @@ describe JsonSchema::Validator do
|
|
213
220
|
refute validate
|
214
221
|
assert_includes error_messages, %{Duplicate items are not allowed.}
|
215
222
|
assert_includes error_types, :unique_items_failed
|
223
|
+
assert_includes error_data, ["websockets", "websockets"]
|
216
224
|
end
|
217
225
|
|
218
226
|
it "validates maximum for an integer with exclusiveMaximum false" do
|
@@ -224,6 +232,7 @@ describe JsonSchema::Validator do
|
|
224
232
|
refute validate
|
225
233
|
assert_includes error_messages, %{11 must be less than or equal to 10.}
|
226
234
|
assert_includes error_types, :max_failed
|
235
|
+
assert_includes error_data, 11
|
227
236
|
end
|
228
237
|
|
229
238
|
it "validates maximum for an integer with exclusiveMaximum true" do
|
@@ -268,6 +277,7 @@ describe JsonSchema::Validator do
|
|
268
277
|
refute validate
|
269
278
|
assert_includes error_messages, %{0 must be greater than or equal to 1.}
|
270
279
|
assert_includes error_types, :min_failed
|
280
|
+
assert_includes error_data, 0
|
271
281
|
end
|
272
282
|
|
273
283
|
it "validates minimum for an integer with exclusiveMaximum true" do
|
@@ -311,6 +321,7 @@ describe JsonSchema::Validator do
|
|
311
321
|
refute validate
|
312
322
|
assert_includes error_messages, %{1 is not a multiple of 2.}
|
313
323
|
assert_includes error_types, :multiple_of_failed
|
324
|
+
assert_includes error_data, 1
|
314
325
|
end
|
315
326
|
|
316
327
|
it "validates multipleOf for a number" do
|
@@ -421,6 +432,7 @@ describe JsonSchema::Validator do
|
|
421
432
|
refute validate
|
422
433
|
assert_includes error_messages, %{No more than 0 properties are allowed; 1 was supplied.}
|
423
434
|
assert_includes error_types, :max_properties_failed
|
435
|
+
assert_includes error_data, { "name" => "cloudnasium" }
|
424
436
|
end
|
425
437
|
|
426
438
|
it "validates minProperties" do
|
@@ -431,6 +443,7 @@ describe JsonSchema::Validator do
|
|
431
443
|
refute validate
|
432
444
|
assert_includes error_messages, %{At least 10 properties are required; 1 was supplied.}
|
433
445
|
assert_includes error_types, :min_properties_failed
|
446
|
+
assert_includes error_data, { "name" => "cloudnasium" }
|
434
447
|
end
|
435
448
|
|
436
449
|
it "validates patternProperties" do
|
@@ -502,6 +515,7 @@ describe JsonSchema::Validator do
|
|
502
515
|
refute validate
|
503
516
|
assert_includes error_messages, %{At least 3 characters are required; only 2 were supplied.}
|
504
517
|
assert_includes error_types, :all_of_failed
|
518
|
+
assert_includes error_data, "ab"
|
505
519
|
end
|
506
520
|
|
507
521
|
it "validates anyOf" do
|
@@ -521,6 +535,7 @@ describe JsonSchema::Validator do
|
|
521
535
|
assert_includes sub_error_messages, [%{At least 5 characters are required; only 2 were supplied.}]
|
522
536
|
assert_includes sub_error_messages, [%{At least 3 characters are required; only 2 were supplied.}]
|
523
537
|
assert_equal sub_error_types, [[:min_length_failed], [:min_length_failed]]
|
538
|
+
assert_includes error_data, "ab"
|
524
539
|
end
|
525
540
|
|
526
541
|
it "validates oneOf" do
|
@@ -540,6 +555,7 @@ describe JsonSchema::Validator do
|
|
540
555
|
sub_error_types = one_of_error.sub_errors.map { |errors| errors.map(&:type) }
|
541
556
|
assert_equal sub_error_messages, [[], [], [%{foo does not match /^(hell|no)$/.}]]
|
542
557
|
assert_equal sub_error_types, [[], [], [:pattern_failed]]
|
558
|
+
assert_includes error_data, "foo"
|
543
559
|
end
|
544
560
|
|
545
561
|
it "validates not" do
|
@@ -550,6 +566,7 @@ describe JsonSchema::Validator do
|
|
550
566
|
refute validate
|
551
567
|
assert_includes error_messages, %{Matched "not" subschema.}
|
552
568
|
assert_includes error_types, :not_failed
|
569
|
+
assert_includes error_data, ""
|
553
570
|
end
|
554
571
|
|
555
572
|
it "validates date format successfully" do
|
@@ -600,6 +617,7 @@ describe JsonSchema::Validator do
|
|
600
617
|
refute validate
|
601
618
|
assert_includes error_messages, %{2014-05-13T08:42:40 is not a valid date-time.}
|
602
619
|
assert_includes error_types, :invalid_format
|
620
|
+
assert_includes error_data, "2014-05-13T08:42:40"
|
603
621
|
end
|
604
622
|
|
605
623
|
it "validates email format successfully" do
|
@@ -720,9 +738,9 @@ describe JsonSchema::Validator do
|
|
720
738
|
pointer("#/definitions/app/definitions/owner").merge!(
|
721
739
|
"format" => "uri"
|
722
740
|
)
|
723
|
-
data_sample["owner"] = "http://"
|
741
|
+
data_sample["owner"] = "http://example.com[]"
|
724
742
|
refute validate
|
725
|
-
assert_includes error_messages, %{http:// is not a valid uri.}
|
743
|
+
assert_includes error_messages, %{http://example.com[] is not a valid uri.}
|
726
744
|
assert_includes error_types, :invalid_format
|
727
745
|
end
|
728
746
|
|
@@ -772,6 +790,7 @@ describe JsonSchema::Validator do
|
|
772
790
|
refute validate
|
773
791
|
assert_includes error_messages, %{ab does not match /^[a-z][a-z0-9-]{3,30}$/.}
|
774
792
|
assert_includes error_types, :pattern_failed
|
793
|
+
assert_includes error_data, "ab"
|
775
794
|
end
|
776
795
|
|
777
796
|
it "builds appropriate JSON Pointers to bad data" do
|
@@ -802,6 +821,10 @@ describe JsonSchema::Validator do
|
|
802
821
|
@validator.errors.map(&:message)
|
803
822
|
end
|
804
823
|
|
824
|
+
def error_data
|
825
|
+
@validator.errors.map(&:data)
|
826
|
+
end
|
827
|
+
|
805
828
|
def error_types
|
806
829
|
@validator.errors.map(&:type)
|
807
830
|
end
|
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.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brandur
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ecma-re-validator
|