json-schema 2.4.1 → 2.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +6 -14
  2. data/README.textile +58 -7
  3. data/lib/json-schema.rb +3 -1
  4. data/lib/json-schema/attributes/additionalitems.rb +14 -11
  5. data/lib/json-schema/attributes/additionalproperties.rb +33 -43
  6. data/lib/json-schema/attributes/anyof.rb +4 -0
  7. data/lib/json-schema/attributes/dependencies.rb +31 -19
  8. data/lib/json-schema/attributes/disallow.rb +2 -3
  9. data/lib/json-schema/attributes/divisibleby.rb +11 -7
  10. data/lib/json-schema/attributes/enum.rb +14 -16
  11. data/lib/json-schema/attributes/format.rb +4 -7
  12. data/lib/json-schema/attributes/formats/date_time_v4.rb +5 -8
  13. data/lib/json-schema/attributes/formats/ip.rb +41 -0
  14. data/lib/json-schema/attributes/formats/uri.rb +10 -8
  15. data/lib/json-schema/attributes/items.rb +15 -16
  16. data/lib/json-schema/attributes/limit.rb +179 -0
  17. data/lib/json-schema/attributes/maxdecimal.rb +7 -6
  18. data/lib/json-schema/attributes/multipleof.rb +4 -11
  19. data/lib/json-schema/attributes/not.rb +1 -1
  20. data/lib/json-schema/attributes/oneof.rb +15 -6
  21. data/lib/json-schema/attributes/pattern.rb +7 -6
  22. data/lib/json-schema/attributes/patternproperties.rb +9 -12
  23. data/lib/json-schema/attributes/properties.rb +55 -39
  24. data/lib/json-schema/attributes/properties_optional.rb +13 -12
  25. data/lib/json-schema/attributes/ref.rb +4 -4
  26. data/lib/json-schema/attributes/required.rb +16 -13
  27. data/lib/json-schema/attributes/type.rb +13 -18
  28. data/lib/json-schema/attributes/type_v4.rb +11 -18
  29. data/lib/json-schema/attributes/uniqueitems.rb +5 -7
  30. data/lib/json-schema/schema.rb +8 -8
  31. data/lib/json-schema/schema/#validator.rb# +37 -0
  32. data/lib/json-schema/schema/reader.rb +113 -0
  33. data/lib/json-schema/util/uri.rb +16 -0
  34. data/lib/json-schema/validator.rb +123 -128
  35. data/lib/json-schema/validators/draft1.rb +1 -1
  36. data/lib/json-schema/validators/draft2.rb +1 -1
  37. data/lib/json-schema/validators/draft3.rb +1 -1
  38. data/lib/json-schema/validators/draft4.rb +1 -1
  39. data/lib/json-schema/validators/hyper-draft4.rb +1 -1
  40. data/test/schemas/address_microformat.json +18 -0
  41. data/test/schemas/definition_schema.json +15 -0
  42. data/test/schemas/ref john with spaces schema.json +11 -0
  43. data/test/schemas/relative_definition_schema.json +8 -0
  44. data/test/test_all_of_ref_schema.rb +12 -15
  45. data/test/test_any_of_ref_schema.rb +7 -9
  46. data/test/test_bad_schema_ref.rb +18 -12
  47. data/test/test_common_test_suite.rb +45 -29
  48. data/test/test_custom_format.rb +2 -3
  49. data/test/test_definition.rb +15 -0
  50. data/test/test_extended_schema.rb +25 -31
  51. data/test/test_extends_and_additionalProperties.rb +23 -21
  52. data/test/test_files_v3.rb +14 -23
  53. data/test/test_fragment_resolution.rb +6 -7
  54. data/test/test_fragment_validation_with_ref.rb +2 -8
  55. data/test/test_full_validation.rb +2 -3
  56. data/test/test_helper.rb +46 -1
  57. data/test/test_initialize_data.rb +118 -0
  58. data/test/test_jsonschema_draft1.rb +48 -600
  59. data/test/test_jsonschema_draft2.rb +48 -699
  60. data/test/test_jsonschema_draft3.rb +91 -861
  61. data/test/test_jsonschema_draft4.rb +173 -812
  62. data/test/test_list_option.rb +6 -7
  63. data/test/{test_merge_misisng_values.rb → test_merge_missing_values.rb} +2 -3
  64. data/test/test_minitems.rb +2 -4
  65. data/test/test_one_of.rb +9 -19
  66. data/test/test_ruby_schema.rb +5 -14
  67. data/test/test_schema_loader.rb +74 -0
  68. data/test/test_schema_type_attribute.rb +2 -3
  69. data/test/test_schema_validation.rb +4 -5
  70. data/test/test_stringify.rb +2 -3
  71. data/test/test_uri_related.rb +67 -0
  72. data/test/test_validator.rb +53 -0
  73. metadata +129 -51
  74. data/lib/json-schema/attributes/dependencies_v4.rb +0 -27
  75. data/lib/json-schema/attributes/formats/ip4.rb +0 -20
  76. data/lib/json-schema/attributes/formats/ip6.rb +0 -20
  77. data/lib/json-schema/attributes/maximum.rb +0 -17
  78. data/lib/json-schema/attributes/maximum_inclusive.rb +0 -17
  79. data/lib/json-schema/attributes/maxitems.rb +0 -14
  80. data/lib/json-schema/attributes/maxlength.rb +0 -16
  81. data/lib/json-schema/attributes/maxproperties.rb +0 -14
  82. data/lib/json-schema/attributes/minimum.rb +0 -17
  83. data/lib/json-schema/attributes/minimum_inclusive.rb +0 -17
  84. data/lib/json-schema/attributes/minitems.rb +0 -14
  85. data/lib/json-schema/attributes/minlength.rb +0 -16
  86. data/lib/json-schema/attributes/minproperties.rb +0 -14
  87. data/lib/json-schema/attributes/properties_v4.rb +0 -58
  88. data/lib/json-schema/uri/file.rb +0 -36
