json-schema-pvdgm 2.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.md +19 -0
  3. data/README.textile +354 -0
  4. data/lib/json-schema.rb +25 -0
  5. data/lib/json-schema/attributes/additionalitems.rb +23 -0
  6. data/lib/json-schema/attributes/additionalproperties.rb +67 -0
  7. data/lib/json-schema/attributes/allof.rb +37 -0
  8. data/lib/json-schema/attributes/anyof.rb +41 -0
  9. data/lib/json-schema/attributes/dependencies.rb +30 -0
  10. data/lib/json-schema/attributes/dependencies_v4.rb +20 -0
  11. data/lib/json-schema/attributes/disallow.rb +11 -0
  12. data/lib/json-schema/attributes/divisibleby.rb +16 -0
  13. data/lib/json-schema/attributes/enum.rb +24 -0
  14. data/lib/json-schema/attributes/extends.rb +49 -0
  15. data/lib/json-schema/attributes/format.rb +123 -0
  16. data/lib/json-schema/attributes/items.rb +25 -0
  17. data/lib/json-schema/attributes/maxdecimal.rb +15 -0
  18. data/lib/json-schema/attributes/maximum.rb +15 -0
  19. data/lib/json-schema/attributes/maximum_inclusive.rb +15 -0
  20. data/lib/json-schema/attributes/maxitems.rb +12 -0
  21. data/lib/json-schema/attributes/maxlength.rb +14 -0
  22. data/lib/json-schema/attributes/maxproperties.rb +12 -0
  23. data/lib/json-schema/attributes/minimum.rb +15 -0
  24. data/lib/json-schema/attributes/minimum_inclusive.rb +15 -0
  25. data/lib/json-schema/attributes/minitems.rb +12 -0
  26. data/lib/json-schema/attributes/minlength.rb +14 -0
  27. data/lib/json-schema/attributes/minproperties.rb +12 -0
  28. data/lib/json-schema/attributes/multipleof.rb +16 -0
  29. data/lib/json-schema/attributes/not.rb +28 -0
  30. data/lib/json-schema/attributes/oneof.rb +32 -0
  31. data/lib/json-schema/attributes/pattern.rb +15 -0
  32. data/lib/json-schema/attributes/patternproperties.rb +23 -0
  33. data/lib/json-schema/attributes/properties.rb +58 -0
  34. data/lib/json-schema/attributes/properties_optional.rb +23 -0
  35. data/lib/json-schema/attributes/properties_v4.rb +57 -0
  36. data/lib/json-schema/attributes/ref.rb +70 -0
  37. data/lib/json-schema/attributes/required.rb +23 -0
  38. data/lib/json-schema/attributes/type.rb +102 -0
  39. data/lib/json-schema/attributes/type_v4.rb +54 -0
  40. data/lib/json-schema/attributes/uniqueitems.rb +16 -0
  41. data/lib/json-schema/model_validator.rb +85 -0
  42. data/lib/json-schema/schema.rb +73 -0
  43. data/lib/json-schema/uri/file.rb +36 -0
  44. data/lib/json-schema/uri/uuid.rb +285 -0
  45. data/lib/json-schema/util/array_set.rb +14 -0
  46. data/lib/json-schema/util/hash.rb +8 -0
  47. data/lib/json-schema/validator.rb +672 -0
  48. data/lib/json-schema/validators/draft1.rb +32 -0
  49. data/lib/json-schema/validators/draft2.rb +33 -0
  50. data/lib/json-schema/validators/draft3.rb +38 -0
  51. data/lib/json-schema/validators/draft4.rb +45 -0
  52. data/resources/draft-01.json +155 -0
  53. data/resources/draft-02.json +166 -0
  54. data/resources/draft-03.json +174 -0
  55. data/resources/draft-04.json +150 -0
  56. data/test/data/all_of_ref_data.json +3 -0
  57. data/test/data/any_of_ref_data.json +7 -0
  58. data/test/data/bad_data_1.json +3 -0
  59. data/test/data/good_data_1.json +3 -0
  60. data/test/data/one_of_ref_links_data.json +5 -0
  61. data/test/schemas/all_of_ref_base_schema.json +6 -0
  62. data/test/schemas/all_of_ref_schema.json +7 -0
  63. data/test/schemas/any_of_ref_jane_schema.json +4 -0
  64. data/test/schemas/any_of_ref_jimmy_schema.json +4 -0
  65. data/test/schemas/any_of_ref_john_schema.json +4 -0
  66. data/test/schemas/any_of_ref_schema.json +15 -0
  67. data/test/schemas/extends_and_additionalProperties-1-filename.schema.json +34 -0
  68. data/test/schemas/extends_and_additionalProperties-1-ref.schema.json +34 -0
  69. data/test/schemas/extends_and_additionalProperties-2-filename.schema.json +33 -0
  70. data/test/schemas/extends_and_additionalProperties-2-ref.schema.json +33 -0
  71. data/test/schemas/good_schema_1.json +10 -0
  72. data/test/schemas/good_schema_2.json +10 -0
  73. data/test/schemas/good_schema_extends1.json +10 -0
  74. data/test/schemas/good_schema_extends2.json +13 -0
  75. data/test/schemas/inner.schema.json +21 -0
  76. data/test/schemas/one_of_ref_links_schema.json +16 -0
  77. data/test/schemas/self_link_schema.json +17 -0
  78. data/test/schemas/up_link_schema.json +17 -0
  79. data/test/test_all_of_ref_schema.rb +11 -0
  80. data/test/test_any_of_ref_schema.rb +11 -0
  81. data/test/test_bad_schema_ref.rb +33 -0
  82. data/test/test_extended_schema.rb +68 -0
  83. data/test/test_extends_and_additionalProperties.rb +50 -0
  84. data/test/test_files_v3.rb +52 -0
  85. data/test/test_fragment_resolution.rb +31 -0
  86. data/test/test_full_validation.rb +209 -0
  87. data/test/test_jsonschema_draft1.rb +701 -0
  88. data/test/test_jsonschema_draft2.rb +773 -0
  89. data/test/test_jsonschema_draft3.rb +1236 -0
  90. data/test/test_jsonschema_draft4.rb +1356 -0
  91. data/test/test_model_validator.rb +52 -0
  92. data/test/test_one_of.rb +42 -0
  93. data/test/test_ruby_schema.rb +38 -0
  94. data/test/test_schema_type_attribute.rb +21 -0
  95. data/test/test_schema_validation.rb +85 -0
  96. metadata +180 -0
