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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +54 -10
  3. data/lib/json-schema/attribute.rb +13 -14
  4. data/lib/json-schema/attributes/additionalitems.rb +1 -0
  5. data/lib/json-schema/attributes/additionalproperties.rb +3 -6
  6. data/lib/json-schema/attributes/allof.rb +6 -4
  7. data/lib/json-schema/attributes/anyof.rb +2 -2
  8. data/lib/json-schema/attributes/const.rb +15 -0
  9. data/lib/json-schema/attributes/dependencies.rb +1 -0
  10. data/lib/json-schema/attributes/disallow.rb +2 -1
  11. data/lib/json-schema/attributes/enum.rb +2 -2
  12. data/lib/json-schema/attributes/extends.rb +6 -6
  13. data/lib/json-schema/attributes/format.rb +2 -1
  14. data/lib/json-schema/attributes/formats/date.rb +1 -0
  15. data/lib/json-schema/attributes/formats/date_time.rb +2 -1
  16. data/lib/json-schema/attributes/formats/date_time_v4.rb +1 -0
  17. data/lib/json-schema/attributes/formats/ip.rb +1 -1
  18. data/lib/json-schema/attributes/formats/uri.rb +1 -0
  19. data/lib/json-schema/attributes/items.rb +1 -0
  20. data/lib/json-schema/attributes/limits/numeric.rb +1 -1
  21. data/lib/json-schema/attributes/maxdecimal.rb +1 -1
  22. data/lib/json-schema/attributes/not.rb +2 -2
  23. data/lib/json-schema/attributes/oneof.rb +2 -4
  24. data/lib/json-schema/attributes/patternproperties.rb +1 -0
  25. data/lib/json-schema/attributes/properties.rb +7 -7
  26. data/lib/json-schema/attributes/properties_v4.rb +1 -1
  27. data/lib/json-schema/attributes/propertynames.rb +23 -0
  28. data/lib/json-schema/attributes/ref.rb +7 -7
  29. data/lib/json-schema/attributes/required.rb +3 -2
  30. data/lib/json-schema/attributes/type.rb +3 -2
  31. data/lib/json-schema/attributes/type_v4.rb +1 -1
  32. data/lib/json-schema/errors/validation_error.rb +4 -5
  33. data/lib/json-schema/schema/reader.rb +3 -1
  34. data/lib/json-schema/schema/validator.rb +3 -3
  35. data/lib/json-schema/schema.rb +3 -4
  36. data/lib/json-schema/util/array_set.rb +1 -1
  37. data/lib/json-schema/util/uri.rb +7 -7
  38. data/lib/json-schema/util/uuid.rb +227 -226
  39. data/lib/json-schema/validator.rb +119 -114
  40. data/lib/json-schema/validators/draft1.rb +21 -23
  41. data/lib/json-schema/validators/draft2.rb +22 -24
  42. data/lib/json-schema/validators/draft3.rb +26 -28
  43. data/lib/json-schema/validators/draft4.rb +34 -36
  44. data/lib/json-schema/validators/draft6.rb +36 -36
  45. data/lib/json-schema/validators/hyper-draft1.rb +2 -3
  46. data/lib/json-schema/validators/hyper-draft2.rb +2 -3
  47. data/lib/json-schema/validators/hyper-draft3.rb +2 -3
  48. data/lib/json-schema/validators/hyper-draft4.rb +2 -3
  49. data/lib/json-schema/validators/hyper-draft6.rb +2 -3
  50. data/lib/json-schema.rb +2 -2
  51. data/resources/draft-06.json +12 -12
  52. metadata +15 -13
@@ -4,6 +4,7 @@ require 'bigdecimal'
4
4
  require 'digest/sha1'
5
5
  require 'date'
6
6
  require 'thread'
7
+ require 'timeout'
7
8
  require 'yaml'
8
9
 
9
10
  require 'json-schema/schema/reader'
