json-schema 3.0.0 → 4.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +15 -1
- 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/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/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 +2 -0
- 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 +97 -84
- 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 -37
- 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 +7 -6
@@ -15,21 +15,22 @@ require 'json-schema/errors/json_parse_error'
|
|
15
15
|
require 'json-schema/util/uri'
|
16
16
|
|
17
17
|
module JSON
|
18
|
-
|
19
18
|
class Validator
|
20
|
-
|
21
19
|
@@schemas = {}
|
22
20
|
@@cache_schemas = true
|
23
21
|
@@default_opts = {
|
24
|
-
:
|
25
|
-
:
|
26
|
-
:
|
27
|
-
:
|
28
|
-
:
|
29
|
-
:
|
30
|
-
:
|
31
|
-
:
|
32
|
-
:
|
22
|
+
list: false,
|
23
|
+
version: nil,
|
24
|
+
validate_schema: false,
|
25
|
+
record_errors: false,
|
26
|
+
errors_as_objects: false,
|
27
|
+
insert_defaults: false,
|
28
|
+
clear_cache: false,
|
29
|
+
strict: false,
|
30
|
+
allPropertiesRequired: false,
|
31
|
+
noAdditionalProperties: false,
|
32
|
+
parse_data: true,
|
33
|
+
parse_integer: true,
|
33
34
|
}
|
34
35
|
@@validators = {}
|
35
36
|
@@default_validator = nil
|
@@ -38,28 +39,32 @@ module JSON
|
|
38
39
|
@@serializer = nil
|
39
40
|
@@mutex = Mutex.new
|
40
41
|
|
41
|
-
def initialize(schema_data,
|
42
|
+
def initialize(schema_data, opts = {})
|
42
43
|
@options = @@default_opts.clone.merge(opts)
|
43
44
|
@errors = []
|
44
45
|
|
45
46
|
configured_validator = self.class.validator_for_name(@options[:version])
|
46
47
|
@options[:schema_reader] ||= self.class.schema_reader
|
47
48
|
|
48
|
-
@validation_options = @options[:record_errors] ? {:
|
49
|
+
@validation_options = @options[:record_errors] ? { record_errors: true } : {}
|
49
50
|
@validation_options[:insert_defaults] = true if @options[:insert_defaults]
|
50
|
-
|
51
|
+
if @options[:strict] == true
|
52
|
+
@validation_options[:allPropertiesRequired] = true
|
53
|
+
@validation_options[:noAdditionalProperties] = true
|
54
|
+
else
|
55
|
+
@validation_options[:allPropertiesRequired] = true if @options[:allPropertiesRequired]
|
56
|
+
@validation_options[:noAdditionalProperties] = true if @options[:noAdditionalProperties]
|
57
|
+
end
|
51
58
|
@validation_options[:clear_cache] = true if !@@cache_schemas || @options[:clear_cache]
|
52
59
|
|
53
60
|
@@mutex.synchronize { @base_schema = initialize_schema(schema_data, configured_validator) }
|
54
|
-
@original_data = data
|
55
|
-
@data = initialize_data(data)
|
56
61
|
@@mutex.synchronize { build_schemas(@base_schema) }
|
57
62
|
|
58
63
|
# validate the schema, if requested
|
59
64
|
if @options[:validate_schema]
|
60
65
|
# Don't clear the cache during metaschema validation!
|
61
|
-
meta_validator = self.class.new(@base_schema.validator.metaschema,
|
62
|
-
meta_validator.validate
|
66
|
+
meta_validator = self.class.new(@base_schema.validator.metaschema, { clear_cache: false })
|
67
|
+
meta_validator.validate(@base_schema.schema)
|
63
68
|
end
|
64
69
|
|
65
70
|
# If the :fragment option is set, try and validate against the fragment
|
@@ -70,11 +75,11 @@ module JSON
|
|
70
75
|
|
71
76
|
def schema_from_fragment(base_schema, fragment)
|
72
77
|
schema_uri = base_schema.uri
|
73
|
-
fragments = fragment.split(
|
78
|
+
fragments = fragment.split('/').map { |f| f.gsub('~0', '~').gsub('~1', '/') }
|
74
79
|
|
75
80
|
# ensure the first element was a hash, per the fragment spec
|
76
|
-
if fragments.shift !=
|
77
|
-
raise JSON::Schema::SchemaError
|
81
|
+
if fragments.shift != '#'
|
82
|
+
raise JSON::Schema::SchemaError, 'Invalid fragment syntax in :fragment option'
|
78
83
|
end
|
79
84
|
|
80
85
|
schema_fragment = base_schema.schema
|
@@ -88,7 +93,7 @@ module JSON
|
|
88
93
|
end
|
89
94
|
|
90
95
|
unless schema_fragment.is_a?(Hash)
|
91
|
-
raise JSON::Schema::SchemaError
|
96
|
+
raise JSON::Schema::SchemaError, 'Invalid fragment resolution for :fragment option'
|
92
97
|
end
|
93
98
|
|
94
99
|
schema = JSON::Schema.new(schema_fragment, schema_uri, base_schema.validator)
|
@@ -103,24 +108,28 @@ module JSON
|
|
103
108
|
end
|
104
109
|
|
105
110
|
# Run a simple true/false validation of data against a schema
|
106
|
-
def validate
|
107
|
-
|
111
|
+
def validate(data)
|
112
|
+
original_data = data
|
113
|
+
data = initialize_data(data)
|
114
|
+
@base_schema.validate(data, [], self, @validation_options)
|
108
115
|
|
109
116
|
if @options[:record_errors]
|
110
117
|
if @options[:errors_as_objects]
|
111
|
-
@errors.map{|e| e.to_hash}
|
118
|
+
@errors.map { |e| e.to_hash }
|
112
119
|
else
|
113
|
-
@errors.map{|e| e.to_string}
|
120
|
+
@errors.map { |e| e.to_string }
|
114
121
|
end
|
115
122
|
else
|
116
123
|
true
|
117
124
|
end
|
118
125
|
ensure
|
126
|
+
@errors = []
|
127
|
+
|
119
128
|
if @validation_options[:clear_cache] == true
|
120
129
|
self.class.clear_cache
|
121
130
|
end
|
122
131
|
if @validation_options[:insert_defaults]
|
123
|
-
self.class.merge_missing_values(
|
132
|
+
self.class.merge_missing_values(data, original_data)
|
124
133
|
end
|
125
134
|
end
|
126
135
|
|
@@ -141,13 +150,13 @@ module JSON
|
|
141
150
|
schema = parent_schema.schema
|
142
151
|
|
143
152
|
# Build ref schemas if they exist
|
144
|
-
if schema[
|
145
|
-
load_ref_schema(parent_schema, schema[
|
153
|
+
if schema['$ref']
|
154
|
+
load_ref_schema(parent_schema, schema['$ref'])
|
146
155
|
end
|
147
156
|
|
148
|
-
case schema[
|
157
|
+
case schema['extends']
|
149
158
|
when String
|
150
|
-
load_ref_schema(parent_schema, schema[
|
159
|
+
load_ref_schema(parent_schema, schema['extends'])
|
151
160
|
when Array
|
152
161
|
schema['extends'].each do |type|
|
153
162
|
handle_schema(parent_schema, type)
|
@@ -155,7 +164,7 @@ module JSON
|
|
155
164
|
end
|
156
165
|
|
157
166
|
# Check for schemas in union types
|
158
|
-
[
|
167
|
+
%w[type disallow].each do |key|
|
159
168
|
if schema[key].is_a?(Array)
|
160
169
|
schema[key].each do |type|
|
161
170
|
if type.is_a?(Hash)
|
@@ -169,6 +178,7 @@ module JSON
|
|
169
178
|
# are themselves schemas.
|
170
179
|
%w[definitions properties patternProperties].each do |key|
|
171
180
|
next unless value = schema[key]
|
181
|
+
|
172
182
|
value.each do |k, inner_schema|
|
173
183
|
handle_schema(parent_schema, inner_schema)
|
174
184
|
end
|
@@ -177,20 +187,22 @@ module JSON
|
|
177
187
|
# Schema properties whose values are themselves schemas.
|
178
188
|
%w[additionalProperties additionalItems dependencies extends].each do |key|
|
179
189
|
next unless schema[key].is_a?(Hash)
|
190
|
+
|
180
191
|
handle_schema(parent_schema, schema[key])
|
181
192
|
end
|
182
193
|
|
183
194
|
# Schema properties whose values may be an array of schemas.
|
184
195
|
%w[allOf anyOf oneOf not].each do |key|
|
185
196
|
next unless value = schema[key]
|
197
|
+
|
186
198
|
Array(value).each do |inner_schema|
|
187
199
|
handle_schema(parent_schema, inner_schema)
|
188
200
|
end
|
189
201
|
end
|
190
202
|
|
191
203
|
# Items are always schemas
|
192
|
-
if schema[
|
193
|
-
items = schema[
|
204
|
+
if schema['items']
|
205
|
+
items = schema['items'].clone
|
194
206
|
items = [items] unless items.is_a?(Array)
|
195
207
|
|
196
208
|
items.each do |item|
|
@@ -199,10 +211,9 @@ module JSON
|
|
199
211
|
end
|
200
212
|
|
201
213
|
# Convert enum to a ArraySet
|
202
|
-
if schema[
|
203
|
-
schema[
|
214
|
+
if schema['enum'].is_a?(Array)
|
215
|
+
schema['enum'] = ArraySet.new(schema['enum'])
|
204
216
|
end
|
205
|
-
|
206
217
|
end
|
207
218
|
|
208
219
|
# Either load a reference schema or create a new schema
|
@@ -225,58 +236,57 @@ module JSON
|
|
225
236
|
@errors
|
226
237
|
end
|
227
238
|
|
228
|
-
|
229
239
|
class << self
|
230
|
-
def validate(schema, data,opts={})
|
240
|
+
def validate(schema, data, opts = {})
|
231
241
|
begin
|
232
242
|
validate!(schema, data, opts)
|
233
243
|
rescue JSON::Schema::ValidationError, JSON::Schema::SchemaError
|
234
|
-
|
244
|
+
false
|
235
245
|
end
|
236
246
|
end
|
237
247
|
|
238
|
-
def validate_json(schema, data, opts={})
|
239
|
-
validate(schema, data, opts.merge(:
|
248
|
+
def validate_json(schema, data, opts = {})
|
249
|
+
validate(schema, data, opts.merge(json: true))
|
240
250
|
end
|
241
251
|
|
242
|
-
def validate_uri(schema, data, opts={})
|
243
|
-
validate(schema, data, opts.merge(:
|
252
|
+
def validate_uri(schema, data, opts = {})
|
253
|
+
validate(schema, data, opts.merge(uri: true))
|
244
254
|
end
|
245
255
|
|
246
|
-
def validate!(schema, data,opts={})
|
247
|
-
validator = new(schema,
|
248
|
-
validator.validate
|
256
|
+
def validate!(schema, data, opts = {})
|
257
|
+
validator = new(schema, opts)
|
258
|
+
validator.validate(data)
|
249
259
|
end
|
250
260
|
|
251
|
-
def validate2(schema, data, opts={})
|
252
|
-
warn
|
261
|
+
def validate2(schema, data, opts = {})
|
262
|
+
warn '[DEPRECATION NOTICE] JSON::Validator#validate2 has been replaced by JSON::Validator#validate! and will be removed in version >= 3. Please use the #validate! method instead.'
|
253
263
|
validate!(schema, data, opts)
|
254
264
|
end
|
255
265
|
|
256
|
-
def validate_json!(schema, data, opts={})
|
257
|
-
validate!(schema, data, opts.merge(:
|
266
|
+
def validate_json!(schema, data, opts = {})
|
267
|
+
validate!(schema, data, opts.merge(json: true))
|
258
268
|
end
|
259
269
|
|
260
|
-
def validate_uri!(schema, data, opts={})
|
261
|
-
validate!(schema, data, opts.merge(:
|
270
|
+
def validate_uri!(schema, data, opts = {})
|
271
|
+
validate!(schema, data, opts.merge(uri: true))
|
262
272
|
end
|
263
273
|
|
264
|
-
def fully_validate(schema, data, opts={})
|
265
|
-
validate!(schema, data, opts.merge(:
|
274
|
+
def fully_validate(schema, data, opts = {})
|
275
|
+
validate!(schema, data, opts.merge(record_errors: true))
|
266
276
|
end
|
267
277
|
|
268
|
-
def fully_validate_schema(schema, opts={})
|
278
|
+
def fully_validate_schema(schema, opts = {})
|
269
279
|
data = schema
|
270
280
|
schema = validator_for_name(opts[:version]).metaschema
|
271
281
|
fully_validate(schema, data, opts)
|
272
282
|
end
|
273
283
|
|
274
|
-
def fully_validate_json(schema, data, opts={})
|
275
|
-
fully_validate(schema, data, opts.merge(:
|
284
|
+
def fully_validate_json(schema, data, opts = {})
|
285
|
+
fully_validate(schema, data, opts.merge(json: true))
|
276
286
|
end
|
277
287
|
|
278
|
-
def fully_validate_uri(schema, data, opts={})
|
279
|
-
fully_validate(schema, data, opts.merge(:
|
288
|
+
def fully_validate_uri(schema, data, opts = {})
|
289
|
+
fully_validate(schema, data, opts.merge(uri: true))
|
280
290
|
end
|
281
291
|
|
282
292
|
def schema_reader
|
@@ -316,7 +326,7 @@ module JSON
|
|
316
326
|
end
|
317
327
|
|
318
328
|
def cache_schemas=(val)
|
319
|
-
warn
|
329
|
+
warn '[DEPRECATION NOTICE] Schema caching is now a validation option. Schemas will still be cached if this is set to true, but this method will be removed in version >= 3. Please use the :clear_cache validation option instead.'
|
320
330
|
@@cache_schemas = val == true ? true : false
|
321
331
|
end
|
322
332
|
|
@@ -328,32 +338,34 @@ module JSON
|
|
328
338
|
@@default_validator
|
329
339
|
end
|
330
340
|
|
331
|
-
def validator_for_uri(schema_uri, raise_not_found=true)
|
341
|
+
def validator_for_uri(schema_uri, raise_not_found = true)
|
332
342
|
return default_validator unless schema_uri
|
343
|
+
|
333
344
|
u = JSON::Util::URI.parse(schema_uri)
|
334
345
|
validator = validators["#{u.scheme}://#{u.host}#{u.path}"]
|
335
346
|
if validator.nil? && raise_not_found
|
336
|
-
raise JSON::Schema::SchemaError
|
347
|
+
raise JSON::Schema::SchemaError, "Schema not found: #{schema_uri}"
|
337
348
|
else
|
338
349
|
validator
|
339
350
|
end
|
340
351
|
end
|
341
352
|
|
342
|
-
def validator_for_name(schema_name, raise_not_found=true)
|
353
|
+
def validator_for_name(schema_name, raise_not_found = true)
|
343
354
|
return default_validator unless schema_name
|
355
|
+
|
344
356
|
schema_name = schema_name.to_s
|
345
357
|
validator = validators.values.detect do |v|
|
346
358
|
Array(v.names).include?(schema_name)
|
347
359
|
end
|
348
360
|
if validator.nil? && raise_not_found
|
349
|
-
raise JSON::Schema::SchemaError
|
361
|
+
raise JSON::Schema::SchemaError, 'The requested JSON schema version is not supported'
|
350
362
|
else
|
351
363
|
validator
|
352
364
|
end
|
353
365
|
end
|
354
366
|
|
355
367
|
def validator_for(schema_uri)
|
356
|
-
warn
|
368
|
+
warn '[DEPRECATION NOTICE] JSON::Validator#validator_for has been replaced by JSON::Validator#validator_for_uri and will be removed in version >= 3. Please use the #validator_for_uri method instead.'
|
357
369
|
validator_for_uri(schema_uri)
|
358
370
|
end
|
359
371
|
|
@@ -365,7 +377,7 @@ module JSON
|
|
365
377
|
@@default_validator = v
|
366
378
|
end
|
367
379
|
|
368
|
-
def register_format_validator(format, validation_proc, versions = (@@validators.flat_map{ |k, v| v.names.first } + [nil]))
|
380
|
+
def register_format_validator(format, validation_proc, versions = (@@validators.flat_map { |k, v| v.names.first } + [nil]))
|
369
381
|
custom_format_validator = JSON::Schema::CustomFormat.new(validation_proc)
|
370
382
|
versions.each do |version|
|
371
383
|
validator = validator_for_name(version)
|
@@ -373,14 +385,14 @@ module JSON
|
|
373
385
|
end
|
374
386
|
end
|
375
387
|
|
376
|
-
def deregister_format_validator(format, versions = (@@validators.flat_map{ |k, v| v.names.first } + [nil]))
|
388
|
+
def deregister_format_validator(format, versions = (@@validators.flat_map { |k, v| v.names.first } + [nil]))
|
377
389
|
versions.each do |version|
|
378
390
|
validator = validator_for_name(version)
|
379
391
|
validator.formats[format.to_s] = validator.default_formats[format.to_s]
|
380
392
|
end
|
381
393
|
end
|
382
394
|
|
383
|
-
def restore_default_formats(versions = (@@validators.flat_map{ |k, v| v.names.first } + [nil]))
|
395
|
+
def restore_default_formats(versions = (@@validators.flat_map { |k, v| v.names.first } + [nil]))
|
384
396
|
versions.each do |version|
|
385
397
|
validator = validator_for_name(version)
|
386
398
|
validator.formats = validator.default_formats.clone
|
@@ -404,7 +416,7 @@ module JSON
|
|
404
416
|
if @@available_json_backends.include?(backend)
|
405
417
|
@@json_backend = backend
|
406
418
|
else
|
407
|
-
raise JSON::Schema::JsonParseError
|
419
|
+
raise JSON::Schema::JsonParseError, "The JSON backend '#{backend}' could not be found."
|
408
420
|
end
|
409
421
|
end
|
410
422
|
end
|
@@ -414,26 +426,26 @@ module JSON
|
|
414
426
|
begin
|
415
427
|
MultiJson.respond_to?(:adapter) ? MultiJson.load(s) : MultiJson.decode(s)
|
416
428
|
rescue MultiJson::ParseError => e
|
417
|
-
raise JSON::Schema::JsonParseError
|
429
|
+
raise JSON::Schema::JsonParseError, e.message
|
418
430
|
end
|
419
431
|
else
|
420
432
|
case @@json_backend.to_s
|
421
433
|
when 'json'
|
422
434
|
begin
|
423
|
-
JSON.parse(s, :
|
435
|
+
JSON.parse(s, quirks_mode: true)
|
424
436
|
rescue JSON::ParserError => e
|
425
|
-
raise JSON::Schema::JsonParseError
|
437
|
+
raise JSON::Schema::JsonParseError, e.message
|
426
438
|
end
|
427
439
|
when 'yajl'
|
428
440
|
begin
|
429
441
|
json = StringIO.new(s)
|
430
442
|
parser = Yajl::Parser.new
|
431
|
-
parser.parse(json) or raise
|
443
|
+
parser.parse(json) or raise(JSON::Schema::JsonParseError, 'The JSON could not be parsed by yajl')
|
432
444
|
rescue Yajl::ParseError => e
|
433
|
-
raise JSON::Schema::JsonParseError
|
445
|
+
raise JSON::Schema::JsonParseError, e.message
|
434
446
|
end
|
435
447
|
else
|
436
|
-
raise JSON::Schema::JsonParseError
|
448
|
+
raise JSON::Schema::JsonParseError, "No supported JSON parsers found. The following parsers are suported:\n * yajl-ruby\n * json"
|
437
449
|
end
|
438
450
|
end
|
439
451
|
end
|
@@ -472,7 +484,6 @@ module JSON
|
|
472
484
|
end
|
473
485
|
end
|
474
486
|
|
475
|
-
|
476
487
|
if Gem::Specification::find_all_by_name('yajl-ruby').any?
|
477
488
|
require 'yajl'
|
478
489
|
@@available_json_backends << 'yajl'
|
@@ -480,11 +491,11 @@ module JSON
|
|
480
491
|
end
|
481
492
|
|
482
493
|
if @@json_backend == 'yajl'
|
483
|
-
@@serializer = lambda{|o| Yajl::Encoder.encode(o) }
|
494
|
+
@@serializer = lambda { |o| Yajl::Encoder.encode(o) }
|
484
495
|
elsif @@json_backend == 'json'
|
485
|
-
@@serializer = lambda{|o| JSON.dump(o) }
|
496
|
+
@@serializer = lambda { |o| JSON.dump(o) }
|
486
497
|
else
|
487
|
-
@@serializer = lambda{|o| YAML.dump(o) }
|
498
|
+
@@serializer = lambda { |o| YAML.dump(o) }
|
488
499
|
end
|
489
500
|
end
|
490
501
|
end
|
@@ -493,10 +504,10 @@ module JSON
|
|
493
504
|
|
494
505
|
if Gem::Specification::find_all_by_name('uuidtools').any?
|
495
506
|
require 'uuidtools'
|
496
|
-
@@fake_uuid_generator = lambda{|s| UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, s).to_s }
|
507
|
+
@@fake_uuid_generator = lambda { |s| UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, s).to_s }
|
497
508
|
else
|
498
509
|
require 'json-schema/util/uuid'
|
499
|
-
@@fake_uuid_generator = lambda{|s| JSON::Util::UUID.create_v5(s,JSON::Util::UUID::Nil).to_s }
|
510
|
+
@@fake_uuid_generator = lambda { |s| JSON::Util::UUID.create_v5(s, JSON::Util::UUID::Nil).to_s }
|
500
511
|
end
|
501
512
|
|
502
513
|
def serialize schema
|
@@ -552,7 +563,7 @@ module JSON
|
|
552
563
|
end
|
553
564
|
self.class.add_schema(schema)
|
554
565
|
else
|
555
|
-
raise JSON::Schema::SchemaParseError,
|
566
|
+
raise JSON::Schema::SchemaParseError, 'Invalid schema - must be either a string or a hash'
|
556
567
|
end
|
557
568
|
|
558
569
|
schema
|
@@ -567,7 +578,9 @@ module JSON
|
|
567
578
|
data = self.class.parse(custom_open(json_uri))
|
568
579
|
elsif data.is_a?(String)
|
569
580
|
begin
|
570
|
-
|
581
|
+
# Check if the string is valid integer
|
582
|
+
strict_convert = data.match?(/\A[+-]?\d+\z/) && !@options[:parse_integer]
|
583
|
+
data = strict_convert ? data : self.class.parse(data)
|
571
584
|
rescue JSON::Schema::JsonParseError
|
572
585
|
begin
|
573
586
|
json_uri = Util::URI.normalized_uri(data)
|
@@ -2,27 +2,26 @@ require 'json-schema/schema/validator'
|
|
2
2
|
|
3
3
|
module JSON
|
4
4
|
class Schema
|
5
|
-
|
6
5
|
class Draft1 < Validator
|
7
6
|
def initialize
|
8
7
|
super
|
9
8
|
@attributes = {
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
9
|
+
'type' => JSON::Schema::TypeAttribute,
|
10
|
+
'disallow' => JSON::Schema::DisallowAttribute,
|
11
|
+
'format' => JSON::Schema::FormatAttribute,
|
12
|
+
'maximum' => JSON::Schema::MaximumInclusiveAttribute,
|
13
|
+
'minimum' => JSON::Schema::MinimumInclusiveAttribute,
|
14
|
+
'minItems' => JSON::Schema::MinItemsAttribute,
|
15
|
+
'maxItems' => JSON::Schema::MaxItemsAttribute,
|
16
|
+
'minLength' => JSON::Schema::MinLengthAttribute,
|
17
|
+
'maxLength' => JSON::Schema::MaxLengthAttribute,
|
18
|
+
'maxDecimal' => JSON::Schema::MaxDecimalAttribute,
|
19
|
+
'enum' => JSON::Schema::EnumAttribute,
|
20
|
+
'properties' => JSON::Schema::PropertiesOptionalAttribute,
|
21
|
+
'pattern' => JSON::Schema::PatternAttribute,
|
22
|
+
'additionalProperties' => JSON::Schema::AdditionalPropertiesAttribute,
|
23
|
+
'items' => JSON::Schema::ItemsAttribute,
|
24
|
+
'extends' => JSON::Schema::ExtendsAttribute,
|
26
25
|
}
|
27
26
|
@default_formats = {
|
28
27
|
'date-time' => DateTimeFormat,
|
@@ -30,16 +29,15 @@ module JSON
|
|
30
29
|
'time' => TimeFormat,
|
31
30
|
'ip-address' => IP4Format,
|
32
31
|
'ipv6' => IP6Format,
|
33
|
-
'uri' => UriFormat
|
32
|
+
'uri' => UriFormat,
|
34
33
|
}
|
35
34
|
@formats = @default_formats.clone
|
36
|
-
@uri = JSON::Util::URI.parse(
|
37
|
-
@names = [
|
38
|
-
@metaschema_name =
|
35
|
+
@uri = JSON::Util::URI.parse('http://json-schema.org/draft-01/schema#')
|
36
|
+
@names = ['draft1']
|
37
|
+
@metaschema_name = 'draft-01.json'
|
39
38
|
end
|
40
39
|
|
41
|
-
JSON::Validator.register_validator(
|
40
|
+
JSON::Validator.register_validator(new)
|
42
41
|
end
|
43
|
-
|
44
42
|
end
|
45
43
|
end
|
@@ -2,28 +2,27 @@ require 'json-schema/schema/validator'
|
|
2
2
|
|
3
3
|
module JSON
|
4
4
|
class Schema
|
5
|
-
|
6
5
|
class Draft2 < Validator
|
7
6
|
def initialize
|
8
7
|
super
|
9
8
|
@attributes = {
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
9
|
+
'type' => JSON::Schema::TypeAttribute,
|
10
|
+
'disallow' => JSON::Schema::DisallowAttribute,
|
11
|
+
'format' => JSON::Schema::FormatAttribute,
|
12
|
+
'maximum' => JSON::Schema::MaximumInclusiveAttribute,
|
13
|
+
'minimum' => JSON::Schema::MinimumInclusiveAttribute,
|
14
|
+
'minItems' => JSON::Schema::MinItemsAttribute,
|
15
|
+
'maxItems' => JSON::Schema::MaxItemsAttribute,
|
16
|
+
'uniqueItems' => JSON::Schema::UniqueItemsAttribute,
|
17
|
+
'minLength' => JSON::Schema::MinLengthAttribute,
|
18
|
+
'maxLength' => JSON::Schema::MaxLengthAttribute,
|
19
|
+
'divisibleBy' => JSON::Schema::DivisibleByAttribute,
|
20
|
+
'enum' => JSON::Schema::EnumAttribute,
|
21
|
+
'properties' => JSON::Schema::PropertiesOptionalAttribute,
|
22
|
+
'pattern' => JSON::Schema::PatternAttribute,
|
23
|
+
'additionalProperties' => JSON::Schema::AdditionalPropertiesAttribute,
|
24
|
+
'items' => JSON::Schema::ItemsAttribute,
|
25
|
+
'extends' => JSON::Schema::ExtendsAttribute,
|
27
26
|
}
|
28
27
|
@default_formats = {
|
29
28
|
'date-time' => DateTimeFormat,
|
@@ -31,16 +30,15 @@ module JSON
|
|
31
30
|
'time' => TimeFormat,
|
32
31
|
'ip-address' => IP4Format,
|
33
32
|
'ipv6' => IP6Format,
|
34
|
-
'uri' => UriFormat
|
33
|
+
'uri' => UriFormat,
|
35
34
|
}
|
36
35
|
@formats = @default_formats.clone
|
37
|
-
@uri = JSON::Util::URI.parse(
|
38
|
-
@names = [
|
39
|
-
@metaschema_name =
|
36
|
+
@uri = JSON::Util::URI.parse('http://json-schema.org/draft-02/schema#')
|
37
|
+
@names = ['draft2']
|
38
|
+
@metaschema_name = 'draft-02.json'
|
40
39
|
end
|
41
40
|
|
42
|
-
JSON::Validator.register_validator(
|
41
|
+
JSON::Validator.register_validator(new)
|
43
42
|
end
|
44
|
-
|
45
43
|
end
|
46
44
|
end
|