@@ -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,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,11 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class AnyOfRefSchemaTest < Test::Unit::TestCase
5
+ def test_all_of_ref_schema
6
+ schema = File.join(File.dirname(__FILE__),"schemas/all_of_ref_schema.json")
7
+ data = File.join(File.dirname(__FILE__),"data/all_of_ref_data.json")
8
+ errors = JSON::Validator.fully_validate(schema,data, :errors_as_objects => true)
9
+ assert(!errors.empty?, "should have failed to validate")
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class AnyOfRefSchemaTest < Test::Unit::TestCase
5
+ def test_any_of_ref_schema
6
+ schema = File.join(File.dirname(__FILE__),"schemas/any_of_ref_schema.json")
7
+ data = File.join(File.dirname(__FILE__),"data/any_of_ref_data.json")
8
+ errors = JSON::Validator.fully_validate(schema,data, :errors_as_objects => true)
9
+ assert(errors.empty?, errors.map{|e| e[:message] }.join("\n"))
10
+ end
11
+ end
@@ -0,0 +1,33 @@
1
+ require 'test/unit'
2
+ require 'socket'
3
+ require File.dirname(__FILE__) + '/../lib/json-schema'
4
+
5
+ class BadSchemaRefTest < Test::Unit::TestCase
6
+
7
+ def test_bad_uri_ref
8
+ schema = {
9
+ "$schema" => "http://json-schema.org/draft-04/schema#",
10
+ "type" => "array",
11
+ "items" => { "$ref" => "../google.json"}
12
+ }
13
+
14
+ data = [1,2,3]
15
+ assert_raise(URI::BadURIError) do
16
+ JSON::Validator.validate(schema,data)
17
+ end
18
+ end
19
+
20
+ def test_bad_host_ref
21
+ schema = {
22
+ "$schema" => "http://json-schema.org/draft-04/schema#",
23
+ "type" => "array",
24
+ "items" => { "$ref" => "http://ppcheesecheseunicornnuuuurrrrr.com/json.schema"}
25
+ }
26
+
27
+ data = [1,2,3]
28
+ assert_raise(SocketError) do
29
+ JSON::Validator.validate(schema,data)
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,68 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class BitwiseAndAttribute < JSON::Schema::Attribute
5
+ def self.validate(current_schema, data, fragments, processor, validator, options = {})
6
+ if data.is_a?(Integer) && data & current_schema.schema['bitwise-and'].to_i == 0
7
+ message = "The property '#{build_fragment(fragments)}' did not evaluate to true when bitwise-AND'd with #{current_schema.schema['bitwise-or']}"
8
+ raise JSON::Schema::ValidationError.new(message, fragments, self, current_schema)
9
+ end
10
+ end
11
+ end
12
+
13
+ class ExtendedSchema < JSON::Schema::Validator
14
+ def initialize
15
+ super
16
+ extend_schema_definition("http://json-schema.org/draft-03/schema#")
17
+ @attributes["bitwise-and"] = BitwiseAndAttribute
18
+ @uri = URI.parse("http://test.com/test.json")
19
+ end
20
+
21
+ JSON::Validator.register_validator(self.new)
22
+ end
23
+
24
+ class JSONSchemaTestExtendedSchema < Test::Unit::TestCase
25
+ def test_schema_from_file
26
+ schema = {
27
+ "$schema" => "http://json-schema.org/draft-03/schema#",
28
+ "properties" => {
29
+ "a" => {
30
+ "bitwise-and" => 1
31
+ },
32
+ "b" => {
33
+ "type" => "string"
34
+ }
35
+ }
36
+ }
37
+
38
+ data = {"a" => 0, "b" => "taco"}
39
+ assert(JSON::Validator.validate(schema,data))
40
+ data = {"a" => 1, "b" => "taco"}
41
+ assert(JSON::Validator.validate(schema,data))
42
+ data = {"a" => 1, "b" => 5}
43
+ assert(!JSON::Validator.validate(schema,data))
44
+
45
+ schema = {
46
+ "$schema" => "http://test.com/test.json",
47
+ "properties" => {
48
+ "a" => {
49
+ "bitwise-and" => 1
50
+ },
51
+ "b" => {
52
+ "type" => "string"
53
+ }
54
+ }
55
+ }
56
+
57
+ data = {
58
+ "a" => 0
59
+ }
60
+
61
+ data = {"a" => 1, "b" => "taco"}
62
+ assert(JSON::Validator.validate(schema,data))
63
+ data = {"a" => 0, "b" => "taco"}
64
+ assert(!JSON::Validator.validate(schema,data))
65
+ data = {"a" => 1, "b" => 5}
66
+ assert(!JSON::Validator.validate(schema,data))
67
+ end
68
+ end
@@ -0,0 +1,50 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class ExtendsNestedTest < Test::Unit::TestCase
5
+
6
+ def assert_validity(valid, schema_name, data, msg=nil)
7
+ file = File.expand_path("../schemas/#{schema_name}.schema.json",__FILE__)
8
+ errors = JSON::Validator.fully_validate file, data
9
+ msg.sub! /\.$/, '' if msg
10
+ send (valid ? :assert_equal : :assert_not_equal), [], errors, \
11
+ "Schema should be #{valid ? :valid : :invalid}#{msg ? ".\n[#{schema_name}] #{msg}" : ''}"
12
+ end
13
+
14
+ def assert_valid(schema_name, data, msg=nil) assert_validity true, schema_name, data, msg end
15
+ def refute_valid(schema_name, data, msg=nil) assert_validity false, schema_name, data, msg end
16
+
17
+ %w[
18
+ extends_and_additionalProperties-1-filename extends_and_additionalProperties-1-ref
19
+ extends_and_additionalProperties-2-filename extends_and_additionalProperties-2-ref
20
+ ].each do |schema_name|
21
+ test_prefix= 'test_' + schema_name.gsub('-','_')
22
+ class_eval <<-EOB
23
+
24
+ def #{test_prefix}_valid_outer
25
+ assert_valid '#{schema_name}', {"outerC"=>true}, "Outer defn is broken, maybe the outer extends overrode it?"
26
+ end
27
+
28
+ def #{test_prefix}_valid_outer_extended
29
+ assert_valid '#{schema_name}', {"innerA"=>true}, "Extends at the root level isn't working."
30
+ end
31
+
32
+ def #{test_prefix}_valid_inner
33
+ assert_valid '#{schema_name}', {"outerB"=>[{"innerA"=>true}]}, "Extends isn't working in the array element defn."
34
+ end
35
+
36
+ def #{test_prefix}_invalid_inner
37
+ refute_valid '#{schema_name}', {"outerB"=>[{"whaaaaat"=>true}]}, "Array element defn allowing anything when it should only allow what's in inner.schema"
38
+ end
39
+ EOB
40
+
41
+ if schema_name['extends_and_additionalProperties-1']
42
+ class_eval <<-EOB
43
+ def #{test_prefix}_invalid_outer
44
+ refute_valid '#{schema_name}', {"whaaaaat"=>true}, "Outer defn allowing anything when it shouldn't."
45
+ end
46
+ EOB
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,52 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class JSONSchemaTest < Test::Unit::TestCase
5
+
6
+ #
7
+ # These tests are ONLY run if there is an appropriate JSON backend parser available
8
+ #
9
+
10
+ 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))
15
+ end
16
+
17
+ def test_data_from_file
18
+ 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")))
21
+ end
22
+
23
+ def test_data_from_json
24
+ if JSON::Validator.json_backend != nil
25
+ 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"})))
28
+ end
29
+ end
30
+
31
+ 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")))
34
+ end
35
+
36
+ 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))
42
+ end
43
+
44
+ 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))
50
+ end
51
+
52
+ end
@@ -0,0 +1,31 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/../lib/json-schema'
3
+
4
+ class FragmentResolution < Test::Unit::TestCase
5
+ def test_fragment_resolution
6
+ schema = {
7
+ "$schema" => "http://json-schema.org/draft-04/schema#",
8
+ "required" => ["a"],
9
+ "properties" => {
10
+ "a" => {
11
+ "type" => "object",
12
+ "properties" => {
13
+ "b" => {"type" => "integer" }
14
+ }
15
+ }
16
+ }
17
+ }
18
+
19
+ data = {"b" => 5}
20
+ assert(!JSON::Validator.validate(schema,data))
21
+ assert(JSON::Validator.validate(schema,data,:fragment => "#/properties/a"))
22
+
23
+ assert_raise JSON::Schema::SchemaError do
24
+ JSON::Validator.validate!(schema,data,:fragment => "/properties/a")
25
+ end
26
+
27
+ assert_raise JSON::Schema::SchemaError do
28
+ JSON::Validator.validate!(schema,data,:fragment => "#/properties/b")
29
+ end
30
+ end
31
+ end