@@ -14,21 +15,22 @@ require 'json-schema/errors/json_parse_error'
14
15
  require 'json-schema/util/uri'
15
16
 
16
17
  module JSON
17
-
18
18
  class Validator
19
-
20
19
  @@schemas = {}
21
20
  @@cache_schemas = true
22
21
  @@default_opts = {
23
- :list => false,
24
- :version => nil,
25
- :validate_schema => false,
26
- :record_errors => false,
27
- :errors_as_objects => false,
28
- :insert_defaults => false,
29
- :clear_cache => false,
30
- :strict => false,
31
- :parse_data => true
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,
32
34
  }
33
35
  @@validators = {}
34
36
  @@default_validator = nil
@@ -37,32 +39,32 @@ module JSON
37
39
  @@serializer = nil
38
40
  @@mutex = Mutex.new
39
41
 
40
- def initialize(schema_data, data, opts={})
42
+ def initialize(schema_data, opts = {})
41
43
  @options = @@default_opts.clone.merge(opts)
42
44
  @errors = []
43
45
 
44
- validator = self.class.validator_for_name(@options[:version])
45
- @options[:version] = validator
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] ? {:record_errors => true} : {}
49
+ @validation_options = @options[:record_errors] ? { record_errors: true } : {}
49
50
  @validation_options[:insert_defaults] = true if @options[:insert_defaults]
50
- @validation_options[:strict] = true if @options[:strict] == true
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
- @@mutex.synchronize { @base_schema = initialize_schema(schema_data) }
54
- @original_data = data
55
- @data = initialize_data(data)
60
+ @@mutex.synchronize { @base_schema = initialize_schema(schema_data, configured_validator) }
56
61
  @@mutex.synchronize { build_schemas(@base_schema) }
57
62
 
58
63
  # validate the schema, if requested
59
64
  if @options[:validate_schema]
60
- if @base_schema.schema["$schema"]
61
- base_validator = self.class.validator_for_name(@base_schema.schema["$schema"])
62
- end
63
- metaschema = base_validator ? base_validator.metaschema : validator.metaschema
64
65
  # Don't clear the cache during metaschema validation!
65
- self.class.validate!(metaschema, @base_schema.schema, {:clear_cache => false})
66
+ meta_validator = self.class.new(@base_schema.validator.metaschema, { clear_cache: false })
67
+ meta_validator.validate(@base_schema.schema)
66
68
  end
67
69
 
68
70
  # If the :fragment option is set, try and validate against the fragment
@@ -73,62 +75,61 @@ module JSON
73
75
 
74
76
  def schema_from_fragment(base_schema, fragment)
75
77
  schema_uri = base_schema.uri
76
- fragments = fragment.split("/")
78
+ fragments = fragment.split('/').map { |f| f.gsub('~0', '~').gsub('~1', '/') }
77
79
 
78
80
  # ensure the first element was a hash, per the fragment spec
79
- if fragments.shift != "#"
80
- raise JSON::Schema::SchemaError.new("Invalid fragment syntax in :fragment option")
81
+ if fragments.shift != '#'
82
+ raise JSON::Schema::SchemaError, 'Invalid fragment syntax in :fragment option'
81
83
  end
82
84
 
85
+ schema_fragment = base_schema.schema
83
86
  fragments.each do |f|
84
- if base_schema.is_a?(JSON::Schema) #test if fragment is a JSON:Schema instance
85
- if !base_schema.schema.has_key?(f)
86
- raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
87
- end
88
- base_schema = base_schema.schema[f]
89
- elsif base_schema.is_a?(Hash)
90
- if !base_schema.has_key?(f)
91
- raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
92
- end
93
- base_schema = JSON::Schema.new(base_schema[f],schema_uri,@options[:version])
94
- elsif base_schema.is_a?(Array)
95
- if base_schema[f.to_i].nil?
96
- raise JSON::Schema::SchemaError.new("Invalid fragment resolution for :fragment option")
97
- end
98
- base_schema = JSON::Schema.new(base_schema[f.to_i],schema_uri,@options[:version])
99
- else
100
- raise JSON::Schema::SchemaError.new("Invalid schema encountered when resolving :fragment option")
87
+ case schema_fragment
88
+ when Hash
89
+ schema_fragment = schema_fragment[f]
90
+ when Array
91
+ schema_fragment = schema_fragment[f.to_i]
101
92
  end
