json-schema 2.5.1 → 2.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/README.textile +2 -2
  3. data/lib/json-schema/attributes/properties.rb +1 -1
  4. data/lib/json-schema/validator.rb +14 -6
  5. metadata +6 -121
  6. data/test/data/all_of_ref_data.json +0 -3
  7. data/test/data/any_of_ref_data.json +0 -7
  8. data/test/data/bad_data_1.json +0 -3
  9. data/test/data/good_data_1.json +0 -3
  10. data/test/data/one_of_ref_links_data.json +0 -5
  11. data/test/schemas/address_microformat.json +0 -18
  12. data/test/schemas/all_of_ref_base_schema.json +0 -6
  13. data/test/schemas/all_of_ref_schema.json +0 -7
  14. data/test/schemas/any_of_ref_jane_schema.json +0 -4
  15. data/test/schemas/any_of_ref_jimmy_schema.json +0 -4
  16. data/test/schemas/any_of_ref_john_schema.json +0 -4
  17. data/test/schemas/any_of_ref_schema.json +0 -15
  18. data/test/schemas/definition_schema.json +0 -15
  19. data/test/schemas/extends_and_additionalProperties-1-filename.schema.json +0 -34
  20. data/test/schemas/extends_and_additionalProperties-1-ref.schema.json +0 -34
  21. data/test/schemas/extends_and_additionalProperties-2-filename.schema.json +0 -33
  22. data/test/schemas/extends_and_additionalProperties-2-ref.schema.json +0 -33
  23. data/test/schemas/good_schema_1.json +0 -10
  24. data/test/schemas/good_schema_2.json +0 -10
  25. data/test/schemas/good_schema_extends1.json +0 -10
  26. data/test/schemas/good_schema_extends2.json +0 -13
  27. data/test/schemas/inner.schema.json +0 -21
  28. data/test/schemas/one_of_ref_links_schema.json +0 -16
  29. data/test/schemas/ref john with spaces schema.json +0 -11
  30. data/test/schemas/relative_definition_schema.json +0 -8
  31. data/test/schemas/self_link_schema.json +0 -17
  32. data/test/schemas/up_link_schema.json +0 -17
  33. data/test/test_all_of_ref_schema.rb +0 -35
  34. data/test/test_any_of_ref_schema.rb +0 -35
  35. data/test/test_bad_schema_ref.rb +0 -39
  36. data/test/test_common_test_suite.rb +0 -66
  37. data/test/test_custom_format.rb +0 -116
  38. data/test/test_definition.rb +0 -15
  39. data/test/test_extended_schema.rb +0 -62
  40. data/test/test_extends_and_additionalProperties.rb +0 -52
  41. data/test/test_files_v3.rb +0 -43
  42. data/test/test_fragment_resolution.rb +0 -30
  43. data/test/test_fragment_validation_with_ref.rb +0 -34
  44. data/test/test_full_validation.rb +0 -208
  45. data/test/test_helper.rb +0 -47
  46. data/test/test_initialize_data.rb +0 -118
  47. data/test/test_jsonschema_draft1.rb +0 -141
  48. data/test/test_jsonschema_draft2.rb +0 -113
  49. data/test/test_jsonschema_draft3.rb +0 -450
  50. data/test/test_jsonschema_draft4.rb +0 -657
  51. data/test/test_list_option.rb +0 -21
  52. data/test/test_load_ref_schema.rb +0 -40
  53. data/test/test_merge_missing_values.rb +0 -45
  54. data/test/test_minitems.rb +0 -16
  55. data/test/test_one_of.rb +0 -85
  56. data/test/test_ruby_schema.rb +0 -59
  57. data/test/test_schema_loader.rb +0 -74
  58. data/test/test_schema_type_attribute.rb +0 -20
  59. data/test/test_schema_validation.rb +0 -185
  60. data/test/test_stringify.rb +0 -48
  61. data/test/test_uri_related.rb +0 -67
  62. data/test/test_validator.rb +0 -53
@@ -1,15 +0,0 @@
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
@@ -1,62 +0,0 @@
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
@@ -1,52 +0,0 @@
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
@@ -1,43 +0,0 @@
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
@@ -1,30 +0,0 @@
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
@@ -1,34 +0,0 @@
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
@@ -1,208 +0,0 @@
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