@@ -1,52 +1,43 @@
1
- require 'test/unit'
2
- require File.dirname(__FILE__) + '/../lib/json-schema'
1
+ require File.expand_path('../test_helper', __FILE__)
3
2
 
4
- class JSONSchemaTest < Test::Unit::TestCase
3
+ class JSONSchemaTest < Minitest::Test
5
4
 
6
5
  #
7
6
  # These tests are ONLY run if there is an appropriate JSON backend parser available
8
7
  #
9
8
 
10
9
  def test_schema_from_file
11
- data = {"a" => 5}
12
- assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
13
- data = {"a" => "bad"}
14
- assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
10
+ assert_valid schema_fixture_path('good_schema_1.json'), { "a" => 5 }
11
+ refute_valid schema_fixture_path('good_schema_1.json'), { "a" => "bad" }
15
12
  end
16
13
 
17
14
  def test_data_from_file
18
15
  schema = {"$schema" => "http://json-schema.org/draft-03/schema#","type" => "object", "properties" => {"a" => {"type" => "integer"}}}
19
- assert(JSON::Validator.validate_uri(schema,File.join(File.dirname(__FILE__),"data/good_data_1.json")))
20
- assert(!JSON::Validator.validate_uri(schema,File.join(File.dirname(__FILE__),"data/bad_data_1.json")))
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
21
18
  end
22
19
 
23
20
  def test_data_from_json
24
21
  if JSON::Validator.json_backend != nil
25
22
  schema = {"$schema" => "http://json-schema.org/draft-03/schema#","type" => "object", "properties" => {"a" => {"type" => "integer"}}}