102
93
  end
103
94
 
95
+ unless schema_fragment.is_a?(Hash)
96
+ raise JSON::Schema::SchemaError, 'Invalid fragment resolution for :fragment option'
97
+ end
98
+
99
+ schema = JSON::Schema.new(schema_fragment, schema_uri, base_schema.validator)
100
+
104
101
  if @options[:list]
105
- base_schema.to_array_schema
106
- elsif base_schema.is_a?(Hash)
107
- JSON::Schema.new(base_schema, schema_uri, @options[:version])
102
+ schema.to_array_schema
103
+ elsif schema.is_a?(Hash)
104
+ JSON::Schema.new(schema, schema_uri, @options[:version])
108
105
  else
109
- base_schema
106
+ schema
110
107
  end
111
108
  end
112
109
 
113
110
  # Run a simple true/false validation of data against a schema
114
- def validate
115
- @base_schema.validate(@data,[],self,@validation_options)
111
+ def validate(data)
112
+ original_data = data
113
+ data = initialize_data(data)
114
+ @base_schema.validate(data, [], self, @validation_options)
116
115
 
117
116
  if @options[:record_errors]
118
117
  if @options[:errors_as_objects]
119
- @errors.map{|e| e.to_hash}
118
+ @errors.map { |e| e.to_hash }
120
119
  else
121
- @errors.map{|e| e.to_string}
120
+ @errors.map { |e| e.to_string }
122
121
  end
123
122
  else
124
123
  true
125
124
  end
126
125
  ensure
126
+ @errors = []
127
+
127
128
  if @validation_options[:clear_cache] == true
128
129
  self.class.clear_cache
129
130
  end
130
131
  if @validation_options[:insert_defaults]
131
- self.class.merge_missing_values(@data, @original_data)
132
+ self.class.merge_missing_values(data, original_data)
132
133
  end
133
134
  end
134
135
 
@@ -149,13 +150,13 @@ module JSON
149
150
  schema = parent_schema.schema
150
151
 
151
152
  # Build ref schemas if they exist
152
- if schema["$ref"]
153
- load_ref_schema(parent_schema, schema["$ref"])
153
+ if schema['$ref']
154
+ load_ref_schema(parent_schema, schema['$ref'])
154
155
  end
155
156
 
156
- case schema["extends"]
157
+ case schema['extends']
157
158
  when String
158
- load_ref_schema(parent_schema, schema["extends"])
159
+ load_ref_schema(parent_schema, schema['extends'])
159
160
  when Array
160
161
  schema['extends'].each do |type|
161
162
  handle_schema(parent_schema, type)
@@ -163,7 +164,7 @@ module JSON
163
164
  end
164
165
 
165
166
  # Check for schemas in union types
166
- ["type", "disallow"].each do |key|
167
+ %w[type disallow].each do |key|
167
168
  if schema[key].is_a?(Array)
168
169
  schema[key].each do |type|
169
170
  if type.is_a?(Hash)
@@ -177,6 +178,7 @@ module JSON
177
178
  # are themselves schemas.
178
179
  %w[definitions properties patternProperties].each do |key|
179
180
  next unless value = schema[key]
181
+
180
182
  value.each do |k, inner_schema|
181
183
  handle_schema(parent_schema, inner_schema)
182
184
  end
@@ -185,20 +187,22 @@ module JSON
185
187
  # Schema properties whose values are themselves schemas.
