json-schema 0.1.4 → 0.1.5
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.
- data/lib/json-schema/validator.rb +24 -29
- data/test/test_files.rb +44 -0
- data/test/test_jsonschema.rb +162 -3
- metadata +6 -4
@@ -73,9 +73,9 @@ module JSON
|
|
73
73
|
|
74
74
|
# Validate the current schema
|
75
75
|
def validate_schema(current_schema, data, fragments)
|
76
|
-
|
76
|
+
|
77
77
|
ValidationMethods.each do |method|
|
78
|
-
if current_schema.schema[method]
|
78
|
+
if !current_schema.schema[method].nil?
|
79
79
|
self.send(("validate_" + method.sub('$','')).to_sym, current_schema, data, fragments)
|
80
80
|
end
|
81
81
|
end
|
@@ -346,10 +346,10 @@ module JSON
|
|
346
346
|
message = "The property '#{build_fragment(fragments)}' contains additional properties outside of the schema when none are allowed"
|
347
347
|
raise ValidationError.new(message, fragments, current_schema)
|
348
348
|
elsif current_schema.schema['additionalProperties'].is_a?(Hash)
|
349
|
-
|
349
|
+
extra_properties.each do |key|
|
350
350
|
schema = JSON::Schema.new(current_schema.schema['additionalProperties'],current_schema.uri)
|
351
351
|
fragments << key
|
352
|
-
validate_schema(schema,
|
352
|
+
validate_schema(schema, data[key], fragments)
|
353
353
|
fragments.pop
|
354
354
|
end
|
355
355
|
end
|
@@ -385,7 +385,7 @@ module JSON
|
|
385
385
|
if current_schema.schema['additionalItems'] == false && current_schema.schema['items'].length != data.length
|
386
386
|
message = "The property '#{build_fragment(fragments)}' contains additional array elements outside of the schema when none are allowed"
|
387
387
|
raise ValidationError.new(message, fragments, current_schema)
|
388
|
-
elsif current_schema.schema['
|
388
|
+
elsif current_schema.schema['additionalItems'].is_a?(Hash)
|
389
389
|
schema = JSON::Schema.new(current_schema.schema['additionalItems'],current_schema.uri)
|
390
390
|
data.each_with_index do |item,i|
|
391
391
|
if i >= current_schema.schema['items'].length
|
@@ -500,6 +500,9 @@ module JSON
|
|
500
500
|
schema = JSON::Schema.new(JSON.parse(open(uri.to_s).read), uri)
|
501
501
|
@schemas[uri.to_s] = schema
|
502
502
|
build_schemas(schema)
|
503
|
+
rescue JSON::ParserError
|
504
|
+
# Don't rescue this error, we want JSON formatting issues to bubble up
|
505
|
+
raise $!
|
503
506
|
rescue
|
504
507
|
# Failures will occur when this URI cannot be referenced yet. Don't worry about it,
|
505
508
|
# the proper error will fall out if the ref isn't ever defined
|
@@ -657,21 +660,17 @@ module JSON
|
|
657
660
|
begin
|
658
661
|
schema = JSON.parse(schema)
|
659
662
|
rescue
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
schema_uri = URI.parse("file://#{Dir.pwd}/#{schema}")
|
669
|
-
end
|
663
|
+
# Build a uri for it
|
664
|
+
schema_uri = URI.parse(schema)
|
665
|
+
if schema_uri.relative?
|
666
|
+
# Check for absolute path
|
667
|
+
if schema[0,1] == '/'
|
668
|
+
schema_uri = URI.parse("file://#{schema}")
|
669
|
+
else
|
670
|
+
schema_uri = URI.parse("file://#{Dir.pwd}/#{schema}")
|
670
671
|
end
|
671
|
-
schema = JSON.parse(open(schema_uri.to_s).read)
|
672
|
-
rescue
|
673
|
-
raise "Invalid schema: #{schema_uri.to_s}"
|
674
672
|
end
|
673
|
+
schema = JSON.parse(open(schema_uri.to_s).read)
|
675
674
|
end
|
676
675
|
end
|
677
676
|
|
@@ -685,19 +684,15 @@ module JSON
|
|
685
684
|
begin
|
686
685
|
data = JSON.parse(data)
|
687
686
|
rescue
|
688
|
-
|
689
|
-
|
690
|
-
if
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
schema_uri = URI.parse("file://#{Dir.pwd}/#{data}")
|
695
|
-
end
|
687
|
+
json_uri = URI.parse(data)
|
688
|
+
if json_uri.relative?
|
689
|
+
if data[0,1] == '/'
|
690
|
+
schema_uri = URI.parse("file://#{data}")
|
691
|
+
else
|
692
|
+
schema_uri = URI.parse("file://#{Dir.pwd}/#{data}")
|
696
693
|
end
|
697
|
-
data = JSON.parse(open(json_uri.to_s).read)
|
698
|
-
rescue
|
699
|
-
raise "Invalid JSON: #{json_uri.to_s}"
|
700
694
|
end
|
695
|
+
data = JSON.parse(open(json_uri.to_s).read)
|
701
696
|
end
|
702
697
|
end
|
703
698
|
data
|
data/test/test_files.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/json-schema'
|
3
|
+
|
4
|
+
class JSONSchemaTest < Test::Unit::TestCase
|
5
|
+
def test_schema_from_file
|
6
|
+
data = {"a" => 5}
|
7
|
+
assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
|
8
|
+
data = {"a" => "bad"}
|
9
|
+
assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_data_from_file
|
13
|
+
schema = {"type" => "object", "properties" => {"a" => {"type" => "integer"}}}
|
14
|
+
assert(JSON::Validator.validate(schema,File.join(File.dirname(__FILE__),"data/good_data_1.json")))
|
15
|
+
assert(!JSON::Validator.validate(schema,File.join(File.dirname(__FILE__),"data/bad_data_1.json")))
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_both_from_file
|
19
|
+
assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),File.join(File.dirname(__FILE__),"data/good_data_1.json")))
|
20
|
+
assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),File.join(File.dirname(__FILE__),"data/bad_data_1.json")))
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_invalid_schema
|
24
|
+
data = {}
|
25
|
+
assert_raise JSON::ParserError do
|
26
|
+
assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/invalid_schema_1.json"),data))
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_invalid_data
|
31
|
+
schema = {}
|
32
|
+
assert_raise JSON::ParserError do
|
33
|
+
assert(JSON::Validator.validate(schema,File.join(File.dirname(__FILE__),"data/invalid_data_1.json")))
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_file_ref
|
38
|
+
data = {"b" => {"a" => 5}}
|
39
|
+
assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_2.json"),data))
|
40
|
+
|
41
|
+
data = {"b" => {"a" => "boo"}}
|
42
|
+
assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
|
43
|
+
end
|
44
|
+
end
|
data/test/test_jsonschema.rb
CHANGED
@@ -146,7 +146,22 @@ class JSONSchemaTest < Test::Unit::TestCase
|
|
146
146
|
|
147
147
|
data["a"] = false
|
148
148
|
assert(!JSON::Validator.validate(schema,data))
|
149
|
-
|
149
|
+
|
150
|
+
# Test a union type with schemas
|
151
|
+
schema["properties"]["a"]["type"] = [{ "type" => "string" }, {"type" => "object", "properties" => {"b" => {"type" => "integer"}}}]
|
152
|
+
|
153
|
+
data["a"] = "test"
|
154
|
+
assert(JSON::Validator.validate(schema,data))
|
155
|
+
|
156
|
+
data["a"] = 5
|
157
|
+
assert(!JSON::Validator.validate(schema,data))
|
158
|
+
|
159
|
+
data["a"] = {"b" => 5}
|
160
|
+
assert(JSON::Validator.validate(schema,data))
|
161
|
+
|
162
|
+
data["a"] = {"b" => "taco"}
|
163
|
+
assert(!JSON::Validator.validate(schema,data))
|
164
|
+
end
|
150
165
|
|
151
166
|
|
152
167
|
|
@@ -157,12 +172,21 @@ class JSONSchemaTest < Test::Unit::TestCase
|
|
157
172
|
"a" => {"required" => true}
|
158
173
|
}
|
159
174
|
}
|
160
|
-
data = {
|
161
|
-
}
|
175
|
+
data = {}
|
162
176
|
|
163
177
|
assert(!JSON::Validator.validate(schema,data))
|
164
178
|
data['a'] = "Hello"
|
165
179
|
assert(JSON::Validator.validate(schema,data))
|
180
|
+
|
181
|
+
schema = {
|
182
|
+
"properties" => {
|
183
|
+
"a" => {"type" => "integer"}
|
184
|
+
}
|
185
|
+
}
|
186
|
+
|
187
|
+
data = {}
|
188
|
+
assert(JSON::Validator.validate(schema,data))
|
189
|
+
|
166
190
|
end
|
167
191
|
|
168
192
|
|
@@ -565,5 +589,140 @@ class JSONSchemaTest < Test::Unit::TestCase
|
|
565
589
|
end
|
566
590
|
|
567
591
|
|
592
|
+
|
593
|
+
def test_extends
|
594
|
+
schema = {
|
595
|
+
"properties" => {
|
596
|
+
"a" => { "type" => "integer"}
|
597
|
+
}
|
598
|
+
}
|
599
|
+
|
600
|
+
schema2 = {
|
601
|
+
"properties" => {
|
602
|
+
"a" => { "maximum" => 5 }
|
603
|
+
}
|
604
|
+
}
|
605
|
+
|
606
|
+
data = {
|
607
|
+
"a" => 10
|
608
|
+
}
|
609
|
+
|
610
|
+
assert(JSON::Validator.validate(schema,data))
|
611
|
+
assert(!JSON::Validator.validate(schema2,data))
|
612
|
+
|
613
|
+
schema["extends"] = schema2
|
614
|
+
|
615
|
+
assert(!JSON::Validator.validate(schema,data))
|
616
|
+
end
|
617
|
+
|
618
|
+
def test_pattern_properties
|
619
|
+
# Set up the default datatype
|
620
|
+
schema = {
|
621
|
+
"patternProperties" => {
|
622
|
+
"\\d+ taco" => {"type" => "integer"}
|
623
|
+
}
|
624
|
+
}
|
625
|
+
|
626
|
+
data = {
|
627
|
+
"a" => true,
|
628
|
+
"1 taco" => 1,
|
629
|
+
"20 tacos" => 20
|
630
|
+
}
|
631
|
+
|
632
|
+
assert(JSON::Validator.validate(schema,data))
|
633
|
+
data["20 tacos"] = "string!"
|
634
|
+
assert(!JSON::Validator.validate(schema,data))
|
635
|
+
end
|
636
|
+
|
637
|
+
|
638
|
+
def test_additional_properties
|
639
|
+
# Test no additional properties allowed
|
640
|
+
schema = {
|
641
|
+
"properties" => {
|
642
|
+
"a" => { "type" => "integer" }
|
643
|
+
},
|
644
|
+
"additionalProperties" => false
|
645
|
+
}
|
646
|
+
|
647
|
+
data = {
|
648
|
+
"a" => 10
|
649
|
+
}
|
650
|
+
|
651
|
+
assert(JSON::Validator.validate(schema,data))
|
652
|
+
data["b"] = 5
|
653
|
+
assert(!JSON::Validator.validate(schema,data))
|
654
|
+
|
655
|
+
# Test additional properties match a schema
|
656
|
+
schema["additionalProperties"] = { "type" => "string" }
|
657
|
+
data["b"] = "taco"
|
658
|
+
assert(JSON::Validator.validate(schema,data))
|
659
|
+
data["b"] = 5
|
660
|
+
assert(!JSON::Validator.validate(schema,data))
|
661
|
+
|
662
|
+
# Make sure this works with pattern properties set, too
|
663
|
+
schema = {
|
664
|
+
"patternProperties" => {
|
665
|
+
"\\d+ taco" => {"type" => "integer"}
|
666
|
+
},
|
667
|
+
"additionalProperties" => false
|
668
|
+
}
|
669
|
+
|
670
|
+
data = {
|
671
|
+
"5 tacos" => 5,
|
672
|
+
"20 tacos" => 20
|
673
|
+
}
|
674
|
+
|
675
|
+
assert(JSON::Validator.validate(schema,data))
|
676
|
+
data["b"] = 5
|
677
|
+
assert(!JSON::Validator.validate(schema,data))
|
678
|
+
end
|
679
|
+
|
680
|
+
|
681
|
+
def test_items
|
682
|
+
schema = {
|
683
|
+
"items" => { "type" => "integer" }
|
684
|
+
}
|
685
|
+
|
686
|
+
data = [1,2,4]
|
687
|
+
assert(JSON::Validator.validate(schema,data))
|
688
|
+
data = [1,2,"string"]
|
689
|
+
assert(!JSON::Validator.validate(schema,data))
|
690
|
+
|
691
|
+
schema = {
|
692
|
+
"items" => [
|
693
|
+
{"type" => "integer"},
|
694
|
+
{"type" => "string"}
|
695
|
+
]
|
696
|
+
}
|
697
|
+
|
698
|
+
data = [1,"string"]
|
699
|
+
assert(JSON::Validator.validate(schema,data))
|
700
|
+
data = [1,"string",3]
|
701
|
+
assert(JSON::Validator.validate(schema,data))
|
702
|
+
data = ["string",1]
|
703
|
+
assert(!JSON::Validator.validate(schema,data))
|
704
|
+
|
705
|
+
schema = {
|
706
|
+
"items" => [
|
707
|
+
{"type" => "integer"},
|
708
|
+
{"type" => "string"}
|
709
|
+
],
|
710
|
+
"additionalItems" => false
|
711
|
+
}
|
712
|
+
|
713
|
+
data = [1,"string"]
|
714
|
+
assert(JSON::Validator.validate(schema,data))
|
715
|
+
data = [1,"string",3]
|
716
|
+
assert(!JSON::Validator.validate(schema,data))
|
717
|
+
|
718
|
+
schema = {"items" => [{"type" => "integer"},{"type" => "string"}],"additionalItems" => {"type" => "integer"}}
|
719
|
+
|
720
|
+
data = [1,"string"]
|
721
|
+
assert(JSON::Validator.validate(schema,data))
|
722
|
+
data = [1,"string",3]
|
723
|
+
assert(JSON::Validator.validate(schema,data))
|
724
|
+
data = [1,"string","string"]
|
725
|
+
assert(!JSON::Validator.validate(schema,data))
|
726
|
+
end
|
568
727
|
end
|
569
728
|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json-schema
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 5
|
10
|
+
version: 0.1.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Kenny Hoxworth
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-12-01 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -45,6 +45,7 @@ files:
|
|
45
45
|
- lib/json-schema/uri/file.rb
|
46
46
|
- lib/json-schema/validator.rb
|
47
47
|
- lib/json-schema.rb
|
48
|
+
- test/test_files.rb
|
48
49
|
- test/test_jsonschema.rb
|
49
50
|
- README.textile
|
50
51
|
has_rdoc: true
|
@@ -82,4 +83,5 @@ signing_key:
|
|
82
83
|
specification_version: 3
|
83
84
|
summary: Ruby JSON Schema Validator
|
84
85
|
test_files:
|
86
|
+
- test/test_files.rb
|
85
87
|
- test/test_jsonschema.rb
|