26
- assert(JSON::Validator.validate_json(schema, %Q({"a" : 5})))
27
- assert(!JSON::Validator.validate_json(schema, %Q({"a" : "poop"})))
23
+ assert_valid schema, %Q({"a": 5}), :json => true
24
+ refute_valid schema, %Q({"a": "poop"}), :json => true
28
25
  end
29
26
  end
30
27
 
31
28
  def test_both_from_file
32
- assert(JSON::Validator.validate_uri(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),File.join(File.dirname(__FILE__),"data/good_data_1.json")))
33
- assert(!JSON::Validator.validate_uri(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),File.join(File.dirname(__FILE__),"data/bad_data_1.json")))
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
34
31
  end
35
32
 
36
33
  def test_file_ref
37
- data = {"b" => {"a" => 5}}
38
- assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_2.json"),data))
39
-
40
- data = {"b" => {"a" => "boo"}}
41
- assert(!JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_1.json"),data))
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" } }
42
36
  end
43
37
 
44
38
  def test_file_extends
45
- data = {"a" => 5}
46
- assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_extends1.json"),data))
47
-
48
- data = {"a" => 5, "b" => {"a" => 5}}
49
- assert(JSON::Validator.validate(File.join(File.dirname(__FILE__),"schemas/good_schema_extends2.json"),data))
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 } }
50
41
  end
51
42
 
52
43
  end
@@ -1,7 +1,6 @@
1
- require 'test/unit'
2
- require File.dirname(__FILE__) + '/../lib/json-schema'
1
+ require File.expand_path('../test_helper', __FILE__)
3
2
 
4
- class FragmentResolution < Test::Unit::TestCase
3
+ class FragmentResolution < Minitest::Test
5
4
  def test_fragment_resolution
6
5
  schema = {
7
6
  "$schema" => "http://json-schema.org/draft-04/schema#",
@@ -17,14 +16,14 @@ class FragmentResolution < Test::Unit::TestCase
17
16
  }
18
17
 
19
18
  data = {"b" => 5}
20
- assert(!JSON::Validator.validate(schema,data))
21
- assert(JSON::Validator.validate(schema,data,:fragment => "#/properties/a"))
19
+ refute_valid schema, data
20
+ assert_valid schema, data, :fragment => "#/properties/a"
22
21
 
23
- assert_raise JSON::Schema::SchemaError do
22
+ assert_raises JSON::Schema::SchemaError do
24
23
  JSON::Validator.validate!(schema,data,:fragment => "/properties/a")
25
24
  end
26
25
 
27
- assert_raise JSON::Schema::SchemaError do
26
+ assert_raises JSON::Schema::SchemaError do
28
27
  JSON::Validator.validate!(schema,data,:fragment => "#/properties/b")
29
28
  end
30
29
  end
@@ -1,7 +1,6 @@
1
1
  require File.expand_path('../test_helper', __FILE__)
2
- require 'json-schema'
3
2
 
4
- class FragmentValidationWithRef < Test::Unit::TestCase
3
+ class FragmentValidationWithRef < Minitest::Test
5
4
  def whole_schema