186
188
  %w[additionalProperties additionalItems dependencies extends].each do |key|
187
189
  next unless schema[key].is_a?(Hash)
190
+
188
191
  handle_schema(parent_schema, schema[key])
189
192
  end
190
193
 
191
194
  # Schema properties whose values may be an array of schemas.
192
195
  %w[allOf anyOf oneOf not].each do |key|
193
196
  next unless value = schema[key]
197
+
194
198
  Array(value).each do |inner_schema|
195
199
  handle_schema(parent_schema, inner_schema)
196
200
  end
197
201
  end
198
202
 
199
203
  # Items are always schemas
200
- if schema["items"]
201
- items = schema["items"].clone
204
+ if schema['items']
205
+ items = schema['items'].clone
202
206
  items = [items] unless items.is_a?(Array)
203
207
 
204
208
  items.each do |item|
@@ -207,10 +211,9 @@ module JSON
207
211
  end
208
212
 
209
213
  # Convert enum to a ArraySet
210
- if schema["enum"].is_a?(Array)
211
- schema["enum"] = ArraySet.new(schema["enum"])
214
+ if schema['enum'].is_a?(Array)
215
+ schema['enum'] = ArraySet.new(schema['enum'])
212
216
  end
213
-
214
217
  end
215
218
 
216
219
  # Either load a reference schema or create a new schema
@@ -233,58 +236,57 @@ module JSON
233
236
  @errors
234
237
  end
235
238
 
236
-
237
239
  class << self
238
- def validate(schema, data,opts={})
240
+ def validate(schema, data, opts = {})
239
241
  begin
240
242
  validate!(schema, data, opts)
241
243
  rescue JSON::Schema::ValidationError, JSON::Schema::SchemaError
242
- return false
244
+ false
243
245
  end
244
246
  end
245
247
 
246
- def validate_json(schema, data, opts={})
247
- validate(schema, data, opts.merge(:json => true))
248
+ def validate_json(schema, data, opts = {})
249
+ validate(schema, data, opts.merge(json: true))
248
250
  end
249
251
 
250
- def validate_uri(schema, data, opts={})
251
- validate(schema, data, opts.merge(:uri => true))
252
+ def validate_uri(schema, data, opts = {})
253
+ validate(schema, data, opts.merge(uri: true))
252
254
  end
253
255
 
254
- def validate!(schema, data,opts={})
255
- validator = new(schema, data, opts)
256
- validator.validate
256
+ def validate!(schema, data, opts = {})
257
+ validator = new(schema, opts)
258
+ validator.validate(data)
257
259
  end
258
260
 
259
- def validate2(schema, data, opts={})
260
- 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."
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.'
261
263
  validate!(schema, data, opts)
262
264
  end
263
265
 
264
- def validate_json!(schema, data, opts={})
265
- validate!(schema, data, opts.merge(:json => true))
266
+ def validate_json!(schema, data, opts = {})
267
+ validate!(schema, data, opts.merge(json: true))
266
268
  end
267
269
 
268
- def validate_uri!(schema, data, opts={})
269
- validate!(schema, data, opts.merge(:uri => true))
270
+ def validate_uri!(schema, data, opts = {})
271
+ validate!(schema, data, opts.merge(uri: true))
270
272
  end
271
273
 
272
- def fully_validate(schema, data, opts={})
273
- validate!(schema, data, opts.merge(:record_errors => true))
274
+ def fully_validate(schema, data, opts = {})
275
+ validate!(schema, data, opts.merge(record_errors: true))
274
276
  end
275
277
 
276
- def fully_validate_schema(schema, opts={})
278
+ def fully_validate_schema(schema, opts = {})
277
279
  data = schema
278
280
  schema = validator_for_name(opts[:version]).metaschema
279
281
  fully_validate(schema, data, opts)
280
282
  end
281
283
 
