json-schema-openc-fork 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (114) hide show
  1. checksums.yaml +15 -0
  2. data/LICENSE.md +19 -0
  3. data/README.textile +452 -0
  4. data/lib/json-schema.rb +19 -0
  5. data/lib/json-schema/attribute.rb +43 -0
  6. data/lib/json-schema/attributes/additionalitems.rb +28 -0
  7. data/lib/json-schema/attributes/additionalproperties.rb +58 -0
  8. data/lib/json-schema/attributes/allof.rb +39 -0
  9. data/lib/json-schema/attributes/anyof.rb +47 -0
  10. data/lib/json-schema/attributes/dependencies.rb +44 -0
  11. data/lib/json-schema/attributes/disallow.rb +12 -0
  12. data/lib/json-schema/attributes/divisibleby.rb +22 -0
  13. data/lib/json-schema/attributes/enum.rb +24 -0
  14. data/lib/json-schema/attributes/extends.rb +50 -0
  15. data/lib/json-schema/attributes/format.rb +14 -0
  16. data/lib/json-schema/attributes/formats/custom.rb +21 -0
  17. data/lib/json-schema/attributes/formats/date.rb +24 -0
  18. data/lib/json-schema/attributes/formats/date_time.rb +36 -0
  19. data/lib/json-schema/attributes/formats/date_time_v4.rb +15 -0
  20. data/lib/json-schema/attributes/formats/ip.rb +41 -0
  21. data/lib/json-schema/attributes/formats/time.rb +22 -0
  22. data/lib/json-schema/attributes/formats/uri.rb +20 -0
  23. data/lib/json-schema/attributes/items.rb +26 -0
  24. data/lib/json-schema/attributes/limit.rb +179 -0
  25. data/lib/json-schema/attributes/maxdecimal.rb +18 -0
  26. data/lib/json-schema/attributes/multipleof.rb +11 -0
  27. data/lib/json-schema/attributes/not.rb +30 -0
  28. data/lib/json-schema/attributes/oneof.rb +56 -0
  29. data/lib/json-schema/attributes/pattern.rb +18 -0
  30. data/lib/json-schema/attributes/patternproperties.rb +22 -0
  31. data/lib/json-schema/attributes/properties.rb +74 -0
  32. data/lib/json-schema/attributes/properties_optional.rb +26 -0
  33. data/lib/json-schema/attributes/ref.rb +74 -0
  34. data/lib/json-schema/attributes/required.rb +28 -0
  35. data/lib/json-schema/attributes/type.rb +83 -0
  36. data/lib/json-schema/attributes/type_v4.rb +29 -0
  37. data/lib/json-schema/attributes/uniqueitems.rb +16 -0
  38. data/lib/json-schema/errors/custom_format_error.rb +6 -0
  39. data/lib/json-schema/errors/json_parse_error.rb +6 -0
  40. data/lib/json-schema/errors/schema_error.rb +6 -0
  41. data/lib/json-schema/errors/validation_error.rb +46 -0
  42. data/lib/json-schema/schema.rb +63 -0
  43. data/lib/json-schema/schema/reader.rb +113 -0
  44. data/lib/json-schema/schema/validator.rb +36 -0
  45. data/lib/json-schema/util/array_set.rb +14 -0
  46. data/lib/json-schema/util/uri.rb +16 -0
  47. data/lib/json-schema/util/uuid.rb +285 -0
  48. data/lib/json-schema/validator.rb +592 -0
  49. data/lib/json-schema/validators/draft1.rb +45 -0
  50. data/lib/json-schema/validators/draft2.rb +46 -0
  51. data/lib/json-schema/validators/draft3.rb +50 -0
  52. data/lib/json-schema/validators/draft4.rb +56 -0
  53. data/lib/json-schema/validators/hyper-draft4.rb +14 -0
  54. data/resources/draft-01.json +155 -0
  55. data/resources/draft-02.json +166 -0
  56. data/resources/draft-03.json +174 -0
  57. data/resources/draft-04.json +150 -0
  58. data/test/data/all_of_ref_data.json +3 -0
  59. data/test/data/any_of_ref_data.json +7 -0
  60. data/test/data/bad_data_1.json +3 -0
  61. data/test/data/good_data_1.json +3 -0
  62. data/test/data/one_of_ref_links_data.json +5 -0
  63. data/test/schemas/address_microformat.json +18 -0
  64. data/test/schemas/all_of_ref_base_schema.json +6 -0
  65. data/test/schemas/all_of_ref_schema.json +7 -0
  66. data/test/schemas/any_of_ref_jane_schema.json +4 -0
  67. data/test/schemas/any_of_ref_jimmy_schema.json +4 -0
  68. data/test/schemas/any_of_ref_john_schema.json +4 -0
  69. data/test/schemas/any_of_ref_schema.json +15 -0
  70. data/test/schemas/definition_schema.json +15 -0
  71. data/test/schemas/extends_and_additionalProperties-1-filename.schema.json +34 -0
  72. data/test/schemas/extends_and_additionalProperties-1-ref.schema.json +34 -0
  73. data/test/schemas/extends_and_additionalProperties-2-filename.schema.json +33 -0
  74. data/test/schemas/extends_and_additionalProperties-2-ref.schema.json +33 -0
  75. data/test/schemas/good_schema_1.json +10 -0
  76. data/test/schemas/good_schema_2.json +10 -0
  77. data/test/schemas/good_schema_extends1.json +10 -0
  78. data/test/schemas/good_schema_extends2.json +13 -0
  79. data/test/schemas/inner.schema.json +21 -0
  80. data/test/schemas/one_of_ref_links_schema.json +16 -0
  81. data/test/schemas/ref john with spaces schema.json +11 -0
  82. data/test/schemas/relative_definition_schema.json +8 -0
  83. data/test/schemas/self_link_schema.json +17 -0
  84. data/test/schemas/up_link_schema.json +17 -0
  85. data/test/test_all_of_ref_schema.rb +35 -0
  86. data/test/test_any_of_ref_schema.rb +35 -0
  87. data/test/test_bad_schema_ref.rb +39 -0
  88. data/test/test_common_test_suite.rb +66 -0
  89. data/test/test_custom_format.rb +116 -0
  90. data/test/test_definition.rb +15 -0
  91. data/test/test_extended_schema.rb +62 -0
  92. data/test/test_extends_and_additionalProperties.rb +52 -0
  93. data/test/test_files_v3.rb +43 -0
  94. data/test/test_fragment_resolution.rb +30 -0
  95. data/test/test_fragment_validation_with_ref.rb +34 -0
  96. data/test/test_full_validation.rb +208 -0
  97. data/test/test_helper.rb +47 -0
  98. data/test/test_initialize_data.rb +118 -0
  99. data/test/test_jsonschema_draft1.rb +171 -0
  100. data/test/test_jsonschema_draft2.rb +142 -0
  101. data/test/test_jsonschema_draft3.rb +502 -0
  102. data/test/test_jsonschema_draft4.rb +704 -0
  103. data/test/test_list_option.rb +21 -0
  104. data/test/test_merge_missing_values.rb +45 -0
  105. data/test/test_minitems.rb +16 -0
  106. data/test/test_one_of.rb +85 -0
  107. data/test/test_ruby_schema.rb +59 -0
  108. data/test/test_schema_loader.rb +74 -0
  109. data/test/test_schema_type_attribute.rb +20 -0
  110. data/test/test_schema_validation.rb +185 -0
  111. data/test/test_stringify.rb +48 -0
  112. data/test/test_uri_related.rb +67 -0
  113. data/test/test_validator.rb +53 -0
  114. metadata +284 -0