6
5
  {
7
6
  "$schema" => "http://json-schema.org/draft-04/schema#",
@@ -30,11 +29,6 @@ class FragmentValidationWithRef < Test::Unit::TestCase
30
29
 
31
30
  def test_validation_of_fragment
32
31
  data = [{"content" => "ohai", "author" => "Bob"}]
33
- v = nil
34
- assert_nothing_raised do
35
- v = JSON::Validator.fully_validate(whole_schema,data,:fragment => "#/definitions/posts")
36
- end
37
-
38
- assert(v.empty?, v.join("\n"))
32
+ assert_valid whole_schema, data, :fragment => "#/definitions/posts"
39
33
  end
40
34
  end
@@ -1,7 +1,6 @@
1
- require 'test/unit'
2
- require File.dirname(__FILE__) + '/../lib/json-schema'
1
+ require File.expand_path('../test_helper', __FILE__)
3
2
 
4
- class JSONFullValidation < Test::Unit::TestCase
3
+ class JSONFullValidation < Minitest::Test
5
4
 
6
5
  def test_full_validation
7
6
  data = {"b" => {"a" => 5}}
@@ -1,2 +1,47 @@
1
- require 'test/unit'
1
+ require 'minitest/autorun'
2
+ require 'webmock/minitest'
3
+
2
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
@@ -0,0 +1,118 @@
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,169 +1,31 @@
1
- require 'test/unit'
2
- require File.dirname(__FILE__) + '/../lib/json-schema'
1
+ require File.expand_path('../test_helper', __FILE__)
3
2
 
4
- class JSONSchemaDraft1Test < Test::Unit::TestCase
5
- def test_types
6
- # Set up the default datatype
7
- schema = {
8
- "properties" => {
9
- "a" => {}
10
- }
11
- }
12
- data = {
13
- "a" => nil
14
- }
15
-
16
- # Test integers
17
- schema["properties"]["a"]["type"] = "integer"
18
- data["a"] = 5
19
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
20
-
21
- data["a"] = 5.2
22
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
23
-
24
- data['a'] = 'string'
25
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
26
-
27
- data['a'] = true
28
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
29
-
30
-
31
- # Test numbers
32
- schema["properties"]["a"]["type"] = "number"
33
- data["a"] = 5
34
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
35
-
36
- data["a"] = 5.2
37
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
38
-
39
- data['a'] = 'string'
40
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
41
-
42
- data['a'] = true
43
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
44
-
45
-
46
- # Test strings
47
- schema["properties"]["a"]["type"] = "string"
48
- data["a"] = 5
49
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
50
-
51
- data["a"] = 5.2
52
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
53
-
54
- data['a'] = 'string'
55
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
56
-
57
- data['a'] = true
58
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
59
-
60
-
61
- # Test booleans
62
- schema["properties"]["a"]["type"] = "boolean"
63
- data["a"] = 5
64
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
65
-
66
- data["a"] = 5.2
67
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
68
-
69
- data['a'] = 'string'
70
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
71
-
72
- data['a'] = true
73
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
74
-
75
- data['a'] = false
76
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
77
-
78
-
79
- # Test object
80
- schema["properties"]["a"]["type"] = "object"
81
- data["a"] = {}
82
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
83
-
84
- data["a"] = 5.2
85
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
86
-
87
- data['a'] = 'string'
88
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
89
-
90
- data['a'] = true
91
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
92
-
93
-
94
- # Test array
95
- schema["properties"]["a"]["type"] = "array"
96
- data["a"] = []
97
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
98
-
99
- data["a"] = 5.2
100
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
101
-
102
- data['a'] = 'string'
103
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
104
-
105
- data['a'] = true
106
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
107
-
108
-
109
- # Test null
110
- schema["properties"]["a"]["type"] = "null"
111
- data["a"] = nil
112
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
113
-
114
- data["a"] = 5.2
115
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
116
-
117
- data['a'] = 'string'
118
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
119
-
120
- data['a'] = true
121
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
122
-
123
-
124
- # Test any
125
- schema["properties"]["a"]["type"] = "any"
126
- data["a"] = 5
127
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
128
-
129
- data["a"] = 5.2
130
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
131
-
132
- data['a'] = 'string'
133
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
134
-
135
- data['a'] = true
136
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
137
-
138
-
139
- # Test a union type
140
- schema["properties"]["a"]["type"] = ["integer","string"]
141
- data["a"] = 5
142
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
143
-
144
- data["a"] = 'boo'
145
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
146
-
147
- data["a"] = false
148
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
3
+ class JSONSchemaDraft1Test < Minitest::Test
4
+ def schema_version
5
+ :draft1
6
+ end
149
7
 
150
- # Test a union type with schemas
151
- schema["properties"]["a"]["type"] = [{ "type" => "string" }, {"type" => "object", "properties" => {"b" => {"type" => "integer"}}}]
8
+ def exclusive_minimum
9
+ { 'minimumCanEqual' => false }
10
+ end
152
11
 
153
- data["a"] = "test"
154
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
12
+ def exclusive_maximum
13
+ { 'maximumCanEqual' => false }
14
+ end
155
15
 
156
- data["a"] = 5
157
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
16
+ include ArrayValidation::ItemsTests
158
17
 
159
- data["a"] = {"b" => 5}
160
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
18
+ include NumberValidation::MinMaxTests
161
19
 
162
- data["a"] = {"b" => "taco"}
163
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
164
- end
20
+ include ObjectValidation::AdditionalPropertiesTests
165
21
 
22
+ include StringValidation::ValueTests
23
+ include StringValidation::FormatTests
24
+ include StringValidation::DateAndTimeFormatTests
166
25
 
26
+ include TypeValidation::SimpleTypeTests
27
+ include TypeValidation::AnyTypeTests
28
+ include TypeValidation::SchemaUnionTypeTests
167
29
 
168
30
  def test_optional
169
31
  # Set up the default datatype
@@ -174,9 +36,9 @@ class JSONSchemaDraft1Test < Test::Unit::TestCase
174
36
  }
