json-schema 3.0.0 → 4.0.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.
- checksums.yaml +4 -4
- data/README.md +2 -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 +2 -2
- 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 +4 -4
- 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 +84 -82
- 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,19 @@ 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
|
+
parse_data: true,
|
33
31
|
}
|
34
32
|
@@validators = {}
|
35
33
|
@@default_validator = nil
|
@@ -38,28 +36,26 @@ module JSON
|
|
38
36
|
@@serializer = nil
|
39
37
|
@@mutex = Mutex.new
|
40
38
|
|
41
|
-
def initialize(schema_data,
|
39
|
+
def initialize(schema_data, opts = {})
|
42
40
|
@options = @@default_opts.clone.merge(opts)
|
43
41
|
@errors = []
|
44
42
|
|
45
43
|
configured_validator = self.class.validator_for_name(@options[:version])
|
46
44
|
@options[:schema_reader] ||= self.class.schema_reader
|
47
45
|
|
48
|
-
@validation_options = @options[:record_errors] ? {:
|
46
|
+
@validation_options = @options[:record_errors] ? { record_errors: true } : {}
|
49
47
|
@validation_options[:insert_defaults] = true if @options[:insert_defaults]
|
50
48
|
@validation_options[:strict] = true if @options[:strict] == true
|
51
49
|
@validation_options[:clear_cache] = true if !@@cache_schemas || @options[:clear_cache]
|
52
50
|
|
53
51
|
@@mutex.synchronize { @base_schema = initialize_schema(schema_data, configured_validator) }
|
54
|
-
@original_data = data
|
55
|
-
@data = initialize_data(data)
|
56
52
|
@@mutex.synchronize { build_schemas(@base_schema) }
|
57
53
|
|
58
54
|
# validate the schema, if requested
|
59
55
|
if @options[:validate_schema]
|
60
56
|
# Don't clear the cache during metaschema validation!
|
61
|
-
meta_validator = self.class.new(@base_schema.validator.metaschema,
|
62
|
-
meta_validator.validate
|
57
|
+
meta_validator = self.class.new(@base_schema.validator.metaschema, { clear_cache: false })
|
58
|
+
meta_validator.validate(@base_schema.schema)
|
63
59
|
end
|
64
60
|
|
65
61
|
# If the :fragment option is set, try and validate against the fragment
|
@@ -70,11 +66,11 @@ module JSON
|
|
70
66
|
|
71
67
|
def schema_from_fragment(base_schema, fragment)
|
72
68
|
schema_uri = base_schema.uri
|
73
|
-
fragments = fragment.split(
|
69
|
+
fragments = fragment.split('/').map { |f| f.gsub('~0', '~').gsub('~1', '/') }
|
74
70
|
|
75
71
|
# ensure the first element was a hash, per the fragment spec
|
76
|
-
if fragments.shift !=
|
77
|
-
raise JSON::Schema::SchemaError
|
72
|
+
if fragments.shift != '#'
|
73
|
+
raise JSON::Schema::SchemaError, 'Invalid fragment syntax in :fragment option'
|
78
74
|
end
|
79
75
|
|
80
76
|
schema_fragment = base_schema.schema
|
@@ -88,7 +84,7 @@ module JSON
|
|
88
84
|
end
|
89
85
|
|
90
86
|
unless schema_fragment.is_a?(Hash)
|
91
|
-
raise JSON::Schema::SchemaError
|
87
|
+
raise JSON::Schema::SchemaError, 'Invalid fragment resolution for :fragment option'
|
92
88
|
end
|
93
89
|
|
94
90
|
schema = JSON::Schema.new(schema_fragment, schema_uri, base_schema.validator)
|
@@ -103,24 +99,28 @@ module JSON
|
|
103
99
|
end
|
104
100
|
|
105
101
|
# Run a simple true/false validation of data against a schema
|
106
|
-
def validate
|
107
|
-
|
102
|
+
def validate(data)
|
103
|
+
original_data = data
|
104
|
+
data = initialize_data(data)
|
105
|
+
@base_schema.validate(data, [], self, @validation_options)
|
108
106
|
|
109
107
|
if @options[:record_errors]
|
110
108
|
if @options[:errors_as_objects]
|
111
|
-
@errors.map{|e| e.to_hash}
|
109
|
+
@errors.map { |e| e.to_hash }
|
112
110
|
else
|
113
|
-
@errors.map{|e| e.to_string}
|
111
|
+
@errors.map { |e| e.to_string }
|
114
112
|
end
|
115
113
|
else
|
116
114
|
true
|
117
115
|
end
|
118
116
|
ensure
|
117
|
+
@errors = []
|
118
|
+
|
119
119
|
if @validation_options[:clear_cache] == true
|
120
120
|
self.class.clear_cache
|
121
121
|
end
|
122
122
|
if @validation_options[:insert_defaults]
|
123
|
-
self.class.merge_missing_values(
|
123
|
+
self.class.merge_missing_values(data, original_data)
|
124
124
|
end
|
125
125
|
end
|
126
126
|
|
@@ -141,13 +141,13 @@ module JSON
|
|
141
141
|
schema = parent_schema.schema
|
142
142
|
|
143
143
|
# Build ref schemas if they exist
|
144
|
-
if schema[
|
145
|
-
load_ref_schema(parent_schema, schema[
|
144
|
+
if schema['$ref']
|
145
|
+
load_ref_schema(parent_schema, schema['$ref'])
|
146
146
|
end
|
147
147
|
|
148
|
-
case schema[
|
148
|
+
case schema['extends']
|
149
149
|
when String
|
150
|
-
load_ref_schema(parent_schema, schema[
|
150
|
+
load_ref_schema(parent_schema, schema['extends'])
|
151
151
|
when Array
|
152
152
|
schema['extends'].each do |type|
|
153
153
|
handle_schema(parent_schema, type)
|
@@ -155,7 +155,7 @@ module JSON
|
|
155
155
|
end
|
156
156
|
|
157
157
|
# Check for schemas in union types
|
158
|
-
[
|
158
|
+
%w[type disallow].each do |key|
|
159
159
|
if schema[key].is_a?(Array)
|
160
160
|
schema[key].each do |type|
|
161
161
|
if type.is_a?(Hash)
|
@@ -169,6 +169,7 @@ module JSON
|
|
169
169
|
# are themselves schemas.
|
170
170
|
%w[definitions properties patternProperties].each do |key|
|
171
171
|
next unless value = schema[key]
|
172
|
+
|
172
173
|
value.each do |k, inner_schema|
|
173
174
|
handle_schema(parent_schema, inner_schema)
|
174
175
|
end
|
@@ -177,20 +178,22 @@ module JSON
|
|
177
178
|
# Schema properties whose values are themselves schemas.
|
178
179
|
%w[additionalProperties additionalItems dependencies extends].each do |key|
|
179
180
|
next unless schema[key].is_a?(Hash)
|
181
|
+
|
180
182
|
handle_schema(parent_schema, schema[key])
|
181
183
|
end
|
182
184
|
|
183
185
|
# Schema properties whose values may be an array of schemas.
|
184
186
|
%w[allOf anyOf oneOf not].each do |key|
|
185
187
|
next unless value = schema[key]
|
188
|
+
|
186
189
|
Array(value).each do |inner_schema|
|
187
190
|
handle_schema(parent_schema, inner_schema)
|
188
191
|
end
|
189
192
|
end
|
190
193
|
|
191
194
|
# Items are always schemas
|
192
|
-
if schema[
|
193
|
-
items = schema[
|
195
|
+
if schema['items']
|
196
|
+
items = schema['items'].clone
|
194
197
|
items = [items] unless items.is_a?(Array)
|
195
198
|
|
196
199
|
items.each do |item|
|
@@ -199,10 +202,9 @@ module JSON
|
|
199
202
|
end
|
200
203
|
|
201
204
|
# Convert enum to a ArraySet
|
202
|
-
if schema[
|
203
|
-
schema[
|
205
|
+
if schema['enum'].is_a?(Array)
|
206
|
+
schema['enum'] = ArraySet.new(schema['enum'])
|
204
207
|
end
|
205
|
-
|
206
208
|
end
|
207
209
|
|
208
210
|
# Either load a reference schema or create a new schema
|
@@ -225,58 +227,57 @@ module JSON
|
|
225
227
|
@errors
|
226
228
|
end
|
227
229
|
|
228
|
-
|
229
230
|
class << self
|
230
|
-
def validate(schema, data,opts={})
|
231
|
+
def validate(schema, data, opts = {})
|
231
232
|
begin
|
232
233
|
validate!(schema, data, opts)
|
233
234
|
rescue JSON::Schema::ValidationError, JSON::Schema::SchemaError
|
234
|
-
|
235
|
+
false
|
235
236
|
end
|
236
237
|
end
|
237
238
|
|
238
|
-
def validate_json(schema, data, opts={})
|
239
|
-
validate(schema, data, opts.merge(:
|
239
|
+
def validate_json(schema, data, opts = {})
|
240
|
+
validate(schema, data, opts.merge(json: true))
|
240
241
|
end
|
241
242
|
|
242
|
-
def validate_uri(schema, data, opts={})
|
243
|
-
validate(schema, data, opts.merge(:
|
243
|
+
def validate_uri(schema, data, opts = {})
|
244
|
+
validate(schema, data, opts.merge(uri: true))
|
244
245
|
end
|
245
246
|
|
246
|
-
def validate!(schema, data,opts={})
|
247
|
-
validator = new(schema,
|
248
|
-
validator.validate
|
247
|
+
def validate!(schema, data, opts = {})
|
248
|
+
validator = new(schema, opts)
|
249
|
+
validator.validate(data)
|
249
250
|
end
|
250
251
|
|
251
|
-
def validate2(schema, data, opts={})
|
252
|
-
warn
|
252
|
+
def validate2(schema, data, opts = {})
|
253
|
+
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
254
|
validate!(schema, data, opts)
|
254
255
|
end
|
255
256
|
|
256
|
-
def validate_json!(schema, data, opts={})
|
257
|
-
validate!(schema, data, opts.merge(:
|
257
|
+
def validate_json!(schema, data, opts = {})
|
258
|
+
validate!(schema, data, opts.merge(json: true))
|
258
259
|
end
|
259
260
|
|
260
|
-
def validate_uri!(schema, data, opts={})
|
261
|
-
validate!(schema, data, opts.merge(:
|
261
|
+
def validate_uri!(schema, data, opts = {})
|
262
|
+
validate!(schema, data, opts.merge(uri: true))
|
262
263
|
end
|
263
264
|
|
264
|
-
def fully_validate(schema, data, opts={})
|
265
|
-
validate!(schema, data, opts.merge(:
|
265
|
+
def fully_validate(schema, data, opts = {})
|
266
|
+
validate!(schema, data, opts.merge(record_errors: true))
|
266
267
|
end
|
267
268
|
|
268
|
-
def fully_validate_schema(schema, opts={})
|
269
|
+
def fully_validate_schema(schema, opts = {})
|
269
270
|
data = schema
|
270
271
|
schema = validator_for_name(opts[:version]).metaschema
|
271
272
|
fully_validate(schema, data, opts)
|
272
273
|
end
|
273
274
|
|
274
|
-
def fully_validate_json(schema, data, opts={})
|
275
|
-
fully_validate(schema, data, opts.merge(:
|
275
|
+
def fully_validate_json(schema, data, opts = {})
|
276
|
+
fully_validate(schema, data, opts.merge(json: true))
|
276
277
|
end
|
277
278
|
|
278
|
-
def fully_validate_uri(schema, data, opts={})
|
279
|
-
fully_validate(schema, data, opts.merge(:
|
279
|
+
def fully_validate_uri(schema, data, opts = {})
|
280
|
+
fully_validate(schema, data, opts.merge(uri: true))
|
280
281
|
end
|
281
282
|
|
282
283
|
def schema_reader
|
@@ -316,7 +317,7 @@ module JSON
|
|
316
317
|
end
|
317
318
|
|
318
319
|
def cache_schemas=(val)
|
319
|
-
warn
|
320
|
+
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
321
|
@@cache_schemas = val == true ? true : false
|
321
322
|
end
|
322
323
|
|
@@ -328,32 +329,34 @@ module JSON
|
|
328
329
|
@@default_validator
|
329
330
|
end
|
330
331
|
|
331
|
-
def validator_for_uri(schema_uri, raise_not_found=true)
|
332
|
+
def validator_for_uri(schema_uri, raise_not_found = true)
|
332
333
|
return default_validator unless schema_uri
|
334
|
+
|
333
335
|
u = JSON::Util::URI.parse(schema_uri)
|
334
336
|
validator = validators["#{u.scheme}://#{u.host}#{u.path}"]
|
335
337
|
if validator.nil? && raise_not_found
|
336
|
-
raise JSON::Schema::SchemaError
|
338
|
+
raise JSON::Schema::SchemaError, "Schema not found: #{schema_uri}"
|
337
339
|
else
|
338
340
|
validator
|
339
341
|
end
|
340
342
|
end
|
341
343
|
|
342
|
-
def validator_for_name(schema_name, raise_not_found=true)
|
344
|
+
def validator_for_name(schema_name, raise_not_found = true)
|
343
345
|
return default_validator unless schema_name
|
346
|
+
|
344
347
|
schema_name = schema_name.to_s
|
345
348
|
validator = validators.values.detect do |v|
|
346
349
|
Array(v.names).include?(schema_name)
|
347
350
|
end
|
348
351
|
if validator.nil? && raise_not_found
|
349
|
-
raise JSON::Schema::SchemaError
|
352
|
+
raise JSON::Schema::SchemaError, 'The requested JSON schema version is not supported'
|
350
353
|
else
|
351
354
|
validator
|
352
355
|
end
|
353
356
|
end
|
354
357
|
|
355
358
|
def validator_for(schema_uri)
|
356
|
-
warn
|
359
|
+
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
360
|
validator_for_uri(schema_uri)
|
358
361
|
end
|
359
362
|
|
@@ -365,7 +368,7 @@ module JSON
|
|
365
368
|
@@default_validator = v
|
366
369
|
end
|
367
370
|
|
368
|
-
def register_format_validator(format, validation_proc, versions = (@@validators.flat_map{ |k, v| v.names.first } + [nil]))
|
371
|
+
def register_format_validator(format, validation_proc, versions = (@@validators.flat_map { |k, v| v.names.first } + [nil]))
|
369
372
|
custom_format_validator = JSON::Schema::CustomFormat.new(validation_proc)
|
370
373
|
versions.each do |version|
|
371
374
|
validator = validator_for_name(version)
|
@@ -373,14 +376,14 @@ module JSON
|
|
373
376
|
end
|
374
377
|
end
|
375
378
|
|
376
|
-
def deregister_format_validator(format, versions = (@@validators.flat_map{ |k, v| v.names.first } + [nil]))
|
379
|
+
def deregister_format_validator(format, versions = (@@validators.flat_map { |k, v| v.names.first } + [nil]))
|
377
380
|
versions.each do |version|
|
378
381
|
validator = validator_for_name(version)
|
379
382
|
validator.formats[format.to_s] = validator.default_formats[format.to_s]
|
380
383
|
end
|
381
384
|
end
|
382
385
|
|
383
|
-
def restore_default_formats(versions = (@@validators.flat_map{ |k, v| v.names.first } + [nil]))
|
386
|
+
def restore_default_formats(versions = (@@validators.flat_map { |k, v| v.names.first } + [nil]))
|
384
387
|
versions.each do |version|
|
385
388
|
validator = validator_for_name(version)
|
386
389
|
validator.formats = validator.default_formats.clone
|
@@ -404,7 +407,7 @@ module JSON
|
|
404
407
|
if @@available_json_backends.include?(backend)
|
405
408
|
@@json_backend = backend
|
406
409
|
else
|
407
|
-
raise JSON::Schema::JsonParseError
|
410
|
+
raise JSON::Schema::JsonParseError, "The JSON backend '#{backend}' could not be found."
|
408
411
|
end
|
409
412
|
end
|
410
413
|
end
|
@@ -414,26 +417,26 @@ module JSON
|
|
414
417
|
begin
|
415
418
|
MultiJson.respond_to?(:adapter) ? MultiJson.load(s) : MultiJson.decode(s)
|
416
419
|
rescue MultiJson::ParseError => e
|
417
|
-
raise JSON::Schema::JsonParseError
|
420
|
+
raise JSON::Schema::JsonParseError, e.message
|
418
421
|
end
|
419
422
|
else
|
420
423
|
case @@json_backend.to_s
|
421
424
|
when 'json'
|
422
425
|
begin
|
423
|
-
JSON.parse(s, :
|
426
|
+
JSON.parse(s, quirks_mode: true)
|
424
427
|
rescue JSON::ParserError => e
|
425
|
-
raise JSON::Schema::JsonParseError
|
428
|
+
raise JSON::Schema::JsonParseError, e.message
|
426
429
|
end
|
427
430
|
when 'yajl'
|
428
431
|
begin
|
429
432
|
json = StringIO.new(s)
|
430
433
|
parser = Yajl::Parser.new
|
431
|
-
parser.parse(json) or raise
|
434
|
+
parser.parse(json) or raise(JSON::Schema::JsonParseError, 'The JSON could not be parsed by yajl')
|
432
435
|
rescue Yajl::ParseError => e
|
433
|
-
raise JSON::Schema::JsonParseError
|
436
|
+
raise JSON::Schema::JsonParseError, e.message
|
434
437
|
end
|
435
438
|
else
|
436
|
-
raise JSON::Schema::JsonParseError
|
439
|
+
raise JSON::Schema::JsonParseError, "No supported JSON parsers found. The following parsers are suported:\n * yajl-ruby\n * json"
|
437
440
|
end
|
438
441
|
end
|
439
442
|
end
|
@@ -472,7 +475,6 @@ module JSON
|
|
472
475
|
end
|
473
476
|
end
|
474
477
|
|
475
|
-
|
476
478
|
if Gem::Specification::find_all_by_name('yajl-ruby').any?
|
477
479
|
require 'yajl'
|
478
480
|
@@available_json_backends << 'yajl'
|
@@ -480,11 +482,11 @@ module JSON
|
|
480
482
|
end
|
481
483
|
|
482
484
|
if @@json_backend == 'yajl'
|
483
|
-
@@serializer = lambda{|o| Yajl::Encoder.encode(o) }
|
485
|
+
@@serializer = lambda { |o| Yajl::Encoder.encode(o) }
|
484
486
|
elsif @@json_backend == 'json'
|
485
|
-
@@serializer = lambda{|o| JSON.dump(o) }
|
487
|
+
@@serializer = lambda { |o| JSON.dump(o) }
|
486
488
|
else
|
487
|
-
@@serializer = lambda{|o| YAML.dump(o) }
|
489
|
+
@@serializer = lambda { |o| YAML.dump(o) }
|
488
490
|
end
|
489
491
|
end
|
490
492
|
end
|
@@ -493,10 +495,10 @@ module JSON
|
|
493
495
|
|
494
496
|
if Gem::Specification::find_all_by_name('uuidtools').any?
|
495
497
|
require 'uuidtools'
|
496
|
-
@@fake_uuid_generator = lambda{|s| UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, s).to_s }
|
498
|
+
@@fake_uuid_generator = lambda { |s| UUIDTools::UUID.sha1_create(UUIDTools::UUID_URL_NAMESPACE, s).to_s }
|
497
499
|
else
|
498
500
|
require 'json-schema/util/uuid'
|
499
|
-
@@fake_uuid_generator = lambda{|s| JSON::Util::UUID.create_v5(s,JSON::Util::UUID::Nil).to_s }
|
501
|
+
@@fake_uuid_generator = lambda { |s| JSON::Util::UUID.create_v5(s, JSON::Util::UUID::Nil).to_s }
|
500
502
|
end
|
501
503
|
|
502
504
|
def serialize schema
|
@@ -552,7 +554,7 @@ module JSON
|
|
552
554
|
end
|
553
555
|
self.class.add_schema(schema)
|
554
556
|
else
|
555
|
-
raise JSON::Schema::SchemaParseError,
|
557
|
+
raise JSON::Schema::SchemaParseError, 'Invalid schema - must be either a string or a hash'
|
556
558
|
end
|
557
559
|
|
558
560
|
schema
|
@@ -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
|
@@ -2,32 +2,31 @@ require 'json-schema/schema/validator'
|
|
2
2
|
|
3
3
|
module JSON
|
4
4
|
class Schema
|
5
|
-
|
6
5
|
class Draft3 < 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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
9
|
+
'type' => JSON::Schema::TypeAttribute,
|
10
|
+
'disallow' => JSON::Schema::DisallowAttribute,
|
11
|
+
'format' => JSON::Schema::FormatAttribute,
|
12
|
+
'maximum' => JSON::Schema::MaximumAttribute,
|
13
|
+
'minimum' => JSON::Schema::MinimumAttribute,
|
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::PropertiesAttribute,
|
22
|
+
'pattern' => JSON::Schema::PatternAttribute,
|
23
|
+
'patternProperties' => JSON::Schema::PatternPropertiesAttribute,
|
24
|
+
'additionalProperties' => JSON::Schema::AdditionalPropertiesAttribute,
|
25
|
+
'items' => JSON::Schema::ItemsAttribute,
|
26
|
+
'additionalItems' => JSON::Schema::AdditionalItemsAttribute,
|
27
|
+
'dependencies' => JSON::Schema::DependenciesAttribute,
|
28
|
+
'extends' => JSON::Schema::ExtendsAttribute,
|
29
|
+
'$ref' => JSON::Schema::RefAttribute,
|
31
30
|
}
|
32
31
|
@default_formats = {
|
33
32
|
'date-time' => DateTimeFormat,
|
@@ -35,16 +34,15 @@ module JSON
|
|
35
34
|
'ip-address' => IP4Format,
|
36
35
|
'ipv6' => IP6Format,
|
37
36
|
'time' => TimeFormat,
|
38
|
-
'uri' => UriFormat
|
37
|
+
'uri' => UriFormat,
|
39
38
|
}
|
40
39
|
@formats = @default_formats.clone
|
41
|
-
@uri = JSON::Util::URI.parse(
|
42
|
-
@names = [
|
43
|
-
@metaschema_name =
|
40
|
+
@uri = JSON::Util::URI.parse('http://json-schema.org/draft-03/schema#')
|
41
|
+
@names = ['draft3', 'http://json-schema.org/draft-03/schema#']
|
42
|
+
@metaschema_name = 'draft-03.json'
|
44
43
|
end
|
45
44
|
|
46
|
-
JSON::Validator.register_validator(
|
45
|
+
JSON::Validator.register_validator(new)
|
47
46
|
end
|
48
|
-
|
49
47
|
end
|
50
48
|
end
|