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
data/test/test_helper.rb DELETED
@@ -1,47 +0,0 @@
1
- require 'minitest/autorun'
2
- require 'webmock/minitest'
3
-
4
- $:.unshift(File.expand_path('../../lib', __FILE__))
5
- require 'json-schema'
6
-
7
- Dir[File.join(File.expand_path('../support', __FILE__), '*.rb')].each do |support_file|
8
- require support_file
9
- end
10
-
11
- class Minitest::Test
12
- def schema_fixture_path(filename)
13
- File.join(File.dirname(__FILE__), 'schemas', filename)
14
- end
15
-
16
- def data_fixture_path(filename)
17
- File.join(File.dirname(__FILE__), 'data', filename)
18
- end
19
-
20
- def assert_valid(schema, data, options = {})
21
- if !options.key?(:version) && respond_to?(:schema_version)
22
- options = options.merge(:version => schema_version)
23
- end
24
-
25
- errors = JSON::Validator.fully_validate(schema, data, options)
26
- assert_equal([], errors, "#{data.inspect} should be valid for schema:\n#{schema.inspect}")
27
- end
28
-
29
- def refute_valid(schema, data, options = {})
30
- if !options.key?(:version) && respond_to?(:schema_version)
31
- options = options.merge(:version => schema_version)
32
- end
33
-
34
- errors = JSON::Validator.fully_validate(schema, data, options)
35
- refute_equal([], errors, "#{data.inspect} should be invalid for schema:\n#{schema.inspect}")
36
- end
37
-
38
- def parser_error
39
- if defined?(::Yajl)
40
- Yajl::ParseError
41
- elsif defined?(::MultiJson)
42
- MultiJson::ParseError
43
- else
44
- JSON::ParserError
45
- end
46
- end
47
- end
@@ -1,118 +0,0 @@
1
- require File.expand_path('../test_helper', __FILE__)
2
-
3
- class InitializeDataTest < Minitest::Test
4
-
5
- def test_parse_character_string
6
- schema = {'type' => 'string'}
7
- data = 'hello world'
8
-
9
- assert(JSON::Validator.validate(schema, data))
10
-
11
- assert(JSON::Validator.validate(schema, data, :parse_data => false))
12
-
13
- assert_raises(parser_error) do
14
- JSON::Validator.validate(schema, data, :json => true)
15
- end
16
-
17
- assert_raises(Errno::ENOENT) { JSON::Validator.validate(schema, data, :uri => true) }
18
- end
19
-
20
- def test_parse_integer_string
21
- schema = {'type' => 'integer'}
22
- data = '42'
23
-
24
- assert(JSON::Validator.validate(schema, data))
25
-
26
- refute(JSON::Validator.validate(schema, data, :parse_data => false))
27
-
28
- assert(JSON::Validator.validate(schema, data, :json => true))
29
-
30
- assert_raises(Errno::ENOENT) { JSON::Validator.validate(schema, data, :uri => true) }
31
- end
32
-
33
- def test_parse_hash_string
34
- schema = { 'type' => 'object', 'properties' => { 'a' => { 'type' => 'string' } } }
35
- data = '{"a": "b"}'
36
-
37
- assert(JSON::Validator.validate(schema, data))
38
-
39
- refute(JSON::Validator.validate(schema, data, :parse_data => false))
40
-
41
- assert(JSON::Validator.validate(schema, data, :json => true))
42
-
43
- assert_raises(Errno::ENOENT) { JSON::Validator.validate(schema, data, :uri => true) }
44
- end
45
-
46
- def test_parse_json_string
47
- schema = {'type' => 'string'}
48
- data = '"hello world"'
49
-
50
- assert(JSON::Validator.validate(schema, data))
51
-
52
- assert(JSON::Validator.validate(schema, data, :parse_data => false))
53
-
54
- assert(JSON::Validator.validate(schema, data, :json => true))
55
-
56
- assert_raises(Errno::ENOENT) { JSON::Validator.validate(schema, data, :uri => true) }
57
- end
58
-
59
- def test_parse_valid_uri_string
60
- schema = {'type' => 'string'}
61
- data = 'http://foo.bar/'
62
-
63
- stub_request(:get, "foo.bar").to_return(:body => '"hello world"', :status => 200)
64
-
65
- assert(JSON::Validator.validate(schema, data))
66
-
67
- assert(JSON::Validator.validate(schema, data, :parse_data => false))
68
-
69
- assert_raises(parser_error) do
70
- JSON::Validator.validate(schema, data, :json => true)
71
- end
72
-
73
- assert(JSON::Validator.validate(schema, data, :uri => true))
74
- end
75
-
76
- def test_parse_invalid_uri_string
77
- schema = {'type' => 'string'}
78
- data = 'http://foo.bar/'
79
-
80
- stub_request(:get, "foo.bar").to_timeout
81
-
82
- assert(JSON::Validator.validate(schema, data))
83
-
84
- assert(JSON::Validator.validate(schema, data, :parse_data => false))
85
-
86
- assert_raises(parser_error) do
87
- JSON::Validator.validate(schema, data, :json => true)
88
- end
89
-
90
- assert_raises(Timeout::Error) { JSON::Validator.validate(schema, data, :uri => true) }
91
- end
92
-
93
- def test_parse_integer
94
- schema = {'type' => 'integer'}
95
- data = 42
96
-
97
- assert(JSON::Validator.validate(schema, data))
98
-
99
- assert(JSON::Validator.validate(schema, data, :parse_data => false))
100
-
101
- assert_raises(TypeError) { JSON::Validator.validate(schema, data, :json => true) }
102
-
103
- assert_raises(TypeError) { JSON::Validator.validate(schema, data, :uri => true) }
104
- end
105
-
106
- def test_parse_hash
107
- schema = { 'type' => 'object', 'properties' => { 'a' => { 'type' => 'string' } } }
108
- data = { 'a' => 'b' }
109
-
110
- assert(JSON::Validator.validate(schema, data))
111
-
112
- assert(JSON::Validator.validate(schema, data, :parse_data => false))
113
-
114
- assert_raises(TypeError) { JSON::Validator.validate(schema, data, :json => true) }
115
-
116
- assert_raises(TypeError) { JSON::Validator.validate(schema, data, :uri => true) }
117
- end
118
- end
@@ -1,141 +0,0 @@
1
- require File.expand_path('../test_helper', __FILE__)
2
-
3
- class JSONSchemaDraft1Test < Minitest::Test
4
- def schema_version
5
- :draft1
6
- end
7
-
8
- def exclusive_minimum
9
- { 'minimumCanEqual' => false }
10
- end
11
-
12
- def exclusive_maximum
13
- { 'maximumCanEqual' => false }
14
- end
15
-
16
- include ArrayValidation::ItemsTests
17
-
18
- include EnumValidation::General
19
- include EnumValidation::V1_V2
20
-
21
- include NumberValidation::MinMaxTests
22
-
23
- include ObjectValidation::AdditionalPropertiesTests
24
-
25
- include StrictValidation
26
-
27
- include StringValidation::ValueTests
28
- include StringValidation::FormatTests
29
- include StringValidation::DateAndTimeFormatTests
30
-
31
- include TypeValidation::SimpleTypeTests
32
- include TypeValidation::AnyTypeTests
33
- include TypeValidation::SchemaUnionTypeTests
34
-
35
- def test_optional
36
- # Set up the default datatype
37
- schema = {
38
- "properties" => {
39
- "a" => {"type" => "string"}
40
- }
41
- }
42
- data = {}
43
-
44
- refute_valid schema, data
45
- data['a'] = "Hello"
46
- assert_valid schema, data
47
-
48
- schema = {
49
- "properties" => {
50
- "a" => {"type" => "integer", "optional" => "true"}
51
- }
52
- }
53
-
54
- data = {}
55
- assert_valid schema, data
56
- end
57
-
58
- def test_max_decimal
59
- # Set up the default datatype
60
- schema = {
61
- "properties" => {
62
- "a" => {"maxDecimal" => 2}
63
- }
64
- }
65
-
66
- data = {
67
- "a" => nil
68
- }
69
-
70
- data["a"] = 3.35
71
- assert_valid schema, data
72
-
73
- data["a"] = 3.455
74
- refute_valid schema, data
75
-
76
-
77
- schema["properties"]["a"]["maxDecimal"] = 0
78
-
79
- data["a"] = 4.0
80
- refute_valid schema, data
81
-
82
- data["a"] = 'boo'
83
- assert_valid schema, data
84
-
85
- data["a"] = 5
86
- assert_valid schema, data
87
- end
88
-
89
-
90
-
91
- def test_disallow
92
- # Set up the default datatype
93
- schema = {
94
- "properties" => {
95
- "a" => {"disallow" => "integer"}
96
- }
97
- }
98
-
99
- data = {
100
- "a" => nil
101
- }
102
-
103
-
104
- data["a"] = 'string'
105
- assert_valid schema, data
106
-
107
- data["a"] = 5
108
- refute_valid schema, data
109
-
110
-
111
- schema["properties"]["a"]["disallow"] = ["integer","string"]
112
- data["a"] = 'string'
113
- refute_valid schema, data
114
-
115
- data["a"] = 5
116
- refute_valid schema, data
117
-
118
- data["a"] = false
119
- assert_valid schema, data
120
-
121
- end
122
-
123
- def test_format_datetime
124
- schema = {
125
- "type" => "object",
126
- "properties" => { "a" => {"type" => "string", "format" => "date-time"}}
127
- }
128
-
129
- assert_valid schema, {"a" => "2010-01-01T12:00:00Z"}
130
- refute_valid schema, {"a" => "2010-01-32T12:00:00Z"}
131
- refute_valid schema, {"a" => "2010-13-01T12:00:00Z"}
132
- refute_valid schema, {"a" => "2010-01-01T24:00:00Z"}
133
- refute_valid schema, {"a" => "2010-01-01T12:60:00Z"}
134
- refute_valid schema, {"a" => "2010-01-01T12:00:60Z"}
135
- refute_valid schema, {"a" => "2010-01-01T12:00:00z"}
136
- refute_valid schema, {"a" => "2010-01-0112:00:00Z"}
137
- refute_valid schema, {"a" => "2010-01-01T12:00:00Z\nabc"}
138
- end
139
-
140
- end
141
-
@@ -1,113 +0,0 @@
1
- require File.expand_path('../test_helper', __FILE__)
2
-
3
- class JSONSchemaDraft2Test < Minitest::Test
4
- def schema_version
5
- :draft2
6
- end
7
-
8
- def exclusive_minimum
9
- { 'minimumCanEqual' => false }
10
- end
11
-
12
- def exclusive_maximum
13
- { 'maximumCanEqual' => false }
14
- end
15
-
16
- def multiple_of
17
- 'divisibleBy'
18
- end
19
-
20
- include ArrayValidation::ItemsTests
21
- include ArrayValidation::UniqueItemsTests
22
-
23
- include EnumValidation::General
24
- include EnumValidation::V1_V2
25
-
26
- include NumberValidation::MinMaxTests
27
- include NumberValidation::MultipleOfTests
28
-
29
- include ObjectValidation::AdditionalPropertiesTests
30
-
31
- include StrictValidation
32
-
33
- include StringValidation::ValueTests
34
- include StringValidation::FormatTests
35
- include StringValidation::DateAndTimeFormatTests
36
-
37
- include TypeValidation::SimpleTypeTests
38
- include TypeValidation::AnyTypeTests
39
- include TypeValidation::SchemaUnionTypeTests
40
-
41
- def test_optional
42
- # Set up the default datatype
43
- schema = {
44
- "properties" => {
45
- "a" => {"type" => "string"}
46
- }
47
- }
48
- data = {}
49
-
50
- refute_valid schema, data
51
- data['a'] = "Hello"
52
- assert_valid schema, data
53
-
54
- schema = {
55
- "properties" => {
56
- "a" => {"type" => "integer", "optional" => "true"}
57
- }
58
- }
59
-
60
- data = {}
61
- assert_valid schema, data
62
- end
63
-
64
- def test_disallow
65
- # Set up the default datatype
66
- schema = {
67
- "properties" => {
68
- "a" => {"disallow" => "integer"}
69
- }
70
- }
71
-
72
- data = {
73
- "a" => nil
74
- }
75
-
76
-
77
- data["a"] = 'string'
78
- assert_valid schema, data
79
-
80
- data["a"] = 5
81
- refute_valid schema, data
82
-
83
-
84
- schema["properties"]["a"]["disallow"] = ["integer","string"]
85
- data["a"] = 'string'
86
- refute_valid schema, data
87
-
88
- data["a"] = 5
89
- refute_valid schema, data
90
-
91
- data["a"] = false
92
- assert_valid schema, data
93
- end
94
-
95
- def test_format_datetime
96
- schema = {
97
- "type" => "object",
98
- "properties" => { "a" => {"type" => "string", "format" => "date-time"}}
99
- }
100
-
101
- assert_valid schema, {"a" => "2010-01-01T12:00:00Z"}
102
- refute_valid schema, {"a" => "2010-01-32T12:00:00Z"}
103
- refute_valid schema, {"a" => "2010-13-01T12:00:00Z"}
104
- refute_valid schema, {"a" => "2010-01-01T24:00:00Z"}
105
- refute_valid schema, {"a" => "2010-01-01T12:60:00Z"}
106
- refute_valid schema, {"a" => "2010-01-01T12:00:60Z"}
107
- refute_valid schema, {"a" => "2010-01-01T12:00:00z"}
108
- refute_valid schema, {"a" => "2010-01-0112:00:00Z"}
109
- refute_valid schema, {"a" => "2010-01-01T12:00:00Z\nabc"}
110
- end
111
-
112
- end
113
-
@@ -1,450 +0,0 @@
1
- # encoding: utf-8
2
- require File.expand_path('../test_helper', __FILE__)
3
-
4
- class JSONSchemaDraft3Test < Minitest::Test
5
- def schema_version
6
- :draft3
7
- end
8
-
9
- def exclusive_minimum
10
- { 'exclusiveMinimum' => true }
11
- end
12
-
13
- def exclusive_maximum
14
- { 'exclusiveMaximum' => true }
15
- end
16
-
17
- def multiple_of
18
- 'divisibleBy'
19
- end
20
-
21
- include ArrayValidation::ItemsTests
22
- include ArrayValidation::AdditionalItemsTests
23
- include ArrayValidation::UniqueItemsTests
24
-
25
- include EnumValidation::General
26
- include EnumValidation::V3_V4
27
-
28
- include NumberValidation::MinMaxTests
29
- include NumberValidation::MultipleOfTests
30
-
31
- include ObjectValidation::AdditionalPropertiesTests
32
- include ObjectValidation::PatternPropertiesTests
33
-
34
- include StrictValidation
35
-
36
- include StringValidation::ValueTests
37
- include StringValidation::FormatTests
38
- include StringValidation::DateAndTimeFormatTests
39
-
40
- include TypeValidation::SimpleTypeTests
41
- include TypeValidation::AnyTypeTests
42
- include TypeValidation::SchemaUnionTypeTests
43
-
44
- def test_types
45
- # Set up the default datatype
46
- schema = {
47
- "$schema" => "http://json-schema.org/draft-03/schema#",
48
- "properties" => {
49
- "a" => {}
50
- }
51
- }
52
- data = {
53
- "a" => nil
54
- }
55
-
56
- # Test an array of unioned-type objects that prevent additionalProperties
57
- schema["properties"]["a"] = {
58
- 'type' => 'array',
59
- 'items' => {
60
- 'type' => [
61
- { 'type' => 'object', 'properties' => { "b" => { "type" => "integer" } } },
62
- { 'type' => 'object', 'properties' => { "c" => { "type" => "string" } } }
63
- ],
64
- 'additionalProperties' => false
65
- }
66
- }
67
-
68
- data["a"] = [{"b" => 5}, {"c" => "foo"}]
69
- errors = JSON::Validator.fully_validate(schema, data)
70
- assert(errors.empty?, errors.join("\n"))
71
-
72
- # This should actually pass, because this matches the first schema in the union
73
- data["a"] << {"c" => false}
74
- assert_valid schema, data
75
- end
76
-
77
- def test_required
78
- # Set up the default datatype
79
- schema = {
80
- "$schema" => "http://json-schema.org/draft-03/schema#",
81
- "properties" => {
82
- "a" => {"required" => true}
83
- }
84
- }
85
- data = {}
86
-
87
- refute_valid schema, data
88
- data['a'] = "Hello"
89
- assert_valid schema, data
90
-
91
- schema = {
92
- "$schema" => "http://json-schema.org/draft-03/schema#",
93
- "properties" => {
94
- "a" => {"type" => "integer"}
95
- }
96
- }
97
-
98
- data = {}
99
- assert_valid schema, data
100
- end
101
-
102
- def test_strict_properties_required_props
103
- schema = {
104
- "$schema" => "http://json-schema.org/draft-03/schema#",
105
- "properties" => {
106
- "a" => {"type" => "string", "required" => true},
107
- "b" => {"type" => "string", "required" => false}
108
- }
109
- }
110
-
111
- data = {"a" => "a"}
112
- assert(JSON::Validator.validate(schema,data,:strict => true))
113
-
114
- data = {"b" => "b"}
115
- assert(!JSON::Validator.validate(schema,data,:strict => true))
116
-
117
- data = {"a" => "a", "b" => "b"}
118
- assert(JSON::Validator.validate(schema,data,:strict => true))
119
- end
120
-
121
- def test_strict_properties_additional_props
122
- schema = {
123
- "$schema" => "http://json-schema.org/draft-03/schema#",
124
- "properties" => {
125
- "a" => {"type" => "string"},
126
- "b" => {"type" => "string"}
127
- },
128
- "additionalProperties" => {"type" => "integer"}
129
- }
130
-
131
- data = {"a" => "a"}
132
- assert(!JSON::Validator.validate(schema,data,:strict => true))
133
-
134
- data = {"b" => "b"}
135
- assert(!JSON::Validator.validate(schema,data,:strict => true))
136
-
137
- data = {"a" => "a", "b" => "b"}
138
- assert(JSON::Validator.validate(schema,data,:strict => true))
139
-
140
- data = {"a" => "a", "b" => "b", "c" => "c"}
141
- assert(!JSON::Validator.validate(schema,data,:strict => true))
142
-
143
- data = {"a" => "a", "b" => "b", "c" => 3}
144
- assert(JSON::Validator.validate(schema,data,:strict => true))
145
- end
146
-
147
- def test_strict_properties_pattern_props
148
- schema = {
149
- "$schema" => "http://json-schema.org/draft-03/schema#",
150
- "properties" => {
151
- "a" => {"type" => "string"},
152
- "b" => {"type" => "string"}
153
- },
154
- "patternProperties" => {"\\d+ taco" => {"type" => "integer"}}
155
- }
156
-
157
- data = {"a" => "a"}
158
- assert(!JSON::Validator.validate(schema,data,:strict => true))
159
-
160
- data = {"b" => "b"}
161
- assert(!JSON::Validator.validate(schema,data,:strict => true))
162
-
163
- data = {"a" => "a", "b" => "b"}
164
- assert(JSON::Validator.validate(schema,data,:strict => true))
165
-
166
- data = {"a" => "a", "b" => "b", "c" => "c"}
167
- assert(!JSON::Validator.validate(schema,data,:strict => true))
168
-
169
- data = {"a" => "a", "b" => "b", "c" => 3}
170
- assert(!JSON::Validator.validate(schema,data,:strict => true))
171
-
172
- data = {"a" => "a", "b" => "b", "23 taco" => 3}
173
- assert(JSON::Validator.validate(schema,data,:strict => true))
174
-
175
- data = {"a" => "a", "b" => "b", "23 taco" => "cheese"}
176
- assert(!JSON::Validator.validate(schema,data,:strict => true))
177
- end
178
-
179
- def test_disallow
180
- # Set up the default datatype
181
- schema = {
182
- "$schema" => "http://json-schema.org/draft-03/schema#",
183
- "properties" => {
184
- "a" => {"disallow" => "integer"}
185
- }
186
- }
187
-
188
- data = {
189
- "a" => nil
190
- }
191
-
192
-
193
- data["a"] = 'string'
194
- assert_valid schema, data
195
-
196
- data["a"] = 5
197
- refute_valid schema, data
198
-
199
-
200
- schema["properties"]["a"]["disallow"] = ["integer","string"]
201
- data["a"] = 'string'
202
- refute_valid schema, data
203
-
204
- data["a"] = 5
205
- refute_valid schema, data
206
-
207
- data["a"] = false
208
- assert_valid schema, data
209
-
210
- end
211
-
212
-
213
-
214
- def test_extends
215
- schema = {
216
- "$schema" => "http://json-schema.org/draft-03/schema#",
217
- "properties" => {
218
- "a" => { "type" => "integer"}
219
- }
220
- }
221
-
222
- schema2 = {
223
- "$schema" => "http://json-schema.org/draft-03/schema#",
224
- "properties" => {
225
- "a" => { "maximum" => 5 }
226
- }
227
- }
228
-
229
- data = {
230
- "a" => 10
231
- }
232
-
233
- assert_valid schema, data
234
- assert(!JSON::Validator.validate(schema2,data))
235
-
236
- schema["extends"] = schema2
237
-
238
- refute_valid schema, data
239
- end
240
-
241
- def test_list_option
242
- schema = {
243
- "$schema" => "http://json-schema.org/draft-03/schema#",
244
- "type" => "object",
245
- "properties" => { "a" => {"type" => "integer", "required" => true} }
246
- }
247
-
248
- data = [{"a" => 1},{"a" => 2},{"a" => 3}]
249
- assert(JSON::Validator.validate(schema,data,:list => true))
250
- refute_valid schema, data
251
-
252
- data = {"a" => 1}
253
- assert(!JSON::Validator.validate(schema,data,:list => true))
254
-
255
- data = [{"a" => 1},{"b" => 2},{"a" => 3}]
256
- assert(!JSON::Validator.validate(schema,data,:list => true))
257
- end
258
-
259
-
260
- def test_self_reference
261
- schema = {
262
- "$schema" => "http://json-schema.org/draft-03/schema#",
263
- "type" => "object",
264
- "properties" => { "a" => {"type" => "integer"}, "b" => {"$ref" => "#"}}
265
- }
266
-
267
- assert_valid schema, {"a" => 5, "b" => {"b" => {"a" => 1}}}
268
- refute_valid schema, {"a" => 5, "b" => {"b" => {"a" => 'taco'}}}
269
- end
270
-
271
- def test_format_datetime
272
- schema = {
273
- "$schema" => "http://json-schema.org/draft-03/schema#",
274
- "type" => "object",
275
- "properties" => { "a" => {"type" => "string", "format" => "date-time"}}
276
- }
277
-
278
- assert_valid schema, {"a" => "2010-01-01T12:00:00Z"}
279
- assert_valid schema, {"a" => "2010-01-01T12:00:00.1Z"}
280
- assert_valid schema, {"a" => "2010-01-01T12:00:00,1Z"}
281
- refute_valid schema, {"a" => "2010-01-32T12:00:00Z"}
282
- refute_valid schema, {"a" => "2010-13-01T12:00:00Z"}
283
- refute_valid schema, {"a" => "2010-01-01T24:00:00Z"}
284
- refute_valid schema, {"a" => "2010-01-01T12:60:00Z"}
285
- refute_valid schema, {"a" => "2010-01-01T12:00:60Z"}
286
- refute_valid schema, {"a" => "2010-01-01T12:00:00z"}
287
- refute_valid schema, {"a" => "2010-01-0112:00:00Z"}
288
- refute_valid schema, {"a" => "2010-01-01T12:00:00.1Z\nabc"}
289
-
290
- # test with a specific timezone
291
- assert_valid schema, {"a" => "2010-01-01T12:00:00+01"}
292
- assert_valid schema, {"a" => "2010-01-01T12:00:00+01:00"}
293
- assert_valid schema, {"a" => "2010-01-01T12:00:00+01:30"}
294
- assert_valid schema, {"a" => "2010-01-01T12:00:00+0234"}
295
- refute_valid schema, {"a" => "2010-01-01T12:00:00+01:"}
296
- refute_valid schema, {"a" => "2010-01-01T12:00:00+0"}
297
- # do not allow mixing Z and specific timezone
298
- refute_valid schema, {"a" => "2010-01-01T12:00:00Z+01"}
299
- refute_valid schema, {"a" => "2010-01-01T12:00:00+01Z"}
300
- refute_valid schema, {"a" => "2010-01-01T12:00:00+01:30Z"}
301
- refute_valid schema, {"a" => "2010-01-01T12:00:00+0Z"}
302
-
303
- # test without any timezone
304
- assert_valid schema, {"a" => "2010-01-01T12:00:00"}
305
- assert_valid schema, {"a" => "2010-01-01T12:00:00.12345"}
306
- assert_valid schema, {"a" => "2010-01-01T12:00:00,12345"}
307
- assert_valid schema, {"a" => "2010-01-01T12:00:00.12345"}
308
- end
309
-
310
- def test_format_uri
311
- data1 = {"a" => "http://gitbuh.com"}
312
- data2 = {"a" => "::boo"}
313
- data3 = {"a" => "http://ja.wikipedia.org/wiki/メインページ"}
314
-
315
- schema = {
316
- "$schema" => "http://json-schema.org/draft-03/schema#",
317
- "type" => "object",
318
- "properties" => { "a" => {"type" => "string", "format" => "uri"}}
319
- }
320
-
321
- assert(JSON::Validator.validate(schema,data1))
322
- assert(!JSON::Validator.validate(schema,data2))
323
- assert(JSON::Validator.validate(schema,data3))
324
- end
325
-
326
-
327
-
328
- def test_schema
329
- schema = {
330
- "$schema" => "http://json-schema.org/THIS-IS-NOT-A-SCHEMA",
331
- "type" => "object"
332
- }
333
-
334
- data = {"a" => "taco"}
335
- assert(!JSON::Validator.validate(schema, data))
336
-
337
- schema = {
338
- "$schema" => "http://json-schema.org/draft-03/schema#",
339
- "type" => "object"
340
- }
341
- assert_valid schema, data
342
- end
343
-
344
- def test_dependency
345
- schema = {
346
- "$schema" => "http://json-schema.org/draft-03/schema#",
347
- "type" => "object",
348
- "properties" => {
349
- "a" => {"type" => "integer"},
350
- "b" => {"type" => "integer"}
351
- },
352
- "dependencies" => {
353
- "a" => "b"
354
- }
355
- }
356
-
357
- data = {"a" => 1, "b" => 2}
358
- assert_valid schema, data
359
- data = {"a" => 1}
360
- refute_valid schema, data
361
-
362
- schema = {
363
- "$schema" => "http://json-schema.org/draft-03/schema#",
364
- "type" => "object",
365
- "properties" => {
366
- "a" => {"type" => "integer"},
367
- "b" => {"type" => "integer"},
368
- "c" => {"type" => "integer"}
369
- },
370
- "dependencies" => {
371
- "a" => ["b","c"]
372
- }
373
- }
374
-
375
- data = {"a" => 1, "c" => 2}
376
- refute_valid schema, data
377
- data = {"a" => 1, "b" => 2, "c" => 3}
378
- assert_valid schema, data
379
- end
380
-
381
- def test_default
382
- schema = {
383
- "$schema" => "http://json-schema.org/draft-03/schema#",
384
- "type" => "object",
385
- "properties" => {
386
- "a" => {"type" => "integer", "default" => 42},
387
- "b" => {"type" => "integer"}
388
- }
389
- }
390
-
391
- data = {:b => 2}
392
- assert_valid schema, data
393
- assert_nil(data["a"])
394
- assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
395
- assert_equal(42, data["a"])
396
- assert_equal(2, data[:b])
397
-
398
- schema = {
399
- "$schema" => "http://json-schema.org/draft-03/schema#",
400
- "type" => "object",
401
- "properties" => {
402
- "a" => {"type" => "integer", "default" => 42, "required" => true},
403
- "b" => {"type" => "integer"}
404
- }
405
- }
406
-
407
- data = {:b => 2}
408
- refute_valid schema, data
409
- assert_nil(data["a"])
410
- assert(JSON::Validator.validate(schema,data, :insert_defaults => true))
411
- assert_equal(42, data["a"])
412
- assert_equal(2, data[:b])
413
-
414
- schema = {
415
- "$schema" => "http://json-schema.org/draft-03/schema#",
416
- "type" => "object",
417
- "properties" => {
418
- "a" => {"type" => "integer", "default" => 42, "required" => true, "readonly" => true},
419
- "b" => {"type" => "integer"}
420
- }
421
- }
422
-
423
- data = {:b => 2}
424
- refute_valid schema, data
425
- assert_nil(data["a"])
426
- assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
427
- assert_nil(data["a"])
428
- assert_equal(2, data[:b])
429
-
430
- schema = {
431
- "$schema" => "http://json-schema.org/draft-03/schema#",
432
- "type" => "object",
433
- "properties" => {
434
- "a" => {"type" => "integer", "default" => "42"},
435
- "b" => {"type" => "integer"}
436
- }
437
- }
438
-
439
- data = {:b => 2}
440
- assert_valid schema, data
441
- assert_nil(data["a"])
442
- assert(!JSON::Validator.validate(schema,data, :insert_defaults => true))
443
- assert_equal("42",data["a"])
444
- assert_equal(2, data[:b])
445
-
446
- end
447
-
448
-
449
- end
450
-