json-schema 2.8.1 → 4.1.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.
- checksums.yaml +4 -4
- data/README.md +54 -10
- data/lib/json-schema/attribute.rb +13 -14
- data/lib/json-schema/attributes/additionalitems.rb +1 -0
- data/lib/json-schema/attributes/additionalproperties.rb +3 -6
- data/lib/json-schema/attributes/allof.rb +6 -4
- data/lib/json-schema/attributes/anyof.rb +2 -2
- data/lib/json-schema/attributes/const.rb +15 -0
- data/lib/json-schema/attributes/dependencies.rb +1 -0
- data/lib/json-schema/attributes/disallow.rb +2 -1
- data/lib/json-schema/attributes/enum.rb +2 -2
- data/lib/json-schema/attributes/extends.rb +6 -6
- data/lib/json-schema/attributes/format.rb +2 -1
- data/lib/json-schema/attributes/formats/date.rb +1 -0
- data/lib/json-schema/attributes/formats/date_time.rb +2 -1
- data/lib/json-schema/attributes/formats/date_time_v4.rb +1 -0
- data/lib/json-schema/attributes/formats/ip.rb +1 -1
- data/lib/json-schema/attributes/formats/uri.rb +1 -0
- data/lib/json-schema/attributes/items.rb +1 -0
- data/lib/json-schema/attributes/limits/numeric.rb +1 -1
- data/lib/json-schema/attributes/maxdecimal.rb +1 -1
- data/lib/json-schema/attributes/not.rb +2 -2
- data/lib/json-schema/attributes/oneof.rb +2 -4
- data/lib/json-schema/attributes/patternproperties.rb +1 -0
- data/lib/json-schema/attributes/properties.rb +7 -7
- data/lib/json-schema/attributes/properties_v4.rb +1 -1
- data/lib/json-schema/attributes/propertynames.rb +23 -0
- data/lib/json-schema/attributes/ref.rb +7 -7
- data/lib/json-schema/attributes/required.rb +3 -2
- data/lib/json-schema/attributes/type.rb +3 -2
- data/lib/json-schema/attributes/type_v4.rb +1 -1
- data/lib/json-schema/errors/validation_error.rb +4 -5
- data/lib/json-schema/schema/reader.rb +3 -1
- data/lib/json-schema/schema/validator.rb +3 -3
- data/lib/json-schema/schema.rb +3 -4
- data/lib/json-schema/util/array_set.rb +1 -1
- data/lib/json-schema/util/uri.rb +7 -7
- data/lib/json-schema/util/uuid.rb +227 -226
- data/lib/json-schema/validator.rb +119 -114
- data/lib/json-schema/validators/draft1.rb +21 -23
- data/lib/json-schema/validators/draft2.rb +22 -24
- data/lib/json-schema/validators/draft3.rb +26 -28
- data/lib/json-schema/validators/draft4.rb +34 -36
- data/lib/json-schema/validators/draft6.rb +36 -36
- data/lib/json-schema/validators/hyper-draft1.rb +2 -3
- data/lib/json-schema/validators/hyper-draft2.rb +2 -3
- data/lib/json-schema/validators/hyper-draft3.rb +2 -3
- data/lib/json-schema/validators/hyper-draft4.rb +2 -3
- data/lib/json-schema/validators/hyper-draft6.rb +2 -3
- data/lib/json-schema.rb +2 -2
- data/resources/draft-06.json +12 -12
- metadata +15 -13
@@ -25,14 +25,14 @@ module JSON
|
|
25
25
|
valid = data_valid_for_type?(data, type)
|
26
26
|
elsif type.is_a?(Hash) && union
|
27
27
|
# Validate as a schema
|
28
|
-
schema = JSON::Schema.new(type,current_schema.uri,validator)
|
28
|
+
schema = JSON::Schema.new(type, current_schema.uri, validator)
|
29
29
|
|
30
30
|
# We're going to add a little cruft here to try and maintain any validation errors that occur in this union type
|
31
31
|
# We'll handle this by keeping an error count before and after validation, extracting those errors and pushing them onto a union error
|
32
32
|
pre_validation_error_count = validation_errors(processor).count
|
33
33
|
|
34
34
|
begin
|
35
|
-
schema.validate(data,fragments,processor,options.merge(:
|
35
|
+
schema.validate(data, fragments, processor, options.merge(disallow: false))
|
36
36
|
valid = true
|
37
37
|
rescue ValidationError
|
38
38
|
# We don't care that these schemas don't validate - we only care that one validated
|
@@ -51,6 +51,7 @@ module JSON
|
|
51
51
|
|
52
52
|
if options[:disallow]
|
53
53
|
return if !valid
|
54
|
+
|
54
55
|
message = "The property '#{build_fragment(fragments)}' matched one or more of the following types: #{list_types(types)}"
|
55
56
|
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
|
56
57
|
elsif !valid
|
@@ -19,7 +19,7 @@ module JSON
|
|
19
19
|
build_fragment(fragments),
|
20
20
|
type_of_data(data),
|
21
21
|
union ? 'one or more of the following types' : 'the following type',
|
22
|
-
types
|
22
|
+
types,
|
23
23
|
)
|
24
24
|
|
25
25
|
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module JSON
|
2
2
|
class Schema
|
3
3
|
class ValidationError < StandardError
|
4
|
-
INDENT =
|
4
|
+
INDENT = ' '
|
5
5
|
attr_accessor :fragments, :schema, :failed_attribute, :sub_errors, :message
|
6
6
|
|
7
7
|
def initialize(message, fragments, failed_attribute, schema)
|
@@ -27,12 +27,11 @@ module JSON
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def to_hash
|
30
|
-
base = {:
|
30
|
+
base = { schema: @schema.uri, fragment: ::JSON::Schema::Attribute.build_fragment(fragments), message: message_with_schema, failed_attribute: @failed_attribute.to_s.split(':').last.split('Attribute').first }
|
31
31
|
if !@sub_errors.empty?
|
32
|
-
base[:errors] = @sub_errors.
|
32
|
+
base[:errors] = @sub_errors.each_with_object({}) do |(subschema, errors), hsh|
|
33
33
|
subschema_sym = subschema.downcase.gsub(/\W+/, '_').to_sym
|
34
|
-
hsh[subschema_sym] = Array(errors).map{|e| e.to_hash}
|
35
|
-
hsh
|
34
|
+
hsh[subschema_sym] = Array(errors).map { |e| e.to_hash }
|
36
35
|
end
|
37
36
|
end
|
38
37
|
base
|
@@ -28,6 +28,7 @@ module JSON
|
|
28
28
|
# a schema should not be read.
|
29
29
|
class ReadRefused < ReadError
|
30
30
|
private
|
31
|
+
|
31
32
|
def error_message
|
32
33
|
"Read of #{type_string} at #{location} refused"
|
33
34
|
end
|
@@ -36,6 +37,7 @@ module JSON
|
|
36
37
|
# Raised by {JSON::Schema::Reader} when an attempt to read a schema fails
|
37
38
|
class ReadFailed < ReadError
|
38
39
|
private
|
40
|
+
|
39
41
|
def error_message
|
40
42
|
"Read of #{type_string} at #{location} failed"
|
41
43
|
end
|
@@ -118,7 +120,7 @@ module JSON
|
|
118
120
|
|
119
121
|
def read_uri(uri)
|
120
122
|
if accept_uri?(uri)
|
121
|
-
open(uri.to_s).read
|
123
|
+
URI.open(uri.to_s).read
|
122
124
|
else
|
123
125
|
raise JSON::Schema::ReadRefused.new(uri.to_s, :uri)
|
124
126
|
end
|
@@ -4,7 +4,7 @@ module JSON
|
|
4
4
|
attr_accessor :attributes, :formats, :uri, :names
|
5
5
|
attr_reader :default_formats
|
6
6
|
|
7
|
-
def initialize
|
7
|
+
def initialize
|
8
8
|
@attributes = {}
|
9
9
|
@formats = {}
|
10
10
|
@default_formats = {}
|
@@ -14,13 +14,13 @@ module JSON
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def extend_schema_definition(schema_uri)
|
17
|
-
warn
|
17
|
+
warn '[DEPRECATION NOTICE] The preferred way to extend a Validator is by subclassing, rather than #extend_schema_definition. This method will be removed in version >= 3.'
|
18
18
|
validator = JSON::Validator.validator_for_uri(schema_uri)
|
19
19
|
@attributes.merge!(validator.attributes)
|
20
20
|
end
|
21
21
|
|
22
22
|
def validate(current_schema, data, fragments, processor, options = {})
|
23
|
-
current_schema.schema.each do |attr_name,attribute|
|
23
|
+
current_schema.schema.each do |attr_name, attribute|
|
24
24
|
if @attributes.has_key?(attr_name.to_s)
|
25
25
|
@attributes[attr_name.to_s].validate(current_schema, data, fragments, processor, self, options)
|
26
26
|
end
|
data/lib/json-schema/schema.rb
CHANGED
@@ -2,15 +2,14 @@ require 'pathname'
|
|
2
2
|
|
3
3
|
module JSON
|
4
4
|
class Schema
|
5
|
-
|
6
5
|
attr_accessor :schema, :uri, :validator
|
7
6
|
|
8
|
-
def initialize(schema,uri,parent_validator=nil)
|
7
|
+
def initialize(schema, uri, parent_validator = nil)
|
9
8
|
@schema = schema
|
10
9
|
@uri = uri
|
11
10
|
|
12
11
|
# If there is an ID on this schema, use it to generate the URI
|
13
|
-
if @schema['id']
|
12
|
+
if @schema['id'].is_a?(String)
|
14
13
|
temp_uri = JSON::Util::URI.parse(@schema['id'])
|
15
14
|
if temp_uri.relative?
|
16
15
|
temp_uri = uri.join(temp_uri)
|
@@ -36,7 +35,7 @@ module JSON
|
|
36
35
|
def self.stringify(schema)
|
37
36
|
case schema
|
38
37
|
when Hash then
|
39
|
-
|
38
|
+
schema.map { |key, value| [key.to_s, stringify(schema[key])] }.to_h
|
40
39
|
when Array then
|
41
40
|
schema.map do |schema_item|
|
42
41
|
stringify(schema_item)
|
@@ -7,7 +7,7 @@ class ArraySet < Array
|
|
7
7
|
def include?(obj)
|
8
8
|
if !defined? @values
|
9
9
|
@values = Set.new
|
10
|
-
|
10
|
+
each { |x| @values << convert_to_float_if_numeric(x) }
|
11
11
|
end
|
12
12
|
@values.include?(convert_to_float_if_numeric(obj))
|
13
13
|
end
|
data/lib/json-schema/util/uri.rb
CHANGED
@@ -14,7 +14,7 @@ module JSON
|
|
14
14
|
# Check for absolute path
|
15
15
|
if normalized_uri.relative?
|
16
16
|
data = normalized_uri
|
17
|
-
data = File.join(base_path, data) if data.path[0,1] !=
|
17
|
+
data = File.join(base_path, data) if data.path[0, 1] != '/'
|
18
18
|
normalized_uri = file_uri(data)
|
19
19
|
end
|
20
20
|
@normalize_cache[uri] = normalized_uri.freeze
|
@@ -42,10 +42,10 @@ module JSON
|
|
42
42
|
ref_uri.merge!(base_uri)
|
43
43
|
|
44
44
|
# Check for absolute path
|
45
|
-
path, fragment = ref.to_s.split(
|
45
|
+
path, fragment = ref.to_s.split('#')
|
46
46
|
if path.nil? || path == ''
|
47
47
|
ref_uri.path = base_uri.path
|
48
|
-
elsif path[0,1] ==
|
48
|
+
elsif path[0, 1] == '/'
|
49
49
|
ref_uri.path = Pathname.new(path).cleanpath.to_s
|
50
50
|
else
|
51
51
|
ref_uri.join!(path)
|
@@ -54,7 +54,7 @@ module JSON
|
|
54
54
|
ref_uri.fragment = fragment
|
55
55
|
end
|
56
56
|
|
57
|
-
ref_uri.fragment =
|
57
|
+
ref_uri.fragment = '' if ref_uri.fragment.nil? || ref_uri.fragment.empty?
|
58
58
|
end
|
59
59
|
|
60
60
|
ref_uri
|
@@ -62,7 +62,7 @@ module JSON
|
|
62
62
|
|
63
63
|
def self.parse(uri)
|
64
64
|
if uri.is_a?(Addressable::URI)
|
65
|
-
|
65
|
+
uri.dup
|
66
66
|
else
|
67
67
|
@parse_cache ||= {}
|
68
68
|
parsed_uri = @parse_cache[uri]
|
@@ -73,7 +73,7 @@ module JSON
|
|
73
73
|
end
|
74
74
|
end
|
75
75
|
rescue Addressable::URI::InvalidURIError => e
|
76
|
-
raise JSON::Schema::UriError
|
76
|
+
raise JSON::Schema::UriError, e.message
|
77
77
|
end
|
78
78
|
|
79
79
|
def self.strip_fragment(uri)
|
@@ -81,7 +81,7 @@ module JSON
|
|
81
81
|
if parsed_uri.fragment.nil? || parsed_uri.fragment.empty?
|
82
82
|
parsed_uri
|
83
83
|
else
|
84
|
-
parsed_uri.merge(:
|
84
|
+
parsed_uri.merge(fragment: '')
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|