282
- def fully_validate_json(schema, data, opts={})
283
- fully_validate(schema, data, opts.merge(:json => true))
284
+ def fully_validate_json(schema, data, opts = {})
285
+ fully_validate(schema, data, opts.merge(json: true))
284
286
  end
285
287
 
286
- def fully_validate_uri(schema, data, opts={})
287
- fully_validate(schema, data, opts.merge(:uri => true))
288
+ def fully_validate_uri(schema, data, opts = {})
289
+ fully_validate(schema, data, opts.merge(uri: true))
288
290
  end
289
291
 
290
292
  def schema_reader
@@ -324,7 +326,7 @@ module JSON
324
326
  end
325
327
 
326
328
  def cache_schemas=(val)
327
- 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."
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.'
328
330
  @@cache_schemas = val == true ? true : false
329
331
  end
330
332
 
@@ -336,32 +338,34 @@ module JSON
336
338
  @@default_validator
337
339
  end
338
340
 
339
- def validator_for_uri(schema_uri, raise_not_found=true)
341
+ def validator_for_uri(schema_uri, raise_not_found = true)
340
342
  return default_validator unless schema_uri
343
+
341
344
  u = JSON::Util::URI.parse(schema_uri)
342
345
  validator = validators["#{u.scheme}://#{u.host}#{u.path}"]
343
346
  if validator.nil? && raise_not_found
344
- raise JSON::Schema::SchemaError.new("Schema not found: #{schema_uri}")
347
+ raise JSON::Schema::SchemaError, "Schema not found: #{schema_uri}"
345
348
  else
346
349
  validator
347
350
  end
348
351
  end
349
352
 
350
- def validator_for_name(schema_name, raise_not_found=true)
353
+ def validator_for_name(schema_name, raise_not_found = true)
351
354
  return default_validator unless schema_name
355
+
352
356
  schema_name = schema_name.to_s
353
357
  validator = validators.values.detect do |v|
354
358
  Array(v.names).include?(schema_name)
355
359
  end
356
360
  if validator.nil? && raise_not_found
357
- raise JSON::Schema::SchemaError.new("The requested JSON schema version is not supported")
361
+ raise JSON::Schema::SchemaError, 'The requested JSON schema version is not supported'
358
362
  else
359
363
  validator
360
364
  end
361
365
  end
362
366
 
363
367
  def validator_for(schema_uri)
364
- 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."
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.'
365
369
  validator_for_uri(schema_uri)
366
370
  end
367
371
 
@@ -373,7 +377,7 @@ module JSON
373
377
  @@default_validator = v
374
378
  end
375
379
 
376
- 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]))
377
381
  custom_format_validator = JSON::Schema::CustomFormat.new(validation_proc)
378
382
  versions.each do |version|
379
383
  validator = validator_for_name(version)
@@ -381,14 +385,14 @@ module JSON
381
385
  end
382
386
  end
383
387
 
384
- 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]))
385
389
  versions.each do |version|
386
390
  validator = validator_for_name(version)
387
391
  validator.formats[format.to_s] = validator.default_formats[format.to_s]
388
392
  end
389
393
  end
390
394
 
391
- 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]))
392
396
  versions.each do |version|
393
397
  validator = validator_for_name(version)
394
398
  validator.formats = validator.default_formats.clone
@@ -412,7 +416,7 @@ module JSON
412
416
  if @@available_json_backends.include?(backend)
413
417
  @@json_backend = backend
414
418
  else
415
- raise JSON::Schema::JsonParseError.new("The JSON backend '#{backend}' could not be found.")
419
+ raise JSON::Schema::JsonParseError, "The JSON backend '#{backend}' could not be found."
416
420
  end
417
421
  end
418
422
  end
@@ -422,26 +426,26 @@ module JSON
422
426
  begin
423
427
  MultiJson.respond_to?(:adapter) ? MultiJson.load(s) : MultiJson.decode(s)
424
428
  rescue MultiJson::ParseError => e
