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,34 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-03/schema#",
3
+ "type": "object",
4
+ "extends": "inner.schema.json",
5
+ "properties": {
6
+ "outerA": {
7
+ "description": "blah",
8
+ "required": false,
9
+ "additionalProperties": false,
10
+ "properties": {
11
+ "outerA1": {
12
+ "type":"boolean",
13
+ "required": false
14
+ }
15
+ }
16
+ },
17
+ "outerB": {
18
+ "required": false,
19
+ "type": "array",
20
+ "minItems": 1,
21
+ "maxItems": 50,
22
+ "items": {
23
+ "extends": "inner.schema.json",
24
+ "additionalProperties": false
25
+ }
26
+ },
27
+ "outerC": {
28
+ "description": "blah",
29
+ "type":"boolean",
30
+ "required": false
31
+ }
32
+ },
33
+ "additionalProperties": false
34
+ }
@@ -0,0 +1,34 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-03/schema#",
3
+ "type": "object",
4
+ "extends": {"$ref":"inner.schema.json#"},
5
+ "properties": {
6
+ "outerA": {
7
+ "description": "blah",
8
+ "required": false,
9
+ "additionalProperties": false,
10
+ "properties": {
11
+ "outerA1": {
12
+ "type":"boolean",
13
+ "required": false
14
+ }
15
+ }
16
+ },
17
+ "outerB": {
18
+ "required": false,
19
+ "type": "array",
20
+ "minItems": 1,
21
+ "maxItems": 50,
22
+ "items": {
23
+ "extends": {"$ref":"inner.schema.json#"},
24
+ "additionalProperties": false
25
+ }
26
+ },
27
+ "outerC": {
28
+ "description": "blah",
29
+ "type":"boolean",
30
+ "required": false
31
+ }
32
+ },
33
+ "additionalProperties": false
34
+ }
@@ -0,0 +1,33 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-03/schema#",
3
+ "type": "object",
4
+ "extends": "inner.schema.json",
5
+ "additionalProperties": {
6
+ "outerA": {
7
+ "description": "blah",
8
+ "required": false,
9
+ "additionalProperties": false,
10
+ "properties": {
11
+ "outerA1": {
12
+ "type":"boolean",
13
+ "required": false
14
+ }
15
+ }
16
+ },
17
+ "outerB": {
18
+ "required": false,
19
+ "type": "array",
20
+ "minItems": 1,
21
+ "maxItems": 50,
22
+ "items": {
23
+ "extends": "inner.schema.json",
24
+ "additionalProperties": false
25
+ }
26
+ },
27
+ "outerC": {
28
+ "description": "blah",
29
+ "type":"boolean",
30
+ "required": false
31
+ }
32
+ }
33
+ }
@@ -0,0 +1,33 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-03/schema#",
3
+ "type": "object",
4
+ "extends": {"$ref":"inner.schema.json#"},
5
+ "additionalProperties": {
6
+ "outerA": {
7
+ "description": "blah",
8
+ "required": false,
9
+ "additionalProperties": false,
10
+ "properties": {
11
+ "outerA1": {
12
+ "type":"boolean",
13
+ "required": false
14
+ }
15
+ }
16
+ },
17
+ "outerB": {
18
+ "required": false,
19
+ "type": "array",
20
+ "minItems": 1,
21
+ "maxItems": 50,
22
+ "items": {
23
+ "extends": {"$ref":"inner.schema.json#"},
24
+ "additionalProperties": false
25
+ }
26
+ },
27
+ "outerC": {
28
+ "description": "blah",
29
+ "type":"boolean",
30
+ "required": false
31
+ }
32
+ }
33
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "$schema" : "http://json-schema.org/draft-03/schema#",
3
+ "type" : "object",
4
+ "properties" : {
5
+ "a" : {
6
+ "type" : "integer",
7
+ "required" : true
8
+ }
9
+ }
10
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "$schema" : "http://json-schema.org/draft-03/schema#",
3
+ "type" : "object",
4
+ "properties" : {
5
+ "b" : {
6
+ "required" : true,
7
+ "$ref" : "good_schema_1.json"
8
+ }
9
+ }
10
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "$schema" : "http://json-schema.org/draft-03/schema#",
3
+ "type" : "object",
4
+ "extends": {"$ref": "good_schema_1.json"},
5
+ "properties" : {
6
+ "c" : {
7
+ "required" : false
8
+ }
9
+ }
10
+ }
@@ -0,0 +1,13 @@
1
+ {
2
+ "$schema" : "http://json-schema.org/draft-03/schema#",
3
+ "type" : "object",
4
+ "extends": [
5
+ {"$ref": "good_schema_1.json"},
6
+ {"$ref": "good_schema_2.json"}
7
+ ],
8
+ "properties" : {
9
+ "c" : {
10
+ "required" : false
11
+ }
12
+ }
13
+ }
@@ -0,0 +1,21 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-03/schema#",
3
+ "type": "object",
4
+ "properties": {
5
+ "innerA": {
6
+ "description": "blah",
7
+ "type":"boolean",
8
+ "required": false
9
+ },
10
+ "innerB": {
11
+ "description": "blah",
12
+ "type":"boolean",
13
+ "required": false
14
+ },
15
+ "innerC": {
16
+ "description": "blah",
17
+ "required": false,
18
+ "type": "boolean"
19
+ }
20
+ }
21
+ }
@@ -0,0 +1,16 @@
1
+ { "$schema": "http://json-schema.org/draft-04/schema#"
2
+ , "type": "object"
3
+ , "properties":
4
+ { "links" :
5
+ { "type" : "array"
6
+ , "items" :
7
+ { "type" : "object"
8
+ , "oneOf" :
9
+ [ { "$ref" : "self_link_schema.json"}
10
+ , { "$ref" : "up_link_schema.json" }
11
+ ]
12
+ }
13
+ }
14
+ }
15
+ }
16
+
@@ -0,0 +1,11 @@
1
+ {
2
+ "$schema" : "http://json-schema.org/draft-04/schema#",
3
+ "type" : "object",
4
+ "required" : ["first"],
5
+ "properties": {
6
+ "first": {
7
+ "type": "string",
8
+ "enum": ["john"]
9
+ }
10
+ }
11
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-04/schema#",
3
+ "properties": {
4
+ "a": {
5
+ "$ref": "definition_schema.json#/definitions/foo"
6
+ }
7
+ }
8
+ }
@@ -0,0 +1,17 @@
1
+ { "$schema": "http://json-schema.org/draft-04/schema#"
2
+ , "type": "object"
3
+ , "properties" :
4
+ { "rel" :
5
+ { "type" : "array"
6
+ , "items" :
7
+ [ { "type" : "string"
8
+ , "pattern" : "self"
9
+ }
10
+ ]
11
+ }
12
+ , "href" :
13
+ { "type" : "string"
14
+ }
15
+ }
16
+ }
17
+
@@ -0,0 +1,17 @@
1
+ { "$schema": "http://json-schema.org/draft-04/schema#"
2
+ , "type": "object"
3
+ , "properties" :
4
+ { "rel" :
5
+ { "type" : "array"
6
+ , "items" :
7
+ [ { "type" : "string"
8
+ , "pattern" : "up"
9
+ }
10
+ ]
11
+ }
12
+ , "href" :
13
+ { "type" : "string"
14
+ }
15
+ }
16
+ }
17
+
@@ -0,0 +1,35 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ class AllOfRefSchemaTest < Minitest::Test
4
+ def schema
5
+ schema_fixture_path('all_of_ref_schema.json')
6
+ end
7
+
8
+ def data
9
+ data_fixture_path('all_of_ref_data.json')
10
+ end
11
+
12
+ def test_all_of_ref_schema_fails
13
+ refute_valid schema, data
14
+ end
15
+
16
+ def test_all_of_ref_schema_succeeds
17
+ assert_valid schema, %({"name": 42})
18
+ end
19
+
20
+ def test_all_of_ref_subschema_errors
21
+ errors = JSON::Validator.fully_validate(schema, data, :errors_as_objects => true)
22
+ nested_errors = errors[0][:errors]
23
+ assert_equal([:allof_0], nested_errors.keys, 'should have nested errors for each allOf subschema')
24
+ assert_match(/the property '#\/name' of type String did not match the following type: integer/i, nested_errors[:allof_0][0][:message])
25
+ end
26
+
27
+ def test_all_of_ref_message
28
+ errors = JSON::Validator.fully_validate(schema, data)
29
+ expected_message = """The property '#/' of type Hash did not match all of the required schemas. The schema specific errors were:
30
+
31
+ - allOf #0:
32
+ - The property '#/name' of type String did not match the following type: integer"""
33
+ assert_equal(expected_message, errors[0])
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+
3
+ class AnyOfRefSchemaTest < Minitest::Test
4
+ def schema
5
+ schema_fixture_path('any_of_ref_schema.json')
6
+ end
7
+
8
+ def test_any_of_ref_schema
9
+ assert_valid schema, data_fixture_path('any_of_ref_data.json')
10
+ end
11
+
12
+ def test_any_of_ref_subschema_errors
13
+ data = %({"names": ["jack"]})
14
+ errors = JSON::Validator.fully_validate(schema, data, :errors_as_objects => true)
15
+ nested_errors = errors[0][:errors]
16
+ assert_equal([:anyof_0, :anyof_1, :anyof_2], nested_errors.keys, 'should have nested errors for each anyOf subschema')
17
+ assert_match(/the property '#\/names\/0' value "jack" did not match the regex 'john'/i, nested_errors[:anyof_0][0][:message])
18
+ assert_match(/the property '#\/names\/0' value "jack" did not match the regex 'jane'/i, nested_errors[:anyof_1][0][:message])
19
+ assert_match(/the property '#\/names\/0' value "jack" did not match the regex 'jimmy'/i, nested_errors[:anyof_2][0][:message])
20
+ end
21
+
22
+ def test_any_of_ref_message
23
+ data = %({"names": ["jack"]})
24
+ errors = JSON::Validator.fully_validate(schema, data)
25
+ expected_message = """The property '#/names/0' of type String did not match one or more of the required schemas. The schema specific errors were:
26
+
27
+ - anyOf #0:
28
+ - The property '#/names/0' value \"jack\" did not match the regex 'john'
29
+ - anyOf #1:
30
+ - The property '#/names/0' value \"jack\" did not match the regex 'jane'
31
+ - anyOf #2:
32
+ - The property '#/names/0' value \"jack\" did not match the regex 'jimmy'"""
33
+ assert_equal(expected_message, errors[0])
34
+ end
35
+ end
@@ -0,0 +1,39 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+ require 'socket'
3
+
4
+
5
+ class BadSchemaRefTest < Minitest::Test
6
+ def setup
7
+ WebMock.allow_net_connect!
8
+ end
9
+
10
+ def teardown
11
+ WebMock.disable_net_connect!
12
+ end
13
+
14
+ def test_bad_uri_ref
15
+ schema = {
16
+ "$schema" => "http://json-schema.org/draft-04/schema#",
17
+ "type" => "array",
18
+ "items" => { "$ref" => "../google.json"}
19
+ }
20
+
21
+ data = [1,2,3]
22
+ assert_raises(Errno::ENOENT) do
23
+ JSON::Validator.validate(schema,data)
24
+ end
25
+ end
26
+
27
+ def test_bad_host_ref
28
+ schema = {
29
+ "$schema" => "http://json-schema.org/draft-04/schema#",
30
+ "type" => "array",
31
+ "items" => { "$ref" => "http://ppcheesecheseunicornnuuuurrrrr.example.invalid/json.schema"}
32
+ }
33
+
34
+ data = [1,2,3]
35
+ assert_raises(SocketError, OpenURI::HTTPError) do
36
+ JSON::Validator.validate(schema,data)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,66 @@
1
+ require File.expand_path('../test_helper', __FILE__)
2
+ require 'json'
3
+
4
+ class CommonTestSuiteTest < Minitest::Test
5
+ TEST_DIR = File.expand_path('../test-suite/tests', __FILE__)
6
+
7
+ # These are test files which we know fail spectacularly, either because we
8
+ # don't support that functionality or because they require external
9
+ # dependencies. To allow finer-grained control over which tests to run,
10
+ # you can replace `:all` with an array containing the names of individual
11
+ # tests to skip.
12
+ IGNORED_TESTS = Hash.new { |h,k| h[k] = [] }.merge({
13
+ "draft3/optional/jsregex.json" => :all,
14
+ "draft3/optional/format.json" => [
15
+ "validation of regular expressions",
16
+ "validation of e-mail addresses",
17
+ "validation of URIs",
18
+ "validation of host names",
19
+ "validation of CSS colors"
20
+ ],
21
+ "draft4/optional/format.json" => [
22
+ "validation of URIs",
23
+ "validation of e-mail addresses",
24
+ "validation of host names"
25
+ ]
26
+ })
27
+
28
+ def setup
29
+ Dir["#{TEST_DIR}/../remotes/**/*.json"].each do |path|
30
+ schema = path.sub(%r{^.*/remotes/}, '')
31
+ stub_request(:get, "http://localhost:1234/#{schema}").
32
+ to_return(:body => File.read(path), :status => 200)
33
+ end
34
+ end
35
+
36
+ Dir["#{TEST_DIR}/*"].each do |suite|
37
+ version = File.basename(suite).to_sym
38
+ Dir["#{suite}/**/*.json"].each do |tfile|
39
+ test_list = JSON.parse(File.read(tfile))
40
+ rel_file = tfile[TEST_DIR.length+1..-1]
41
+
42
+ test_list.each do |test|
43
+ schema = test["schema"]
44
+ base_description = test["description"]
45
+
46
+ test["tests"].each do |t|
47
+ next if IGNORED_TESTS[rel_file] == :all
48
+ next if IGNORED_TESTS[rel_file].any? { |ignored|
49
+ base_description == ignored || "#{base_description}/#{t['description']}" == ignored
50
+ }
51
+
52
+ err_id = "#{rel_file}: #{base_description}/#{t['description']}"
53
+ define_method("test_#{err_id}") do
54
+ errors = JSON::Validator.fully_validate(schema,
55
+ t["data"],
56
+ :parse_data => false,
57
+ :validate_schema => true,
58
+ :version => version
59
+ )
60
+ assert_equal t["valid"], errors.empty?, "Common test suite case failed: #{err_id}"
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end