json-schema 2.4.1 → 2.5.0

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 (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,27 +0,0 @@
1
- require 'json-schema/attribute'
2
-
3
- module JSON
4
- class Schema
5
- class DependenciesV4Attribute < Attribute
6
- def self.validate(current_schema, data, fragments, processor, validator, options = {})
7
- if data.is_a?(Hash)
8
- current_schema.schema['dependencies'].each do |property,dependency_value|
9
- if data.has_key?(property.to_s)
10
- if dependency_value.is_a?(Array)
11
- dependency_value.each do |value|
12
- if !data.has_key?(value.to_s)
13
- message = "The property '#{build_fragment(fragments)}' has a property '#{property}' that depends on a missing property '#{value}'"
14
- validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
15
- end
16
- end
17
- elsif dependency_value.is_a?(Hash)
18
- schema = JSON::Schema.new(dependency_value,current_schema.uri,validator)
19
- schema.validate(data, fragments, processor, options)
20
- end
21
- end
22
- end
23
- end
24
- end
25
- end
26
- end
27
- end
@@ -1,20 +0,0 @@
1
- require 'json-schema/attribute'
2
- require 'ipaddr'
3
-
4
- module JSON
5
- class Schema
6
- class IP4Format < FormatAttribute
7
- def self.validate(current_schema, data, fragments, processor, validator, options = {})
8
- if data.is_a?(String)
9
- error_message = "The property '#{build_fragment(fragments)}' must be a valid IPv4 address"
10
- begin
11
- ip = IPAddr.new data
12
- rescue ArgumentError => e
13
- raise e unless e.message == 'invalid address'
14
- end
15
- validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) unless ip && ip.ipv4?
16
- end
17
- end
18
- end
19
- end
20
- end
@@ -1,20 +0,0 @@
1
- require 'json-schema/attribute'
2
- require 'ipaddr'
3
-
4
- module JSON
5
- class Schema
6
- class IP6Format < FormatAttribute
7
- def self.validate(current_schema, data, fragments, processor, validator, options = {})
8
- if data.is_a?(String)
9
- error_message = "The property '#{build_fragment(fragments)}' must be a valid IPv6 address"
10
- begin
11
- ip = IPAddr.new data
12
- rescue ArgumentError => e
13
- raise e unless e.message == 'invalid address'
14
- end
15
- validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) unless ip && ip.ipv6?
16
- end
17
- end
18
- end
19
- end
20
- end
@@ -1,17 +0,0 @@
1
- require 'json-schema/attribute'
2
-
3
- module JSON
4
- class Schema
5
- class MaximumAttribute < Attribute
6
- def self.validate(current_schema, data, fragments, processor, validator, options = {})
7
- if data.is_a?(Numeric)
8
- if (current_schema.schema['exclusiveMaximum'] ? data >= current_schema.schema['maximum'] : data > current_schema.schema['maximum'])
9
- message = "The property '#{build_fragment(fragments)}' did not have a maximum value of #{current_schema.schema['maximum']}, "
10
- message += current_schema.schema['exclusiveMaximum'] ? 'exclusively' : 'inclusively'
11
- validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
12
- end
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,17 +0,0 @@
1
- require 'json-schema/attribute'
2
-
3
- module JSON
4
- class Schema
5
- class MaximumInclusiveAttribute < Attribute
6
- def self.validate(current_schema, data, fragments, processor, validator, options = {})
7
- if data.is_a?(Numeric)
8
- if (current_schema.schema['maximumCanEqual'] == false ? data >= current_schema.schema['maximum'] : data > current_schema.schema['maximum'])
9
- message = "The property '#{build_fragment(fragments)}' did not have a maximum value of #{current_schema.schema['maximum']}, "
10
- message += current_schema.schema['exclusiveMaximum'] ? 'exclusively' : 'inclusively'
11
- validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
12
- end
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,14 +0,0 @@
1
- require 'json-schema/attribute'
2
-
3
- module JSON
4
- class Schema
5
- class MaxItemsAttribute < Attribute
6
- def self.validate(current_schema, data, fragments, processor, validator, options = {})
7
- if data.is_a?(Array) && (data.compact.size > current_schema.schema['maxItems'])
8
- message = "The property '#{build_fragment(fragments)}' had more items than the allowed #{current_schema.schema['maxItems']}"
9
- validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,16 +0,0 @@
1
- require 'json-schema/attribute'
2
-
3
- module JSON
4
- class Schema
5
- class MaxLengthAttribute < Attribute
6
- def self.validate(current_schema, data, fragments, processor, validator, options = {})
7
- if data.is_a?(String)
8
- if data.length > current_schema.schema['maxLength']
9
- message = "The property '#{build_fragment(fragments)}' was not of a maximum string length of #{current_schema.schema['maxLength']}"
10
- validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
11
- end
12
- end
13
- end
14
- end
15
- end
16
- end
@@ -1,14 +0,0 @@
1
- require 'json-schema/attribute'
2
-
3
- module JSON
4
- class Schema
5
- class MaxPropertiesAttribute < Attribute
6
- def self.validate(current_schema, data, fragments, processor, validator, options = {})
7
- if data.is_a?(Hash) && (data.size > current_schema.schema['maxProperties'])
8
- message = "The property '#{build_fragment(fragments)}' did not contain a minimum number of properties #{current_schema.schema['maxProperties']}"
9
- validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,17 +0,0 @@
1
- require 'json-schema/attribute'
2
-
3
- module JSON
4
- class Schema
5
- class MinimumAttribute < Attribute
6
- def self.validate(current_schema, data, fragments, processor, validator, options = {})
7
- if data.is_a?(Numeric)
8
- if (current_schema.schema['exclusiveMinimum'] ? data <= current_schema.schema['minimum'] : data < current_schema.schema['minimum'])
9
- message = "The property '#{build_fragment(fragments)}' did not have a minimum value of #{current_schema.schema['minimum']}, "
10
- message += current_schema.schema['exclusiveMinimum'] ? 'exclusively' : 'inclusively'
11
- validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
12
- end
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,17 +0,0 @@
1
- require 'json-schema/attribute'
2
-
3
- module JSON
4
- class Schema
5
- class MinimumInclusiveAttribute < Attribute
6
- def self.validate(current_schema, data, fragments, processor, validator, options = {})
7
- if data.is_a?(Numeric)
8
- if (current_schema.schema['minimumCanEqual'] == false ? data <= current_schema.schema['minimum'] : data < current_schema.schema['minimum'])
9
- message = "The property '#{build_fragment(fragments)}' did not have a minimum value of #{current_schema.schema['minimum']}, "
10
- message += current_schema.schema['exclusiveMinimum'] ? 'exclusively' : 'inclusively'
11
- validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
12
- end
13
- end
14
- end
15
- end
16
- end
17
- end
@@ -1,14 +0,0 @@
1
- require 'json-schema/attribute'
2
-
3
- module JSON
4
- class Schema
5
- class MinItemsAttribute < Attribute
6
- def self.validate(current_schema, data, fragments, processor, validator, options = {})
7
- if data.is_a?(Array) && (data.size < current_schema.schema['minItems'])
8
- message = "The property '#{build_fragment(fragments)}' did not contain a minimum number of items #{current_schema.schema['minItems']}"
9
- validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,16 +0,0 @@
1
- require 'json-schema/attribute'
2
-
3
- module JSON
4
- class Schema
5
- class MinLengthAttribute < Attribute
6
- def self.validate(current_schema, data, fragments, processor, validator, options = {})
7
- if data.is_a?(String)
8
- if data.length < current_schema.schema['minLength']
9
- message = "The property '#{build_fragment(fragments)}' was not of a minimum string length of #{current_schema.schema['minLength']}"
10
- validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
11
- end
12
- end
13
- end
14
- end
15
- end
16
- end
@@ -1,14 +0,0 @@
1
- require 'json-schema/attribute'
2
-
3
- module JSON
4
- class Schema
5
- class MinPropertiesAttribute < Attribute
6
- def self.validate(current_schema, data, fragments, processor, validator, options = {})
7
- if data.is_a?(Hash) && (data.size < current_schema.schema['minProperties'])
8
- message = "The property '#{build_fragment(fragments)}' did not contain a minimum number of properties #{current_schema.schema['minProperties']}"
9
- validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
10
- end
11
- end
12
- end
13
- end
14
- end
@@ -1,58 +0,0 @@
1
- require 'json-schema/attribute'
2
-
3
- module JSON
4
- class Schema
5
- class PropertiesV4Attribute < Attribute
6
- def self.validate(current_schema, data, fragments, processor, validator, options = {})
7
- if data.is_a?(Hash)
8
- current_schema.schema['properties'].each do |property,property_schema|
9
- if !data.has_key?(property.to_s) &&
10
- property_schema['default'] &&
11
- !property_schema['readonly'] &&
12
- options[:insert_defaults]
13
- default = property_schema['default']
14
- data[property.to_s] = (default.is_a?(Hash) ? default.clone : default)
15
- end
16
-
17
- if options[:strict] == true && !data.has_key?(property.to_s)
18
- message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
19
- validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
20
- end
21
-
22
- if data.has_key?(property.to_s)
23
- schema = JSON::Schema.new(property_schema,current_schema.uri,validator)
24
- fragments << property.to_s
25
- schema.validate(data[property.to_s],fragments,processor,options)
26
- fragments.pop
27
- end
28
- end
29
- end
30
-
31
- # When strict is true, ensure no undefined properties exist in the data
32
- if (options[:strict] == true && !current_schema.schema.has_key?('additionalProperties'))
33
- diff = data.select do |k,v|
34
- if current_schema.schema.has_key?('patternProperties')
35
- match = false
36
- current_schema.schema['patternProperties'].each do |property,property_schema|
37
- r = Regexp.new(property)
38
- if r.match(k)
39
- match = true
40
- break
41
- end
42
- end
43
-
44
- !current_schema.schema['properties'].has_key?(k.to_s) && !match
45
- else
46
- !current_schema.schema['properties'].has_key?(k.to_s)
47
- end
48
- end
49
-
50
- if diff.size > 0
51
- message = "The property '#{build_fragment(fragments)}' contained undefined properties: '#{diff.keys.join(", ")}'"
52
- validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
53
- end
54
- end
55
- end
56
- end
57
- end
58
- end
@@ -1,36 +0,0 @@
1
- require 'rbconfig'
2
- require 'uri'
3
-
4
- module URI
5
-
6
- # Ruby does not have built-in support for filesystem URIs, and definitely does not have built-in support for
7
- # using open-uri with filesystem URIs
8
- class File < Generic
9
-
10
- COMPONENT = [
11
- :scheme,
12
- :path,
13
- :fragment,
14
- :host
15
- ].freeze
16
-
17
- def initialize(*arg)
18
- # arg[2] is the 'host'; this logic to set it to "" causes file schemes with UNC to break
19
- # so don't do it on windows platforms
20
- is_windows = (RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/)
21
- arg[2] = "" unless is_windows
22
- super(*arg)
23
- end
24
-
25
- def self.build(args)
26
- tmp = Util::make_components_hash(self, args)
27
- return super(tmp)
28
- end
29
-
30
- def open(*rest, &block)
31
- ::File.open(self.path, *rest, &block)
32
- end
33
-
34
- @@schemes['FILE'] = File
35
- end
36
- end