425
- raise JSON::Schema::JsonParseError.new(e.message)
429
+ raise JSON::Schema::JsonParseError, e.message
426
430
  end
427
431
  else
428
432
  case @@json_backend.to_s
429
433
  when 'json'
430
434
  begin
431
- JSON.parse(s, :quirks_mode => true)
435
+ JSON.parse(s, quirks_mode: true)
432
436
  rescue JSON::ParserError => e
433
- raise JSON::Schema::JsonParseError.new(e.message)
437
+ raise JSON::Schema::JsonParseError, e.message
434
438
  end
435
439
  when 'yajl'
436
440
  begin
437
441
  json = StringIO.new(s)
438
442
  parser = Yajl::Parser.new
439
- parser.parse(json) or raise JSON::Schema::JsonParseError.new("The JSON could not be parsed by yajl")
443
+ parser.parse(json) or raise(JSON::Schema::JsonParseError, 'The JSON could not be parsed by yajl')
440
444
  rescue Yajl::ParseError => e
441
- raise JSON::Schema::JsonParseError.new(e.message)
445
+ raise JSON::Schema::JsonParseError, e.message
442
446
  end
443
447
  else
444
- raise JSON::Schema::JsonParseError.new("No supported JSON parsers found. The following parsers are suported:\n * yajl-ruby\n * json")
448
+ raise JSON::Schema::JsonParseError, "No supported JSON parsers found. The following parsers are suported:\n * yajl-ruby\n * json"
445
449
  end
446
450
  end
447
451
  end
@@ -480,7 +484,6 @@ module JSON
480
484
  end
481
485
  end
482
486
 
483
-
484
487
  if Gem::Specification::find_all_by_name('yajl-ruby').any?
485
488
  require 'yajl'
486
489
  @@available_json_backends << 'yajl'
@@ -488,11 +491,11 @@ module JSON
488
491
  end
489
492
 
490
493
  if @@json_backend == 'yajl'
491
- @@serializer = lambda{|o| Yajl::Encoder.encode(o) }
494
+ @@serializer = lambda { |o| Yajl::Encoder.encode(o) }
492
495
  elsif @@json_backend == 'json'
493
- @@serializer = lambda{|o| JSON.dump(o) }
496
+ @@serializer = lambda { |o| JSON.dump(o) }
494
497
  else
495
- @@serializer = lambda{|o| YAML.dump(o) }
498
+ @@serializer = lambda { |o| YAML.dump(o) }
496
499
  end
497
500
  end
498
501
  end
@@ -501,10 +504,10 @@ module JSON
501
504
 
502
505
  if Gem::Specification::find_all_by_name('uuidtools').any?
503
506
  require 'uuidtools'
504
- @@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 }
505
508
  else
506
509
  require 'json-schema/util/uuid'
507
- @@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 }
508
511
  end
509
512
 
510
513
  def serialize schema
@@ -519,12 +522,12 @@ module JSON
519
522
  @@fake_uuid_generator.call(schema)
520
523
  end
521
524
 
522
- def initialize_schema(schema)
525
+ def initialize_schema(schema, default_validator)
523
526
  if schema.is_a?(String)
524
527
  begin
525
528
  # Build a fake URI for this
526
529
  schema_uri = JSON::Util::URI.parse(fake_uuid(schema))
527
- schema = JSON::Schema.new(self.class.parse(schema), schema_uri, @options[:version])
530
+ schema = JSON::Schema.new(JSON::Validator.parse(schema), schema_uri, default_validator)
528
531
  if @options[:list] && @options[:fragment].nil?
529
532
  schema = schema.to_array_schema
530
533
  end
@@ -554,13 +557,13 @@ module JSON
554
557
  elsif schema.is_a?(Hash)
555
558
  schema_uri = JSON::Util::URI.parse(fake_uuid(serialize(schema)))
556
559
  schema = JSON::Schema.stringify(schema)
