json-schema-openc-fork 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/LICENSE.md +19 -0
- data/README.textile +452 -0
- data/lib/json-schema.rb +19 -0
- data/lib/json-schema/attribute.rb +43 -0
- data/lib/json-schema/attributes/additionalitems.rb +28 -0
- data/lib/json-schema/attributes/additionalproperties.rb +58 -0
- data/lib/json-schema/attributes/allof.rb +39 -0
- data/lib/json-schema/attributes/anyof.rb +47 -0
- data/lib/json-schema/attributes/dependencies.rb +44 -0
- data/lib/json-schema/attributes/disallow.rb +12 -0
- data/lib/json-schema/attributes/divisibleby.rb +22 -0
- data/lib/json-schema/attributes/enum.rb +24 -0
- data/lib/json-schema/attributes/extends.rb +50 -0
- data/lib/json-schema/attributes/format.rb +14 -0
- data/lib/json-schema/attributes/formats/custom.rb +21 -0
- data/lib/json-schema/attributes/formats/date.rb +24 -0
- data/lib/json-schema/attributes/formats/date_time.rb +36 -0
- data/lib/json-schema/attributes/formats/date_time_v4.rb +15 -0
- data/lib/json-schema/attributes/formats/ip.rb +41 -0
- data/lib/json-schema/attributes/formats/time.rb +22 -0
- data/lib/json-schema/attributes/formats/uri.rb +20 -0
- data/lib/json-schema/attributes/items.rb +26 -0
- data/lib/json-schema/attributes/limit.rb +179 -0
- data/lib/json-schema/attributes/maxdecimal.rb +18 -0
- data/lib/json-schema/attributes/multipleof.rb +11 -0
- data/lib/json-schema/attributes/not.rb +30 -0
- data/lib/json-schema/attributes/oneof.rb +56 -0
- data/lib/json-schema/attributes/pattern.rb +18 -0
- data/lib/json-schema/attributes/patternproperties.rb +22 -0
- data/lib/json-schema/attributes/properties.rb +74 -0
- data/lib/json-schema/attributes/properties_optional.rb +26 -0
- data/lib/json-schema/attributes/ref.rb +74 -0
- data/lib/json-schema/attributes/required.rb +28 -0
- data/lib/json-schema/attributes/type.rb +83 -0
- data/lib/json-schema/attributes/type_v4.rb +29 -0
- data/lib/json-schema/attributes/uniqueitems.rb +16 -0
- data/lib/json-schema/errors/custom_format_error.rb +6 -0
- data/lib/json-schema/errors/json_parse_error.rb +6 -0
- data/lib/json-schema/errors/schema_error.rb +6 -0
- data/lib/json-schema/errors/validation_error.rb +46 -0
- data/lib/json-schema/schema.rb +63 -0
- data/lib/json-schema/schema/reader.rb +113 -0
- data/lib/json-schema/schema/validator.rb +36 -0
- data/lib/json-schema/util/array_set.rb +14 -0
- data/lib/json-schema/util/uri.rb +16 -0
- data/lib/json-schema/util/uuid.rb +285 -0
- data/lib/json-schema/validator.rb +592 -0
- data/lib/json-schema/validators/draft1.rb +45 -0
- data/lib/json-schema/validators/draft2.rb +46 -0
- data/lib/json-schema/validators/draft3.rb +50 -0
- data/lib/json-schema/validators/draft4.rb +56 -0
- data/lib/json-schema/validators/hyper-draft4.rb +14 -0
- data/resources/draft-01.json +155 -0
- data/resources/draft-02.json +166 -0
- data/resources/draft-03.json +174 -0
- data/resources/draft-04.json +150 -0
- data/test/data/all_of_ref_data.json +3 -0
- data/test/data/any_of_ref_data.json +7 -0
- data/test/data/bad_data_1.json +3 -0
- data/test/data/good_data_1.json +3 -0
- data/test/data/one_of_ref_links_data.json +5 -0
- data/test/schemas/address_microformat.json +18 -0
- data/test/schemas/all_of_ref_base_schema.json +6 -0
- data/test/schemas/all_of_ref_schema.json +7 -0
- data/test/schemas/any_of_ref_jane_schema.json +4 -0
- data/test/schemas/any_of_ref_jimmy_schema.json +4 -0
- data/test/schemas/any_of_ref_john_schema.json +4 -0
- data/test/schemas/any_of_ref_schema.json +15 -0
- data/test/schemas/definition_schema.json +15 -0
- data/test/schemas/extends_and_additionalProperties-1-filename.schema.json +34 -0
- data/test/schemas/extends_and_additionalProperties-1-ref.schema.json +34 -0
- data/test/schemas/extends_and_additionalProperties-2-filename.schema.json +33 -0
- data/test/schemas/extends_and_additionalProperties-2-ref.schema.json +33 -0
- data/test/schemas/good_schema_1.json +10 -0
- data/test/schemas/good_schema_2.json +10 -0
- data/test/schemas/good_schema_extends1.json +10 -0
- data/test/schemas/good_schema_extends2.json +13 -0
- data/test/schemas/inner.schema.json +21 -0
- data/test/schemas/one_of_ref_links_schema.json +16 -0
- data/test/schemas/ref john with spaces schema.json +11 -0
- data/test/schemas/relative_definition_schema.json +8 -0
- data/test/schemas/self_link_schema.json +17 -0
- data/test/schemas/up_link_schema.json +17 -0
- data/test/test_all_of_ref_schema.rb +35 -0
- data/test/test_any_of_ref_schema.rb +35 -0
- data/test/test_bad_schema_ref.rb +39 -0
- data/test/test_common_test_suite.rb +66 -0
- data/test/test_custom_format.rb +116 -0
- data/test/test_definition.rb +15 -0
- data/test/test_extended_schema.rb +62 -0
- data/test/test_extends_and_additionalProperties.rb +52 -0
- data/test/test_files_v3.rb +43 -0
- data/test/test_fragment_resolution.rb +30 -0
- data/test/test_fragment_validation_with_ref.rb +34 -0
- data/test/test_full_validation.rb +208 -0
- data/test/test_helper.rb +47 -0
- data/test/test_initialize_data.rb +118 -0
- data/test/test_jsonschema_draft1.rb +171 -0
- data/test/test_jsonschema_draft2.rb +142 -0
- data/test/test_jsonschema_draft3.rb +502 -0
- data/test/test_jsonschema_draft4.rb +704 -0
- data/test/test_list_option.rb +21 -0
- data/test/test_merge_missing_values.rb +45 -0
- data/test/test_minitems.rb +16 -0
- data/test/test_one_of.rb +85 -0
- data/test/test_ruby_schema.rb +59 -0
- data/test/test_schema_loader.rb +74 -0
- data/test/test_schema_type_attribute.rb +20 -0
- data/test/test_schema_validation.rb +185 -0
- data/test/test_stringify.rb +48 -0
- data/test/test_uri_related.rb +67 -0
- data/test/test_validator.rb +53 -0
- metadata +284 -0
@@ -0,0 +1,116 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path('../test_helper', __FILE__)
|
3
|
+
|
4
|
+
class JSONSchemaCustomFormatTest < Minitest::Test
|
5
|
+
def setup
|
6
|
+
@all_versions = ['draft1', 'draft2', 'draft3', 'draft4']
|
7
|
+
@format_proc = lambda { |value| raise JSON::Schema::CustomFormatError.new("must be 42") unless value == "42" }
|
8
|
+
@schema_4 = {
|
9
|
+
"$schema" => "http://json-schema.org/draft-04/schema#",
|
10
|
+
"properties" => {
|
11
|
+
"a" => {
|
12
|
+
"type" => "string",
|
13
|
+
"format" => "custom",
|
14
|
+
},
|
15
|
+
}
|
16
|
+
}
|
17
|
+
@schema_3 = @schema_4.clone
|
18
|
+
@schema_3["$schema"] = "http://json-schema.org/draft-03/schema#"
|
19
|
+
@schema_2 = @schema_4.clone
|
20
|
+
@schema_2["$schema"] = "http://json-schema.org/draft-02/schema#"
|
21
|
+
@schema_1 = @schema_4.clone
|
22
|
+
@schema_1["$schema"] = "http://json-schema.org/draft-01/schema#"
|
23
|
+
@schemas = {
|
24
|
+
"draft1" => @schema_1,
|
25
|
+
"draft2" => @schema_2,
|
26
|
+
"draft3" => @schema_3,
|
27
|
+
"draft4" => @schema_4
|
28
|
+
}
|
29
|
+
JSON::Validator.restore_default_formats
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_single_registration
|
33
|
+
@all_versions.each do |version|
|
34
|
+
assert(JSON::Validator.validator_for_name(version).formats['custom'].nil?, "Format 'custom' for #{version} should be nil")
|
35
|
+
JSON::Validator.register_format_validator("custom", @format_proc, [version])
|
36
|
+
assert(JSON::Validator.validator_for_name(version).formats['custom'].is_a?(JSON::Schema::CustomFormat), "Format 'custom' should be registered for #{version}")
|
37
|
+
(@all_versions - [version]).each do |other_version|
|
38
|
+
assert(JSON::Validator.validator_for_name(other_version).formats['custom'].nil?, "Format 'custom' should still be nil for #{other_version}")
|
39
|
+
end
|
40
|
+
JSON::Validator.deregister_format_validator("custom", [version])
|
41
|
+
assert(JSON::Validator.validator_for_name(version).formats['custom'].nil?, "Format 'custom' should be deregistered for #{version}")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_register_for_all_by_default
|
46
|
+
JSON::Validator.register_format_validator("custom", @format_proc)
|
47
|
+
@all_versions.each do |version|
|
48
|
+
assert(JSON::Validator.validator_for_name(version).formats['custom'].is_a?(JSON::Schema::CustomFormat), "Format 'custom' should be registered for #{version}")
|
49
|
+
end
|
50
|
+
JSON::Validator.restore_default_formats
|
51
|
+
@all_versions.each do |version|
|
52
|
+
assert(JSON::Validator.validator_for_name(version).formats['custom'].nil?, "Format 'custom' should still be nil for #{version}")
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_multi_registration
|
57
|
+
unregistered_version = @all_versions.delete("draft1")
|
58
|
+
JSON::Validator.register_format_validator("custom", @format_proc, @all_versions)
|
59
|
+
@all_versions.each do |version|
|
60
|
+
assert(JSON::Validator.validator_for_name(version).formats['custom'].is_a?(JSON::Schema::CustomFormat), "Format 'custom' should be registered for #{version}")
|
61
|
+
end
|
62
|
+
assert(JSON::Validator.validator_for_name(unregistered_version).formats['custom'].nil?, "Format 'custom' should still be nil for #{unregistered_version}")
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_format_validation
|
66
|
+
@all_versions.each do |version|
|
67
|
+
data = {
|
68
|
+
"a" => "23"
|
69
|
+
}
|
70
|
+
schema = @schemas[version]
|
71
|
+
prefix = "Validation for '#{version}'"
|
72
|
+
|
73
|
+
assert(JSON::Validator.validate(schema, data), "#{prefix} succeeds with no 'custom' format validator registered")
|
74
|
+
|
75
|
+
JSON::Validator.register_format_validator("custom", @format_proc, [version])
|
76
|
+
data["a"] = "42"
|
77
|
+
assert(JSON::Validator.validate(schema, data), "#{prefix} succeeds with 'custom' format validator and correct data")
|
78
|
+
|
79
|
+
data["a"] = "23"
|
80
|
+
assert(!JSON::Validator.validate(schema, data), "#{prefix} fails with 'custom' format validator and wrong data")
|
81
|
+
|
82
|
+
errors = JSON::Validator.fully_validate(schema, data)
|
83
|
+
assert(errors.count == 1 && errors.first.match(/The property '#\/a' must be 42 in schema/), "#{prefix} records fromat error")
|
84
|
+
|
85
|
+
data["a"] = 23
|
86
|
+
errors = JSON::Validator.fully_validate(schema, data)
|
87
|
+
assert(errors.count == 1 && errors.first.match(/The property '#\/a' of type (?:integer|Fixnum) did not match the following type: string/), "#{prefix} records no fromat error on type mismatch")
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_override_default_format
|
92
|
+
@all_versions.each do |version|
|
93
|
+
data = {
|
94
|
+
"a" => "2001:db8:85a3:0:0:8a2e:370:7334"
|
95
|
+
}
|
96
|
+
schema = @schemas[version]
|
97
|
+
schema["properties"]["a"]["format"] = "ipv6"
|
98
|
+
prefix = "Validation for '#{version}'"
|
99
|
+
|
100
|
+
assert(JSON::Validator.validate(schema, data), "#{prefix} succeeds for default format with correct data")
|
101
|
+
|
102
|
+
data["a"] = "no_ip6_address"
|
103
|
+
assert(!JSON::Validator.validate(schema, data), "#{prefix} fails for default format and wrong data")
|
104
|
+
|
105
|
+
data["a"] = "42"
|
106
|
+
JSON::Validator.register_format_validator("ipv6", @format_proc, [version])
|
107
|
+
assert(JSON::Validator.validate(schema, data), "#{prefix} succeeds with overriden default format and correct data")
|
108
|
+
|
109
|
+
JSON::Validator.deregister_format_validator("ipv6", [version])
|
110
|
+
data["a"] = "2001:db8:85a3:0:0:8a2e:370:7334"
|
111
|
+
assert(JSON::Validator.validate(schema, data), "#{prefix} restores the default format on deregistration")
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class RelativeDefinitionTest < Minitest::Test
|
4
|
+
|
5
|
+
def test_definition_schema
|
6
|
+
assert_valid schema_fixture_path('definition_schema.json'), {"a" => 5}
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_relative_definition
|
10
|
+
schema = schema_fixture_path('relative_definition_schema.json')
|
11
|
+
assert_valid schema, {"a" => 5}
|
12
|
+
refute_valid schema, {"a" => "foo"}
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class BitwiseAndAttribute < JSON::Schema::Attribute
|
4
|
+
def self.validate(current_schema, data, fragments, processor, validator, options = {})
|
5
|
+
return unless data.is_a?(Integer)
|
6
|
+
|
7
|
+
if data & current_schema.schema['bitwise-and'].to_i == 0
|
8
|
+
message = "The property '#{build_fragment(fragments)}' did not evaluate to true when bitwise-AND'd with #{current_schema.schema['bitwise-and']}"
|
9
|
+
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class ExtendedSchema < JSON::Schema::Validator
|
15
|
+
def initialize
|
16
|
+
super
|
17
|
+
extend_schema_definition("http://json-schema.org/draft-03/schema#")
|
18
|
+
@attributes["bitwise-and"] = BitwiseAndAttribute
|
19
|
+
@uri = Addressable::URI.parse("http://test.com/test.json")
|
20
|
+
end
|
21
|
+
|
22
|
+
JSON::Validator.register_validator(self.new)
|
23
|
+
end
|
24
|
+
|
25
|
+
class TestExtendedSchema < Minitest::Test
|
26
|
+
def test_extended_schema_validation
|
27
|
+
schema = {
|
28
|
+
"$schema" => "http://test.com/test.json",
|
29
|
+
"properties" => {
|
30
|
+
"a" => {
|
31
|
+
"bitwise-and" => 1
|
32
|
+
},
|
33
|
+
"b" => {
|
34
|
+
"type" => "string"
|
35
|
+
}
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
assert_valid schema, {"a" => 1, "b" => "taco"}
|
40
|
+
refute_valid schema, {"a" => 0, "b" => "taco"}
|
41
|
+
refute_valid schema, {"a" => 1, "b" => 5}
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_unextended_schema
|
45
|
+
# Verify that using the original schema disregards the `bitwise-and` property
|
46
|
+
schema = {
|
47
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
48
|
+
"properties" => {
|
49
|
+
"a" => {
|
50
|
+
"bitwise-and" => 1
|
51
|
+
},
|
52
|
+
"b" => {
|
53
|
+
"type" => "string"
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
assert_valid schema, {"a" => 0, "b" => "taco"}
|
59
|
+
assert_valid schema, {"a" => 1, "b" => "taco"}
|
60
|
+
refute_valid schema, {"a" => 1, "b" => 5}
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class ExtendsNestedTest < Minitest::Test
|
4
|
+
|
5
|
+
def assert_validity(valid, schema_name, data, msg)
|
6
|
+
msg = "Schema should be #{valid ? :valid : :invalid}.\n(#{schema_name}) #{msg}"
|
7
|
+
schema = schema_fixture_path("#{schema_name}.schema.json")
|
8
|
+
errors = JSON::Validator.fully_validate(schema, data)
|
9
|
+
|
10
|
+
if valid
|
11
|
+
assert_equal([], errors, msg)
|
12
|
+
else
|
13
|
+
refute_equal([], errors, msg)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
%w[
|
18
|
+
extends_and_additionalProperties-1-filename
|
19
|
+
extends_and_additionalProperties-1-ref
|
20
|
+
extends_and_additionalProperties-2-filename
|
21
|
+
extends_and_additionalProperties-2-ref
|
22
|
+
].each do |schema_name|
|
23
|
+
test_prefix = 'test_' + schema_name.gsub('-','_')
|
24
|
+
|
25
|
+
class_eval <<-EOB
|
26
|
+
def #{test_prefix}_valid_outer
|
27
|
+
assert_validity true, '#{schema_name}', {"outerC"=>true}, "Outer defn is broken, maybe the outer extends overrode it"
|
28
|
+
end
|
29
|
+
|
30
|
+
def #{test_prefix}_valid_outer_extended
|
31
|
+
assert_validity true, '#{schema_name}', {"innerA"=>true}, "Extends at the root level isn't working"
|
32
|
+
end
|
33
|
+
|
34
|
+
def #{test_prefix}_valid_inner
|
35
|
+
assert_validity true, '#{schema_name}', {"outerB"=>[{"innerA"=>true}]}, "Extends isn't working in the array element defn"
|
36
|
+
end
|
37
|
+
|
38
|
+
def #{test_prefix}_invalid_inner
|
39
|
+
assert_validity false, '#{schema_name}', {"outerB"=>[{"whaaaaat"=>true}]}, "Array element defn allowing anything when it should only allow what's in inner.schema"
|
40
|
+
end
|
41
|
+
EOB
|
42
|
+
|
43
|
+
if schema_name['extends_and_additionalProperties-1']
|
44
|
+
class_eval <<-EOB
|
45
|
+
def #{test_prefix}_invalid_outer
|
46
|
+
assert_validity false, '#{schema_name}', {"whaaaaat"=>true}, "Outer defn allowing anything when it shouldn't"
|
47
|
+
end
|
48
|
+
EOB
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class JSONSchemaTest < Minitest::Test
|
4
|
+
|
5
|
+
#
|
6
|
+
# These tests are ONLY run if there is an appropriate JSON backend parser available
|
7
|
+
#
|
8
|
+
|
9
|
+
def test_schema_from_file
|
10
|
+
assert_valid schema_fixture_path('good_schema_1.json'), { "a" => 5 }
|
11
|
+
refute_valid schema_fixture_path('good_schema_1.json'), { "a" => "bad" }
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_data_from_file
|
15
|
+
schema = {"$schema" => "http://json-schema.org/draft-03/schema#","type" => "object", "properties" => {"a" => {"type" => "integer"}}}
|
16
|
+
assert_valid schema, data_fixture_path('good_data_1.json'), :uri => true
|
17
|
+
refute_valid schema, data_fixture_path('bad_data_1.json'), :uri => true
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_data_from_json
|
21
|
+
if JSON::Validator.json_backend != nil
|
22
|
+
schema = {"$schema" => "http://json-schema.org/draft-03/schema#","type" => "object", "properties" => {"a" => {"type" => "integer"}}}
|
23
|
+
assert_valid schema, %Q({"a": 5}), :json => true
|
24
|
+
refute_valid schema, %Q({"a": "poop"}), :json => true
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_both_from_file
|
29
|
+
assert_valid schema_fixture_path('good_schema_1.json'), data_fixture_path('good_data_1.json'), :uri => true
|
30
|
+
refute_valid schema_fixture_path('good_schema_1.json'), data_fixture_path('bad_data_1.json'), :uri => true
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_file_ref
|
34
|
+
assert_valid schema_fixture_path('good_schema_2.json'), { "b" => { "a" => 5 } }
|
35
|
+
refute_valid schema_fixture_path('good_schema_1.json'), { "b" => { "a" => "boo" } }
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_file_extends
|
39
|
+
assert_valid schema_fixture_path('good_schema_extends1.json'), { "a" => 5 }
|
40
|
+
assert_valid schema_fixture_path('good_schema_extends2.json'), { "a" => 5, "b" => { "a" => 5 } }
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class FragmentResolution < Minitest::Test
|
4
|
+
def test_fragment_resolution
|
5
|
+
schema = {
|
6
|
+
"$schema" => "http://json-schema.org/draft-04/schema#",
|
7
|
+
"required" => ["a"],
|
8
|
+
"properties" => {
|
9
|
+
"a" => {
|
10
|
+
"type" => "object",
|
11
|
+
"properties" => {
|
12
|
+
"b" => {"type" => "integer" }
|
13
|
+
}
|
14
|
+
}
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
data = {"b" => 5}
|
19
|
+
refute_valid schema, data
|
20
|
+
assert_valid schema, data, :fragment => "#/properties/a"
|
21
|
+
|
22
|
+
assert_raises JSON::Schema::SchemaError do
|
23
|
+
JSON::Validator.validate!(schema,data,:fragment => "/properties/a")
|
24
|
+
end
|
25
|
+
|
26
|
+
assert_raises JSON::Schema::SchemaError do
|
27
|
+
JSON::Validator.validate!(schema,data,:fragment => "#/properties/b")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class FragmentValidationWithRef < Minitest::Test
|
4
|
+
def whole_schema
|
5
|
+
{
|
6
|
+
"$schema" => "http://json-schema.org/draft-04/schema#",
|
7
|
+
"type" => "object",
|
8
|
+
"definitions" => {
|
9
|
+
"post" => {
|
10
|
+
"type" => "object",
|
11
|
+
"properties" => {
|
12
|
+
"content" => {
|
13
|
+
"type" => "string"
|
14
|
+
},
|
15
|
+
"author" => {
|
16
|
+
"type" => "string"
|
17
|
+
}
|
18
|
+
}
|
19
|
+
},
|
20
|
+
"posts" => {
|
21
|
+
"type" => "array",
|
22
|
+
"items" => {
|
23
|
+
"$ref" => "#/definitions/post"
|
24
|
+
}
|
25
|
+
}
|
26
|
+
}
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_validation_of_fragment
|
31
|
+
data = [{"content" => "ohai", "author" => "Bob"}]
|
32
|
+
assert_valid whole_schema, data, :fragment => "#/definitions/posts"
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,208 @@
|
|
1
|
+
require File.expand_path('../test_helper', __FILE__)
|
2
|
+
|
3
|
+
class JSONFullValidation < Minitest::Test
|
4
|
+
|
5
|
+
def test_full_validation
|
6
|
+
data = {"b" => {"a" => 5}}
|
7
|
+
schema = {
|
8
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
9
|
+
"type" => "object",
|
10
|
+
"properties" => {
|
11
|
+
"b" => {
|
12
|
+
"required" => true
|
13
|
+
}
|
14
|
+
}
|
15
|
+
}
|
16
|
+
|
17
|
+
errors = JSON::Validator.fully_validate(schema,data)
|
18
|
+
assert(errors.empty?)
|
19
|
+
|
20
|
+
data = {"c" => 5}
|
21
|
+
schema = {
|
22
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
23
|
+
"type" => "object",
|
24
|
+
"properties" => {
|
25
|
+
"b" => {
|
26
|
+
"required" => true
|
27
|
+
},
|
28
|
+
"c" => {
|
29
|
+
"type" => "string"
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}
|
33
|
+
|
34
|
+
errors = JSON::Validator.fully_validate(schema,data)
|
35
|
+
assert(errors.length == 2)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_full_validation_with_union_types
|
39
|
+
data = {"b" => 5}
|
40
|
+
schema = {
|
41
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
42
|
+
"type" => "object",
|
43
|
+
"properties" => {
|
44
|
+
"b" => {
|
45
|
+
"type" => ["null","integer"]
|
46
|
+
}
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
errors = JSON::Validator.fully_validate(schema,data)
|
51
|
+
assert(errors.empty?)
|
52
|
+
|
53
|
+
schema = {
|
54
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
55
|
+
"type" => "object",
|
56
|
+
"properties" => {
|
57
|
+
"b" => {
|
58
|
+
"type" => ["integer","null"]
|
59
|
+
}
|
60
|
+
}
|
61
|
+
}
|
62
|
+
|
63
|
+
errors = JSON::Validator.fully_validate(schema,data)
|
64
|
+
assert(errors.empty?)
|
65
|
+
|
66
|
+
data = {"b" => "a string"}
|
67
|
+
|
68
|
+
errors = JSON::Validator.fully_validate(schema,data)
|
69
|
+
assert(errors.length == 1)
|
70
|
+
|
71
|
+
schema = {
|
72
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
73
|
+
"type" => "object",
|
74
|
+
"properties" => {
|
75
|
+
"b" => {
|
76
|
+
"type" => [
|
77
|
+
{
|
78
|
+
"type" => "object",
|
79
|
+
"properties" => {
|
80
|
+
"c" => {"type" => "string"}
|
81
|
+
}
|
82
|
+
},
|
83
|
+
{
|
84
|
+
"type" => "object",
|
85
|
+
"properties" => {
|
86
|
+
"d" => {"type" => "integer"}
|
87
|
+
}
|
88
|
+
}
|
89
|
+
]
|
90
|
+
}
|
91
|
+
}
|
92
|
+
}
|
93
|
+
|
94
|
+
data = {"b" => {"c" => "taco"}}
|
95
|
+
|
96
|
+
errors = JSON::Validator.fully_validate(schema,data)
|
97
|
+
assert(errors.empty?)
|
98
|
+
|
99
|
+
data = {"b" => {"d" => 6}}
|
100
|
+
|
101
|
+
errors = JSON::Validator.fully_validate(schema,data)
|
102
|
+
assert(errors.empty?)
|
103
|
+
|
104
|
+
data = {"b" => {"c" => 6, "d" => "OH GOD"}}
|
105
|
+
|
106
|
+
errors = JSON::Validator.fully_validate(schema,data)
|
107
|
+
assert(errors.length == 1)
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
def test_full_validation_with_object_errors
|
112
|
+
data = {"b" => {"a" => 5}}
|
113
|
+
schema = {
|
114
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
115
|
+
"type" => "object",
|
116
|
+
"properties" => {
|
117
|
+
"b" => {
|
118
|
+
"required" => true
|
119
|
+
}
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
errors = JSON::Validator.fully_validate(schema,data,:errors_as_objects => true)
|
124
|
+
assert(errors.empty?)
|
125
|
+
|
126
|
+
data = {"c" => 5}
|
127
|
+
schema = {
|
128
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
129
|
+
"type" => "object",
|
130
|
+
"properties" => {
|
131
|
+
"b" => {
|
132
|
+
"required" => true
|
133
|
+
},
|
134
|
+
"c" => {
|
135
|
+
"type" => "string"
|
136
|
+
}
|
137
|
+
}
|
138
|
+
}
|
139
|
+
|
140
|
+
errors = JSON::Validator.fully_validate(schema,data,:errors_as_objects => true)
|
141
|
+
assert(errors.length == 2)
|
142
|
+
assert(errors[0][:failed_attribute] == "Properties")
|
143
|
+
assert(errors[0][:fragment] == "#/")
|
144
|
+
assert(errors[1][:failed_attribute] == "Type")
|
145
|
+
assert(errors[1][:fragment] == "#/c")
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_full_validation_with_nested_required_properties
|
149
|
+
schema = {
|
150
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
151
|
+
"type" => "object",
|
152
|
+
"properties" => {
|
153
|
+
"x" => {
|
154
|
+
"required" => true,
|
155
|
+
"type" => "object",
|
156
|
+
"properties" => {
|
157
|
+
"a" => {"type"=>"integer","required"=>true},
|
158
|
+
"b" => {"type"=>"integer","required"=>true},
|
159
|
+
"c" => {"type"=>"integer","required"=>false},
|
160
|
+
"d" => {"type"=>"integer","required"=>false},
|
161
|
+
"e" => {"type"=>"integer","required"=>false},
|
162
|
+
}
|
163
|
+
}
|
164
|
+
}
|
165
|
+
}
|
166
|
+
data = {"x" => {"a"=>5, "d"=>5, "e"=>"what?"}}
|
167
|
+
|
168
|
+
errors = JSON::Validator.fully_validate(schema,data,:errors_as_objects => true)
|
169
|
+
assert_equal 2, errors.length
|
170
|
+
assert_equal '#/x', errors[0][:fragment]
|
171
|
+
assert_equal 'Properties', errors[0][:failed_attribute]
|
172
|
+
assert_equal '#/x/e', errors[1][:fragment]
|
173
|
+
assert_equal 'Type', errors[1][:failed_attribute]
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_full_validation_with_nested_required_propertiesin_array
|
177
|
+
schema = {
|
178
|
+
"$schema" => "http://json-schema.org/draft-03/schema#",
|
179
|
+
"type" => "object",
|
180
|
+
"properties" => {
|
181
|
+
"x" => {
|
182
|
+
"required" => true,
|
183
|
+
"type" => "array",
|
184
|
+
"items" => {
|
185
|
+
"type" => "object",
|
186
|
+
"properties" => {
|
187
|
+
"a" => {"type"=>"integer","required"=>true},
|
188
|
+
"b" => {"type"=>"integer","required"=>true},
|
189
|
+
"c" => {"type"=>"integer","required"=>false},
|
190
|
+
"d" => {"type"=>"integer","required"=>false},
|
191
|
+
"e" => {"type"=>"integer","required"=>false},
|
192
|
+
}
|
193
|
+
}
|
194
|
+
}
|
195
|
+
}
|
196
|
+
}
|
197
|
+
missing_b= {"a"=>5}
|
198
|
+
e_is_wrong_type= {"a"=>5,"b"=>5,"e"=>"what?"}
|
199
|
+
data = {"x" => [missing_b, e_is_wrong_type]}
|
200
|
+
|
201
|
+
errors = JSON::Validator.fully_validate(schema,data,:errors_as_objects => true)
|
202
|
+
assert_equal 2, errors.length
|
203
|
+
assert_equal '#/x/0', errors[0][:fragment]
|
204
|
+
assert_equal 'Properties', errors[0][:failed_attribute]
|
205
|
+
assert_equal '#/x/1/e', errors[1][:fragment]
|
206
|
+
assert_equal 'Type', errors[1][:failed_attribute]
|
207
|
+
end
|
208
|
+
end
|