json-schema 2.8.0 → 5.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 +5 -5
- data/README.md +82 -11
- 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 -6
- data/lib/json-schema/attributes/dependencies_v4.rb +11 -0
- data/lib/json-schema/attributes/disallow.rb +2 -1
- data/lib/json-schema/attributes/divisibleby.rb +1 -1
- data/lib/json-schema/attributes/enum.rb +2 -2
- data/lib/json-schema/attributes/extends.rb +7 -7
- data/lib/json-schema/attributes/format.rb +2 -1
- data/lib/json-schema/attributes/formats/custom.rb +1 -1
- data/lib/json-schema/attributes/formats/date.rb +2 -1
- data/lib/json-schema/attributes/formats/date_time.rb +3 -2
- data/lib/json-schema/attributes/formats/date_time_v4.rb +2 -1
- data/lib/json-schema/attributes/formats/ip.rb +1 -1
- data/lib/json-schema/attributes/formats/time.rb +1 -1
- data/lib/json-schema/attributes/formats/uri.rb +2 -1
- data/lib/json-schema/attributes/items.rb +1 -0
- data/lib/json-schema/attributes/limit.rb +0 -127
- data/lib/json-schema/attributes/limits/items.rb +15 -0
- data/lib/json-schema/attributes/limits/length.rb +15 -0
- data/lib/json-schema/attributes/limits/max_items.rb +15 -0
- data/lib/json-schema/attributes/limits/max_length.rb +15 -0
- data/lib/json-schema/attributes/limits/max_properties.rb +15 -0
- data/lib/json-schema/attributes/limits/maximum.rb +15 -0
- data/lib/json-schema/attributes/limits/maximum_inclusive.rb +11 -0
- data/lib/json-schema/attributes/limits/min_items.rb +15 -0
- data/lib/json-schema/attributes/limits/min_length.rb +15 -0
- data/lib/json-schema/attributes/limits/min_properties.rb +15 -0
- data/lib/json-schema/attributes/limits/minimum.rb +15 -0
- data/lib/json-schema/attributes/limits/minimum_inclusive.rb +11 -0
- data/lib/json-schema/attributes/limits/numeric.rb +16 -0
- data/lib/json-schema/attributes/limits/properties.rb +15 -0
- 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 +2 -1
- data/lib/json-schema/attributes/properties.rb +9 -17
- data/lib/json-schema/attributes/properties_v4.rb +13 -0
- data/lib/json-schema/attributes/propertynames.rb +23 -0
- data/lib/json-schema/attributes/ref.rb +8 -8
- data/lib/json-schema/attributes/required.rb +4 -3
- 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 +5 -6
- 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 +98 -75
- data/lib/json-schema/util/uuid.rb +203 -226
- data/lib/json-schema/validator.rb +122 -115
- 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 -3
- data/resources/draft-06.json +41 -41
- metadata +67 -31
@@ -48,132 +48,5 @@ module JSON
|
|
48
48
|
raise NotImplementedError
|
49
49
|
end
|
50
50
|
end
|
51
|
-
|
52
|
-
class MinLengthAttribute < LimitAttribute
|
53
|
-
def self.acceptable_type
|
54
|
-
String
|
55
|
-
end
|
56
|
-
|
57
|
-
def self.limit_name
|
58
|
-
'minLength'
|
59
|
-
end
|
60
|
-
|
61
|
-
def self.error_message(schema)
|
62
|
-
"was not of a minimum string length of #{limit(schema)}"
|
63
|
-
end
|
64
|
-
|
65
|
-
def self.value(data)
|
66
|
-
data.length
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
class MaxLengthAttribute < MinLengthAttribute
|
71
|
-
def self.limit_name
|
72
|
-
'maxLength'
|
73
|
-
end
|
74
|
-
|
75
|
-
def self.error_message(schema)
|
76
|
-
"was not of a maximum string length of #{limit(schema)}"
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
class MinItemsAttribute < LimitAttribute
|
81
|
-
def self.acceptable_type
|
82
|
-
Array
|
83
|
-
end
|
84
|
-
|
85
|
-
def self.value(data)
|
86
|
-
data.length
|
87
|
-
end
|
88
|
-
|
89
|
-
def self.limit_name
|
90
|
-
'minItems'
|
91
|
-
end
|
92
|
-
|
93
|
-
def self.error_message(schema)
|
94
|
-
"did not contain a minimum number of items #{limit(schema)}"
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
class MaxItemsAttribute < MinItemsAttribute
|
99
|
-
def self.limit_name
|
100
|
-
'maxItems'
|
101
|
-
end
|
102
|
-
|
103
|
-
def self.error_message(schema)
|
104
|
-
"had more items than the allowed #{limit(schema)}"
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
class MinPropertiesAttribute < LimitAttribute
|
109
|
-
def self.acceptable_type
|
110
|
-
Hash
|
111
|
-
end
|
112
|
-
|
113
|
-
def self.value(data)
|
114
|
-
data.size
|
115
|
-
end
|
116
|
-
|
117
|
-
def self.limit_name
|
118
|
-
'minProperties'
|
119
|
-
end
|
120
|
-
|
121
|
-
def self.error_message(schema)
|
122
|
-
"did not contain a minimum number of properties #{limit(schema)}"
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
class MaxPropertiesAttribute < MinPropertiesAttribute
|
127
|
-
def self.limit_name
|
128
|
-
'maxProperties'
|
129
|
-
end
|
130
|
-
|
131
|
-
def self.error_message(schema)
|
132
|
-
"had more properties than the allowed #{limit(schema)}"
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
class NumericLimitAttribute < LimitAttribute
|
137
|
-
def self.acceptable_type
|
138
|
-
Numeric
|
139
|
-
end
|
140
|
-
|
141
|
-
def self.error_message(schema)
|
142
|
-
exclusivity = exclusive?(schema) ? 'exclusively' : 'inclusively'
|
143
|
-
format("did not have a %s value of %s, %s", limit_name, limit(schema), exclusivity)
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
class MaximumAttribute < NumericLimitAttribute
|
148
|
-
def self.limit_name
|
149
|
-
'maximum'
|
150
|
-
end
|
151
|
-
|
152
|
-
def self.exclusive?(schema)
|
153
|
-
schema['exclusiveMaximum']
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
class MaximumInclusiveAttribute < MaximumAttribute
|
158
|
-
def self.exclusive?(schema)
|
159
|
-
schema['maximumCanEqual'] == false
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
class MinimumAttribute < NumericLimitAttribute
|
164
|
-
def self.limit_name
|
165
|
-
'minimum'
|
166
|
-
end
|
167
|
-
|
168
|
-
def self.exclusive?(schema)
|
169
|
-
schema['exclusiveMinimum']
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
class MinimumInclusiveAttribute < MinimumAttribute
|
174
|
-
def self.exclusive?(schema)
|
175
|
-
schema['minimumCanEqual'] == false
|
176
|
-
end
|
177
|
-
end
|
178
51
|
end
|
179
52
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'json-schema/attributes/limits/items'
|
2
|
+
|
3
|
+
module JSON
|
4
|
+
class Schema
|
5
|
+
class MaxItemsAttribute < ItemsLimitAttribute
|
6
|
+
def self.limit_name
|
7
|
+
'maxItems'
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.error_message(schema)
|
11
|
+
"had more items than the allowed #{limit(schema)}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'json-schema/attributes/limits/length'
|
2
|
+
|
3
|
+
module JSON
|
4
|
+
class Schema
|
5
|
+
class MaxLengthAttribute < LengthLimitAttribute
|
6
|
+
def self.limit_name
|
7
|
+
'maxLength'
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.error_message(schema)
|
11
|
+
"was not of a maximum string length of #{limit(schema)}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'json-schema/attributes/limits/properties'
|
2
|
+
|
3
|
+
module JSON
|
4
|
+
class Schema
|
5
|
+
class MaxPropertiesAttribute < PropertiesLimitAttribute
|
6
|
+
def self.limit_name
|
7
|
+
'maxProperties'
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.error_message(schema)
|
11
|
+
"had more properties than the allowed #{limit(schema)}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'json-schema/attributes/limits/numeric'
|
2
|
+
|
3
|
+
module JSON
|
4
|
+
class Schema
|
5
|
+
class MaximumAttribute < NumericLimitAttribute
|
6
|
+
def self.limit_name
|
7
|
+
'maximum'
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.exclusive?(schema)
|
11
|
+
schema['exclusiveMaximum']
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'json-schema/attributes/limits/items'
|
2
|
+
|
3
|
+
module JSON
|
4
|
+
class Schema
|
5
|
+
class MinItemsAttribute < ItemsLimitAttribute
|
6
|
+
def self.limit_name
|
7
|
+
'minItems'
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.error_message(schema)
|
11
|
+
"did not contain a minimum number of items #{limit(schema)}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'json-schema/attributes/limits/length'
|
2
|
+
|
3
|
+
module JSON
|
4
|
+
class Schema
|
5
|
+
class MinLengthAttribute < LengthLimitAttribute
|
6
|
+
def self.limit_name
|
7
|
+
'minLength'
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.error_message(schema)
|
11
|
+
"was not of a minimum string length of #{limit(schema)}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'json-schema/attributes/limits/properties'
|
2
|
+
|
3
|
+
module JSON
|
4
|
+
class Schema
|
5
|
+
class MinPropertiesAttribute < PropertiesLimitAttribute
|
6
|
+
def self.limit_name
|
7
|
+
'minProperties'
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.error_message(schema)
|
11
|
+
"did not contain a minimum number of properties #{limit(schema)}"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'json-schema/attributes/limits/numeric'
|
2
|
+
|
3
|
+
module JSON
|
4
|
+
class Schema
|
5
|
+
class MinimumAttribute < NumericLimitAttribute
|
6
|
+
def self.limit_name
|
7
|
+
'minimum'
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.exclusive?(schema)
|
11
|
+
schema['exclusiveMinimum']
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'json-schema/attributes/limit'
|
2
|
+
|
3
|
+
module JSON
|
4
|
+
class Schema
|
5
|
+
class NumericLimitAttribute < LimitAttribute
|
6
|
+
def self.acceptable_type
|
7
|
+
Numeric
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.error_message(schema)
|
11
|
+
exclusivity = exclusive?(schema) ? 'exclusively' : 'inclusively'
|
12
|
+
format('did not have a %s value of %s, %s', limit_name, limit(schema), exclusivity)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -7,7 +7,7 @@ module JSON
|
|
7
7
|
return unless data.is_a?(Numeric)
|
8
8
|
|
9
9
|
max_decimal_places = current_schema.schema['maxDecimal']
|
10
|
-
s = data.to_s.split(
|
10
|
+
s = data.to_s.split('.')[1]
|
11
11
|
if s && s.length > max_decimal_places
|
12
12
|
message = "The property '#{build_fragment(fragments)}' had more decimal places than the allowed #{max_decimal_places}"
|
13
13
|
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
|
@@ -4,12 +4,12 @@ module JSON
|
|
4
4
|
class Schema
|
5
5
|
class NotAttribute < Attribute
|
6
6
|
def self.validate(current_schema, data, fragments, processor, validator, options = {})
|
7
|
-
schema = JSON::Schema.new(current_schema.schema['not'],current_schema.uri,validator)
|
7
|
+
schema = JSON::Schema.new(current_schema.schema['not'], current_schema.uri, validator)
|
8
8
|
failed = true
|
9
9
|
errors_copy = processor.validation_errors.clone
|
10
10
|
|
11
11
|
begin
|
12
|
-
schema.validate(data,fragments,processor,options)
|
12
|
+
schema.validate(data, fragments, processor, options)
|
13
13
|
# If we're recording errors, we don't throw an exception. Instead, check the errors array length
|
14
14
|
if options[:record_errors] && errors_copy.length != processor.validation_errors.length
|
15
15
|
processor.validation_errors.replace(errors_copy)
|
@@ -15,10 +15,10 @@ module JSON
|
|
15
15
|
valid = false
|
16
16
|
|
17
17
|
one_of.each_with_index do |element, schema_index|
|
18
|
-
schema = JSON::Schema.new(element,current_schema.uri,validator)
|
18
|
+
schema = JSON::Schema.new(element, current_schema.uri, validator)
|
19
19
|
pre_validation_error_count = validation_errors(processor).count
|
20
20
|
begin
|
21
|
-
schema.validate(data,fragments,processor,options)
|
21
|
+
schema.validate(data, fragments, processor, options)
|
22
22
|
success_data = data.is_a?(Hash) ? data.clone : data
|
23
23
|
valid = true
|
24
24
|
rescue ValidationError
|
@@ -35,8 +35,6 @@ module JSON
|
|
35
35
|
data = original_data
|
36
36
|
end
|
37
37
|
|
38
|
-
|
39
|
-
|
40
38
|
if validation_error_count == one_of.length - 1
|
41
39
|
data = success_data
|
42
40
|
return
|
@@ -10,8 +10,9 @@ module JSON
|
|
10
10
|
regexp = Regexp.new(property)
|
11
11
|
|
12
12
|
# Check each key in the data hash to see if it matches the regex
|
13
|
-
data.each do |key,
|
13
|
+
data.each do |key, _value|
|
14
14
|
next unless regexp.match(key)
|
15
|
+
|
15
16
|
schema = JSON::Schema.new(property_schema, current_schema.uri, validator)
|
16
17
|
schema.validate(data[key], fragments + [key], processor, options)
|
17
18
|
end
|
@@ -4,7 +4,7 @@ module JSON
|
|
4
4
|
class Schema
|
5
5
|
class PropertiesAttribute < Attribute
|
6
6
|
def self.required?(schema, options)
|
7
|
-
schema.fetch('required') { options[:
|
7
|
+
schema.fetch('required') { options[:allPropertiesRequired] }
|
8
8
|
end
|
9
9
|
|
10
10
|
def self.validate(current_schema, data, fragments, processor, validator, options = {})
|
@@ -15,9 +15,9 @@ module JSON
|
|
15
15
|
property = property.to_s
|
16
16
|
|
17
17
|
if !data.key?(property) &&
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
options[:insert_defaults] &&
|
19
|
+
property_schema.has_key?('default') &&
|
20
|
+
!property_schema['readonly']
|
21
21
|
default = property_schema['default']
|
22
22
|
data[property] = default.is_a?(Hash) ? default.clone : default
|
23
23
|
end
|
@@ -33,15 +33,15 @@ module JSON
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
# When
|
37
|
-
return unless options[:
|
36
|
+
# When noAdditionalProperties is true, ensure no undefined properties exist in the data
|
37
|
+
return unless options[:noAdditionalProperties] == true && !schema.key?('additionalProperties')
|
38
38
|
|
39
|
-
diff = data.select do |k,
|
39
|
+
diff = data.select do |k, _v|
|
40
40
|
k = k.to_s
|
41
41
|
|
42
42
|
if schema.has_key?('patternProperties')
|
43
43
|
match = false
|
44
|
-
schema['patternProperties'].each do |property,
|
44
|
+
schema['patternProperties'].each do |property, _property_schema|
|
45
45
|
regexp = Regexp.new(property)
|
46
46
|
if regexp.match(k)
|
47
47
|
match = true
|
@@ -55,20 +55,12 @@ module JSON
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
-
|
58
|
+
unless diff.empty?
|
59
59
|
properties = diff.keys.join(', ')
|
60
60
|
message = "The property '#{build_fragment(fragments)}' contained undefined properties: '#{properties}'"
|
61
61
|
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
|
62
62
|
end
|
63
63
|
end
|
64
64
|
end
|
65
|
-
|
66
|
-
class PropertiesV4Attribute < PropertiesAttribute
|
67
|
-
# draft4 relies on its own RequiredAttribute validation at a higher level, rather than
|
68
|
-
# as an attribute of individual properties.
|
69
|
-
def self.required?(schema, options)
|
70
|
-
options[:strict] == true
|
71
|
-
end
|
72
|
-
end
|
73
65
|
end
|
74
66
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'json-schema/attributes/properties'
|
2
|
+
|
3
|
+
module JSON
|
4
|
+
class Schema
|
5
|
+
class PropertiesV4Attribute < PropertiesAttribute
|
6
|
+
# draft4 relies on its own RequiredAttribute validation at a higher level, rather than
|
7
|
+
# as an attribute of individual properties.
|
8
|
+
def self.required?(schema, options)
|
9
|
+
options[:allPropertiesRequired] == true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'json-schema/attribute'
|
2
|
+
|
3
|
+
module JSON
|
4
|
+
class Schema
|
5
|
+
class PropertyNames < Attribute
|
6
|
+
def self.validate(current_schema, data, fragments, processor, validator, options = {})
|
7
|
+
return unless data.is_a?(Hash)
|
8
|
+
|
9
|
+
propnames = current_schema.schema['propertyNames']
|
10
|
+
|
11
|
+
if propnames.is_a?(Hash)
|
12
|
+
schema = JSON::Schema.new(propnames, current_schema.uri, validator)
|
13
|
+
data.each_key do |key|
|
14
|
+
schema.validate(key, fragments + [key], processor, options)
|
15
|
+
end
|
16
|
+
elsif propnames == false && data.any?
|
17
|
+
message = "The property '#{build_fragment(fragments)}' contains additional properties #{data.keys.inspect} outside of the schema when none are allowed"
|
18
|
+
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -6,12 +6,12 @@ module JSON
|
|
6
6
|
class Schema
|
7
7
|
class RefAttribute < Attribute
|
8
8
|
def self.validate(current_schema, data, fragments, processor, validator, options = {})
|
9
|
-
uri,schema = get_referenced_uri_and_schema(current_schema.schema, current_schema, validator)
|
9
|
+
uri, schema = get_referenced_uri_and_schema(current_schema.schema, current_schema, validator)
|
10
10
|
|
11
11
|
if schema
|
12
12
|
schema.validate(data, fragments, processor, options)
|
13
13
|
elsif uri
|
14
|
-
message = "The referenced schema '#{uri
|
14
|
+
message = "The referenced schema '#{uri}' cannot be found"
|
15
15
|
validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
|
16
16
|
else
|
17
17
|
message = "The property '#{build_fragment(fragments)}' was not a valid schema"
|
@@ -20,19 +20,19 @@ module JSON
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def self.get_referenced_uri_and_schema(s, current_schema, validator)
|
23
|
-
uri,schema = nil,nil
|
23
|
+
uri, schema = nil, nil
|
24
24
|
|
25
25
|
temp_uri = JSON::Util::URI.normalize_ref(s['$ref'], current_schema.uri)
|
26
26
|
|
27
27
|
# Grab the parent schema from the schema list
|
28
|
-
schema_key = temp_uri.to_s.split(
|
28
|
+
schema_key = temp_uri.to_s.split('#')[0] + '#'
|
29
29
|
|
30
30
|
ref_schema = JSON::Validator.schema_for_uri(schema_key)
|
31
31
|
|
32
32
|
if ref_schema
|
33
33
|
# Perform fragment resolution to retrieve the appropriate level for the schema
|
34
34
|
target_schema = ref_schema.schema
|
35
|
-
fragments = JSON::Util::URI.parse(JSON::Util::URI.unescape_uri(temp_uri)).fragment.split(
|
35
|
+
fragments = JSON::Util::URI.parse(JSON::Util::URI.unescape_uri(temp_uri)).fragment.split('/')
|
36
36
|
fragment_path = ''
|
37
37
|
fragments.each do |fragment|
|
38
38
|
if fragment && fragment != ''
|
@@ -44,17 +44,17 @@ module JSON
|
|
44
44
|
end
|
45
45
|
fragment_path = fragment_path + "/#{fragment}"
|
46
46
|
if target_schema.nil?
|
47
|
-
raise SchemaError
|
47
|
+
raise SchemaError, "The fragment '#{fragment_path}' does not exist on schema #{ref_schema.uri}"
|
48
48
|
end
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
52
|
# We have the schema finally, build it and validate!
|
53
53
|
uri = temp_uri
|
54
|
-
schema = JSON::Schema.new(target_schema,temp_uri,validator)
|
54
|
+
schema = JSON::Schema.new(target_schema, temp_uri, validator)
|
55
55
|
end
|
56
56
|
|
57
|
-
[uri,schema]
|
57
|
+
[uri, schema]
|
58
58
|
end
|
59
59
|
end
|
60
60
|
end
|
@@ -9,13 +9,14 @@ module JSON
|
|
9
9
|
schema = current_schema.schema
|
10
10
|
defined_properties = schema['properties']
|
11
11
|
|
12
|
-
schema['required'].each do |property,
|
12
|
+
schema['required'].each do |property, _property_schema|
|
13
13
|
next if data.has_key?(property.to_s)
|
14
|
+
|
14
15
|
prop_defaults = options[:insert_defaults] &&
|
15
16
|
defined_properties &&
|
16
17
|
defined_properties[property] &&
|
17
|
-
!defined_properties[property][
|
18
|
-
!defined_properties[property][
|
18
|
+
!defined_properties[property]['default'].nil? &&
|
19
|
+
!defined_properties[property]['readonly']
|
19
20
|
|
20
21
|
if !prop_defaults
|
21
22
|
message = "The property '#{build_fragment(fragments)}' did not contain a required property of '#{property}'"
|
@@ -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)
|
@@ -20,19 +20,18 @@ module JSON
|
|
20
20
|
messages = ["#{message}. The schema specific errors were:\n"]
|
21
21
|
@sub_errors.each do |subschema, errors|
|
22
22
|
messages.push "- #{subschema}:"
|
23
|
-
messages.concat
|
23
|
+
messages.concat(Array(errors).map { |e| "#{INDENT}- #{e.to_string(subschema_level + 1)}" })
|
24
24
|
end
|
25
25
|
messages.map { |m| (INDENT * subschema_level) + m }.join("\n")
|
26
26
|
end
|
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
|