557
- schema = JSON::Schema.new(schema, schema_uri, @options[:version])
560
+ schema = JSON::Schema.new(schema, schema_uri, default_validator)
558
561
  if @options[:list] && @options[:fragment].nil?
559
562
  schema = schema.to_array_schema
560
563
  end
561
564
  self.class.add_schema(schema)
562
565
  else
563
- raise JSON::Schema::SchemaParseError, "Invalid schema - must be either a string or a hash"
566
+ raise JSON::Schema::SchemaParseError, 'Invalid schema - must be either a string or a hash'
564
567
  end
565
568
 
566
569
  schema
@@ -575,7 +578,9 @@ module JSON
575
578
  data = self.class.parse(custom_open(json_uri))
576
579
  elsif data.is_a?(String)
577
580
  begin
578
- data = self.class.parse(data)
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)
579
584
  rescue JSON::Schema::JsonParseError
580
585
  begin
581
586
  json_uri = Util::URI.normalized_uri(data)
@@ -593,7 +598,7 @@ module JSON
593
598
  uri = Util::URI.normalized_uri(uri) if uri.is_a?(String)
594
599
  if uri.absolute? && Util::URI::SUPPORTED_PROTOCOLS.include?(uri.scheme)
595
600
  begin
596
- open(uri.to_s).read
601
+ URI.open(uri.to_s).read
597
602
  rescue OpenURI::HTTPError, Timeout::Error => e
598
603
  raise JSON::Schema::JsonLoadError, e.message
599
604
  end
@@ -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
- "type" => JSON::Schema::TypeAttribute,
11
- "disallow" => JSON::Schema::DisallowAttribute,
12
- "format" => JSON::Schema::FormatAttribute,
13
- "maximum" => JSON::Schema::MaximumInclusiveAttribute,
14
- "minimum" => JSON::Schema::MinimumInclusiveAttribute,
15
- "minItems" => JSON::Schema::MinItemsAttribute,
16
- "maxItems" => JSON::Schema::MaxItemsAttribute,
17
- "minLength" => JSON::Schema::MinLengthAttribute,
18
- "maxLength" => JSON::Schema::MaxLengthAttribute,
19
- "maxDecimal" => JSON::Schema::MaxDecimalAttribute,
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
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("http://json-schema.org/draft-01/schema#")
37
- @names = ["draft1"]
38
- @metaschema_name = "draft-01.json"
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(self.new)
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
- "type" => JSON::Schema::TypeAttribute,
11
- "disallow" => JSON::Schema::DisallowAttribute,
12
- "format" => JSON::Schema::FormatAttribute,
13
- "maximum" => JSON::Schema::MaximumInclusiveAttribute,
14
- "minimum" => JSON::Schema::MinimumInclusiveAttribute,
15
- "minItems" => JSON::Schema::MinItemsAttribute,
16
- "maxItems" => JSON::Schema::MaxItemsAttribute,
17
- "uniqueItems" => JSON::Schema::UniqueItemsAttribute,
18
- "minLength" => JSON::Schema::MinLengthAttribute,
19
- "maxLength" => JSON::Schema::MaxLengthAttribute,
20
- "divisibleBy" => JSON::Schema::DivisibleByAttribute,
21
- "enum" => JSON::Schema::EnumAttribute,
22
- "properties" => JSON::Schema::PropertiesOptionalAttribute,
23
- "pattern" => JSON::Schema::PatternAttribute,
24
- "additionalProperties" => JSON::Schema::AdditionalPropertiesAttribute,
25
- "items" => JSON::Schema::ItemsAttribute,
26
- "extends" => JSON::Schema::ExtendsAttribute
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("http://json-schema.org/draft-02/schema#")
38
- @names = ["draft2"]
39
- @metaschema_name = "draft-02.json"
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(self.new)
41
+ JSON::Validator.register_validator(new)
43
42
  end
44
-
45
43
  end
46
44
  end