175
37
  data = {}
176
38
 
177
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
39
+ refute_valid schema, data
178
40
  data['a'] = "Hello"
179
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
41
+ assert_valid schema, data
180
42
 
181
43
  schema = {
182
44
  "properties" => {
@@ -185,237 +47,9 @@ class JSONSchemaDraft1Test < Test::Unit::TestCase
185
47
  }
186
48
 
187
49
  data = {}
188
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
189
-
190
- end
191
-
192
-
193
-
194
- def test_minimum
195
- # Set up the default datatype
196
- schema = {
197
- "properties" => {
198
- "a" => {"minimum" => 5}
199
- }
200
- }
201
-
202
- data = {
203
- "a" => nil
204
- }
205
-
206
-
207
- # Test an integer
208
- data["a"] = 5
209
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
210
-
211
- data["a"] = 4
212
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
213
-
214
- # Test a float
215
- data["a"] = 5.0
216
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
217
-
218
- data["a"] = 4.9
219
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
220
-
221
- # Test a non-number
222
- data["a"] = "a string"
223
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
224
-
225
- # Test exclusiveMinimum
226
- schema["properties"]["a"]["minimumCanEqual"] = false
227
-
228
- data["a"] = 6
229
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
230
-
231
- data["a"] = 5
232
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
233
-
234
- # Test with float
235
- data["a"] = 5.00000001
236
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
237
-
238
- data["a"] = 5.0
239
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
240
- end
241
-
242
-
243
-
244
- def test_maximum
245
- # Set up the default datatype
246
- schema = {
247
- "properties" => {
248
- "a" => {"maximum" => 5}
249
- }
250
- }
251
-
252
- data = {
253
- "a" => nil
254
- }
255
-
256
-
257
- # Test an integer
258
- data["a"] = 5
259
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
260
-
261
- data["a"] = 6
262
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
263
-
264
- # Test a float
265
- data["a"] = 5.0
266
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
267
-
268
- data["a"] = 5.1
269
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
270
-
271
- # Test a non-number
272
- data["a"] = "a string"
273
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
274
-
275
- # Test exclusiveMinimum
276
- schema["properties"]["a"]["maximumCanEqual"] = false
277
-
278
- data["a"] = 4
279
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
280
-
281
- data["a"] = 5
282
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
283
-
284
- # Test with float
285
- data["a"] = 4.9999999
286
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
287
-
288
- data["a"] = 5.0
289
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
290
- end
291
-
292
-
293
- def test_min_items
294
- # Set up the default datatype
295
- schema = {
296
- "properties" => {
297
- "a" => {"minItems" => 1}
298
- }
299
- }
300
-
301
- data = {
302
- "a" => nil
303
- }
304
-
305
- # Test with an array
306
- data["a"] = ["boo"]
307
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
308
-
309
- data["a"] = []
310
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
311
-
312
- # Test with a non-array
313
- data["a"] = "boo"
314
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
315
- end
316
-
317
-
318
-
319
- def test_max_items
320
- # Set up the default datatype
321
- schema = {
322
- "properties" => {
323
- "a" => {"maxItems" => 1}
324
- }
325
- }
326
-
327
- data = {
328
- "a" => nil
329
- }
330
-
331
- # Test with an array
332
- data["a"] = ["boo"]
333
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
334
-
335
- data["a"] = ["boo","taco"]
336
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
337
-
338
- # Test with a non-array
339
- data["a"] = "boo"
340
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
341
- end
342
-
343
-
344
- def test_pattern
345
- # Set up the default datatype
346
- schema = {
347
- "properties" => {
348
- "a" => {"pattern" => "\\d+ taco"}
349
- }
350
- }
351
-
352
- data = {
353
- "a" => nil
354
- }
355
-
356
- # Test strings
357
- data["a"] = "156 taco bell"
358
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
359
-
360
- # Test a non-string
361
- data["a"] = 5
362
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
363
-
364
- data["a"] = "taco"
365
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
366
- end
367
-
368
-
369
- def test_min_length
370
- # Set up the default datatype
371
- schema = {
372
- "properties" => {
373
- "a" => {"minLength" => 1}
374
- }
375
- }
376
-
377
- data = {
378
- "a" => nil
379
- }
380
-
381
- # Try out strings
382
- data["a"] = "t"
383
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
384
-
385
- data["a"] = ""
386
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
387
-
388
- # Try out non-string
389
- data["a"] = 5
390
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
391
- end
392
-
393
-
394
- def test_max_length
395
- # Set up the default datatype
396
- schema = {
397
- "properties" => {
398
- "a" => {"maxLength" => 1}
399
- }
400
- }
401
-
402
- data = {
403
- "a" => nil
404
- }
405
-
406
- # Try out strings
407
- data["a"] = "t"
408
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
409
-
410
- data["a"] = "tt"
411
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
412
-
413
- # Try out non-string
414
- data["a"] = 5
415
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
50
+ assert_valid schema, data
416
51
  end
417
52
 
418
-
419
53
  def test_enum
420
54
  # Set up the default datatype
421
55
  schema = {
@@ -430,24 +64,24 @@ class JSONSchemaDraft1Test < Test::Unit::TestCase
430
64
 
431
65
  # Make sure all of the above are valid...
432
66
  data["a"] = 1
433
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
67
+ assert_valid schema, data
434
68
 
435
69
  data["a"] = 'boo'
436
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
70
+ assert_valid schema, data
437
71
 
438
72
  data["a"] = [1,2,3]
439
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
73
+ assert_valid schema, data
440
74
 
441
75
  data["a"] = {"a" => "b"}
442
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
76
+ assert_valid schema, data
443
77
 
444
78
  # Test something that doesn't exist
445
79
  data["a"] = 'taco'
446
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
80
+ refute_valid schema, data
447
81
 
448
82
  # Try it without the key
449
83
  data = {}
450
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
84
+ assert_valid schema, data
451
85
  end
452
86
 
453
87
 
@@ -464,22 +98,22 @@ class JSONSchemaDraft1Test < Test::Unit::TestCase
464
98
  }
465
99
 
466
100
  data["a"] = 3.35
467
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
101
+ assert_valid schema, data
468
102
 
469
103
  data["a"] = 3.455
470
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
104
+ refute_valid schema, data
471
105
 
472
106
 
473
107
  schema["properties"]["a"]["maxDecimal"] = 0
474
108
 
475
109
  data["a"] = 4.0
476
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
110
+ refute_valid schema, data
477
111
 
478
112
  data["a"] = 'boo'
479
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
113
+ assert_valid schema, data
480
114
 
481
115
  data["a"] = 5
482
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
116
+ assert_valid schema, data
483
117
  end
484
118
 
485
119
 
@@ -498,225 +132,39 @@ class JSONSchemaDraft1Test < Test::Unit::TestCase
498
132
 
499
133
 
500
134
  data["a"] = 'string'
501
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
135
+ assert_valid schema, data
502
136
 
503
137
  data["a"] = 5
504
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
138
+ refute_valid schema, data
505
139
 
506
140
 
507
141
  schema["properties"]["a"]["disallow"] = ["integer","string"]
508
142
  data["a"] = 'string'
509
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
143
+ refute_valid schema, data
510
144
 
511
145
  data["a"] = 5
512
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
146
+ refute_valid schema, data
513
147
 
514
148
  data["a"] = false
515
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
149
+ assert_valid schema, data
516
150
 
517
151
  end
518
152
 
519
-
520
-
521
- def test_additional_properties
522
- # Test no additional properties allowed
523
- schema = {
524
- "properties" => {
525
- "a" => { "type" => "integer" }
526
- },
527
- "additionalProperties" => false
528
- }
529
-
530
- data = {
531
- "a" => 10
532
- }
533
-
534
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
535
- data["b"] = 5
536
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
537
-
538
- # Test additional properties match a schema
539
- schema["additionalProperties"] = { "type" => "string" }
540
- data["b"] = "taco"
541
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
542
- data["b"] = 5
543
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
544
- end
545
-
546
-
547
- def test_items
548
- schema = {
549
- "items" => { "type" => "integer" }
550
- }
551
-
552
- data = [1,2,4]
553
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
554
- data = [1,2,"string"]
555
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
556
-
557
- schema = {
558
- "items" => [
559
- {"type" => "integer"},
560
- {"type" => "string"}
561
- ]
562
- }
563
-
564
- data = [1,"string"]
565
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
566
- data = [1,"string",3]
567
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
568
- data = ["string",1]
569
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
570
-
571
- end
572
-
573
-
574
- def test_format_ipv4
575
- schema = {
576
- "type" => "object",
577
- "properties" => { "a" => {"type" => "string", "format" => "ip-address"}}
578
- }
579
-
580
- data = {"a" => "1.1.1.1"}
581
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
582
- data = {"a" => "1.1.1"}
583
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
584
- data = {"a" => "1.1.1.300"}
585
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
586
- data = {"a" => 5}
587
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
588
- data = {"a" => "1.1.1"}
589
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
590
- data = {"a" => "1.1.1.1b"}
591
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
592
- data = {"a" => "b1.1.1.1"}
593
- end
594
-
595
-
596
- def test_format_ipv6
597
- schema = {
598
- "type" => "object",
599
- "properties" => { "a" => {"type" => "string", "format" => "ipv6"}}
600
- }
601
-
602
- data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff"}
603
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
604
- data = {"a" => "1111:0:8888:0:0:0:eeee:ffff"}
605
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
606
- data = {"a" => "1111:2222:8888::eeee:ffff"}
607
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
608
- data = {"a" => "1111:2222:8888:99999:aaaa:cccc:eeee:ffff"}
609
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
610
- data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:gggg"}
611
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
612
- data = {"a" => "1111:2222::9999::cccc:eeee:ffff"}
613
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
614
- data = {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"}
615
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
616
- assert(JSON::Validator.validate(schema, {"a" => "::1"}, :version => :draft1), 'validate with shortcut')
617
- assert(!JSON::Validator.validate(schema, {"a" => "42"}, :version => :draft1), 'not validate a simple number')
618
- end
619
-
620
- def test_format_time
621
- schema = {
622
- "type" => "object",
623
- "properties" => { "a" => {"type" => "string", "format" => "time"}}
624
- }
625
-
626
- data = {"a" => "12:00:00"}
627
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
628
- data = {"a" => "12:00"}
629
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
630
- data = {"a" => "12:00:60"}
631
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
632
- data = {"a" => "12:60:00"}
633
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
634
- data = {"a" => "24:00:00"}
635
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
636
- data = {"a" => "0:00:00"}
637
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
638
- data = {"a" => "-12:00:00"}
639
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
640
- data = {"a" => "12:00:00b"}
641
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
642
- data = {"a" => "12:00:00"}
643
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
644
- data = {"a" => "12:00:00\nabc"}
645
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
646
- end
647
-
648
-
649
- def test_format_date
650
- schema = {
651
- "type" => "object",
652
- "properties" => { "a" => {"type" => "string", "format" => "date"}}
653
- }
654
-
655
- data = {"a" => "2010-01-01"}
656
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
657
- data = {"a" => "2010-01-32"}
658
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
659
- data = {"a" => "n2010-01-01"}
660
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
661
- data = {"a" => "2010-1-01"}
662
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
663
- data = {"a" => "2010-01-1"}
664
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
665
- data = {"a" => "2010-01-01n"}
666
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
667
- data = {"a" => "2010-01-01\nabc"}
668
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
669
- end
670
-
671
153
  def test_format_datetime
672
154
  schema = {
673
155
  "type" => "object",
674
156
  "properties" => { "a" => {"type" => "string", "format" => "date-time"}}
675
157
  }
676
158
 
677
- data = {"a" => "2010-01-01T12:00:00Z"}
678
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
679
- data = {"a" => "2010-01-32T12:00:00Z"}
680
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
681
- data = {"a" => "2010-13-01T12:00:00Z"}
682
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
683
- data = {"a" => "2010-01-01T24:00:00Z"}
684
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
685
- data = {"a" => "2010-01-01T12:60:00Z"}
686
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
687
- data = {"a" => "2010-01-01T12:00:60Z"}
688
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
689
- data = {"a" => "2010-01-01T12:00:00z"}
690
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
691
- data = {"a" => "2010-01-0112:00:00Z"}
692
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
693
- data = {"a" => "2010-01-01T12:00:00Z\nabc"}
694
- assert(!JSON::Validator.validate(schema,data,:version => :draft1))
695
- end
696
-
697
- def test_format_unknown
698
- schema = {
699
- "type" => "object",
700
- "properties" => { "a" => {"type" => "string", "format" => "unknown"}}
701
- }
702
-
703
- data = {"a" => "I can write what I want here"}
704
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
705
- data = {"a" => ""}
706
- assert(JSON::Validator.validate(schema,data,:version => :draft1))
707
- end
708
-
709
-
710
- def test_format_union
711
- data1 = {"a" => "boo"}
712
- data2 = {"a" => nil}
713
-
714
- schema = {
715
- "type" => "object",
716
- "properties" => { "a" => {"type" => ["string","null"], "format" => "ip-address"}}
717
- }
718
- assert(!JSON::Validator.validate(schema,data1,:version => :draft1))
719
- assert(JSON::Validator.validate(schema,data2,:version => :draft1))
159
+ assert_valid schema, {"a" => "2010-01-01T12:00:00Z"}
160
+ refute_valid schema, {"a" => "2010-01-32T12:00:00Z"}
161
+ refute_valid schema, {"a" => "2010-13-01T12:00:00Z"}
162
+ refute_valid schema, {"a" => "2010-01-01T24:00:00Z"}
163
+ refute_valid schema, {"a" => "2010-01-01T12:60:00Z"}
164
+ refute_valid schema, {"a" => "2010-01-01T12:00:60Z"}
165
+ refute_valid schema, {"a" => "2010-01-01T12:00:00z"}
166
+ refute_valid schema, {"a" => "2010-01-0112:00:00Z"}
167
+ refute_valid schema, {"a" => "2010-01-01T12:00:00Z\nabc"}
720
168
  end
721
169
 
722
170
  end