@@ -0,0 +1,47 @@
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
@@ -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
@@ -0,0 +1,171 @@
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 NumberValidation::MinMaxTests
19
+
20
+ include ObjectValidation::AdditionalPropertiesTests
21
+
22
+ include StringValidation::ValueTests
23
+ include StringValidation::FormatTests
24
+ include StringValidation::DateAndTimeFormatTests
25
+
26
+ include TypeValidation::SimpleTypeTests
27
+ include TypeValidation::AnyTypeTests
28
+ include TypeValidation::SchemaUnionTypeTests
29
+
30
+ def test_optional
31
+ # Set up the default datatype
32
+ schema = {
33
+ "properties" => {
34
+ "a" => {"type" => "string"}
35
+ }
36
+ }
37
+ data = {}
38
+
39
+ refute_valid schema, data
40
+ data['a'] = "Hello"
41
+ assert_valid schema, data
42
+
43
+ schema = {
44
+ "properties" => {
45
+ "a" => {"type" => "integer", "optional" => "true"}
46
+ }
47
+ }
48
+
49
+ data = {}
50
+ assert_valid schema, data
51
+ end
52
+
53
+ def test_enum
54
+ # Set up the default datatype
55
+ schema = {
56
+ "properties" => {
57
+ "a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}], "optional" => true}
58
+ }
59
+ }
60
+
61
+ data = {
62
+ "a" => nil
63
+ }
64
+
65
+ # Make sure all of the above are valid...
66
+ data["a"] = 1
67
+ assert_valid schema, data
68
+
69
+ data["a"] = 'boo'
70
+ assert_valid schema, data
71
+
72
+ data["a"] = [1,2,3]
73
+ assert_valid schema, data
74
+
75
+ data["a"] = {"a" => "b"}
76
+ assert_valid schema, data
77
+
78
+ # Test something that doesn't exist
79
+ data["a"] = 'taco'
80
+ refute_valid schema, data
81
+
82
+ # Try it without the key
83
+ data = {}
84
+ assert_valid schema, data
85
+ end
86
+
87
+
88
+ def test_max_decimal
89
+ # Set up the default datatype
90
+ schema = {
91
+ "properties" => {
92
+ "a" => {"maxDecimal" => 2}
93
+ }
94
+ }
95
+
96
+ data = {
97
+ "a" => nil
98
+ }
99
+
100
+ data["a"] = 3.35
101
+ assert_valid schema, data
102
+
103
+ data["a"] = 3.455
104
+ refute_valid schema, data
105
+
106
+
107
+ schema["properties"]["a"]["maxDecimal"] = 0
108
+
109
+ data["a"] = 4.0
110
+ refute_valid schema, data
111
+
112
+ data["a"] = 'boo'
113
+ assert_valid schema, data
114
+
115
+ data["a"] = 5
116
+ assert_valid schema, data
117
+ end
118
+
119
+
120
+
121
+ def test_disallow
122
+ # Set up the default datatype
123
+ schema = {
124
+ "properties" => {
125
+ "a" => {"disallow" => "integer"}
126
+ }
127
+ }
128
+
129
+ data = {
130
+ "a" => nil
131
+ }
132
+
133
+
134
+ data["a"] = 'string'
135
+ assert_valid schema, data
136
+
137
+ data["a"] = 5
138
+ refute_valid schema, data
139
+
140
+
141
+ schema["properties"]["a"]["disallow"] = ["integer","string"]
142
+ data["a"] = 'string'
143
+ refute_valid schema, data
144
+
145
+ data["a"] = 5
146
+ refute_valid schema, data
147
+
148
+ data["a"] = false
149
+ assert_valid schema, data
150
+
151
+ end
152
+
153
+ def test_format_datetime
154
+ schema = {
155
+ "type" => "object",
156
+ "properties" => { "a" => {"type" => "string", "format" => "date-time"}}
157
+ }
158
+
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"}
168
+ end
169
+
170
+ end
171
+
@@ -0,0 +1,142 @@
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 NumberValidation::MinMaxTests
24
+ include NumberValidation::MultipleOfTests
25
+
26
+ include ObjectValidation::AdditionalPropertiesTests
27
+
28
+ include StringValidation::ValueTests
29
+ include StringValidation::FormatTests
30
+ include StringValidation::DateAndTimeFormatTests
31
+
32
+ include TypeValidation::SimpleTypeTests
33
+ include TypeValidation::AnyTypeTests
34
+ include TypeValidation::SchemaUnionTypeTests
35
+
36
+ def test_optional
37
+ # Set up the default datatype
38
+ schema = {
39
+ "properties" => {
40
+ "a" => {"type" => "string"}
41
+ }
42
+ }
43
+ data = {}
44
+
45
+ refute_valid schema, data
46
+ data['a'] = "Hello"
47
+ assert_valid schema, data
48
+
49
+ schema = {
50
+ "properties" => {
51
+ "a" => {"type" => "integer", "optional" => "true"}
52
+ }
53
+ }
54
+
55
+ data = {}
56
+ assert_valid schema, data
57
+ end
58
+
59
+ def test_enum
60
+ # Set up the default datatype
61
+ schema = {
62
+ "properties" => {
63
+ "a" => {"enum" => [1,'boo',[1,2,3],{"a" => "b"}], "optional" => true}
64
+ }
65
+ }
66
+
67
+ data = {
68
+ "a" => nil
69
+ }
70
+
71
+ # Make sure all of the above are valid...
72
+ data["a"] = 1
73
+ assert_valid schema, data
74
+
75
+ data["a"] = 'boo'
76
+ assert_valid schema, data
77
+
78
+ data["a"] = [1,2,3]
79
+ assert_valid schema, data
80
+
81
+ data["a"] = {"a" => "b"}
82
+ assert_valid schema, data
83
+
84
+ # Test something that doesn't exist
85
+ data["a"] = 'taco'
86
+ refute_valid schema, data
87
+
88
+ # Try it without the key
89
+ data = {}
90
+ assert_valid schema, data
91
+ end
92
+
93
+ def test_disallow
94
+ # Set up the default datatype
95
+ schema = {
96
+ "properties" => {
97
+ "a" => {"disallow" => "integer"}
98
+ }
99
+ }
100
+
101
+ data = {
102
+ "a" => nil
103
+ }
104
+
105
+
106
+ data["a"] = 'string'
107
+ assert_valid schema, data
108
+
109
+ data["a"] = 5
110
+ refute_valid schema, data
111
+
112
+
113
+ schema["properties"]["a"]["disallow"] = ["integer","string"]
114
+ data["a"] = 'string'
115
+ refute_valid schema, data
116
+
117
+ data["a"] = 5
118
+ refute_valid schema, data
119
+
120
+ data["a"] = false
121
+ assert_valid schema, data
122
+ end
123
+
124
+ def test_format_datetime
125
+ schema = {
126
+ "type" => "object",
127
+ "properties" => { "a" => {"type" => "string", "format" => "date-time"}}
128
+ }
129
+
130
+ assert_valid schema, {"a" => "2010-01-01T12:00:00Z"}
131
+ refute_valid schema, {"a" => "2010-01-32T12:00:00Z"}
132
+ refute_valid schema, {"a" => "2010-13-01T12:00:00Z"}
133
+ refute_valid schema, {"a" => "2010-01-01T24:00:00Z"}
134
+ refute_valid schema, {"a" => "2010-01-01T12:60:00Z"}
135
+ refute_valid schema, {"a" => "2010-01-01T12:00:60Z"}
136
+ refute_valid schema, {"a" => "2010-01-01T12:00:00z"}
137
+ refute_valid schema, {"a" => "2010-01-0112:00:00Z"}
138
+ refute_valid schema, {"a" => "2010-01-01T12:00:00Z\nabc"}
139
+ end
140
+
141
+ end
142
+