json-schema 2.6.2 → 2.7.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 59384ba05ea450b7add4dd65758c55a82615ce5a
4
- data.tar.gz: 45388f1bcb2c6a4e9dd9a4479d3cd76627929b65
3
+ metadata.gz: f440a3f87c238f6b4a3ba8fb93818ae17affd9fc
4
+ data.tar.gz: 910c2401bf9e11a0d57dfe8e47086c876e7e48b4
5
5
  SHA512:
6
- metadata.gz: c27a0a2cec5b072cc964108ae4b331ff7320b906e55d20701eb556d296080cb348e4613d6ae0719d945bc1bafe73cb298c2f9448549b0c612301224bd2dbe2c9
7
- data.tar.gz: 135eea52b5d08f17a16a98cbda09136b3846bed948cc3c2ef39b37143ac2c29acb5337a18ccfa609a602fbe01b61e133463d8ab4de663bb3b1c17cc3797af76d
6
+ metadata.gz: dbf3e34a23275021b9cf6f9d9ccb5aa86956182411f55e93fa3cd29b30d24b01d18c30e191cb31dd5a1c71b780188b08cb261d7248e203bcfa74d9b1f104f273
7
+ data.tar.gz: a5311ed16954806392eabe157ccd92d9b54b3e20e228697c31bc0042f1455c0b7e4bff6a2b33db473a93116020fdcf9a6296e621cb55638287103142e54c3839
data/README.md CHANGED
@@ -256,6 +256,24 @@ begin
256
256
  rescue TypeError => e
257
257
  e.message
258
258
  end
259
+
260
+ #
261
+ # with the `:clear_cache` option set to true, the internal cache of schemas is
262
+ # cleared after validation (otherwise schemas are cached for efficiency)
263
+ #
264
+
265
+ File.write("schema.json", v2_schema.to_json)
266
+
267
+ # => true
268
+ JSON::Validator.validate("schema.json", {})
269
+
270
+ File.write("schema.json", schema.to_json)
271
+
272
+ # => true
273
+ JSON::Validator.validate("schema.json", {}, :clear_cache => true)
274
+
275
+ # => false
276
+ JSON::Validator.validate("schema.json", {})
259
277
  ```
260
278
 
261
279
  Extending Schemas
@@ -11,7 +11,7 @@ module JSON
11
11
 
12
12
  case schema['additionalItems']
13
13
  when false
14
- if schema['items'].length != data.length
14
+ if schema['items'].length < data.length
15
15
  message = "The property '#{build_fragment(fragments)}' contains additional array elements outside of the schema when none are allowed"
16
16
  validation_error(processor, message, fragments, current_schema, self, options[:record_errors])
17
17
  end
@@ -14,7 +14,7 @@ module JSON
14
14
  if addprop.is_a?(Hash)
15
15
  matching_properties = extra_properties # & addprop.keys
16
16
  matching_properties.each do |key|
17
- additional_property_schema = JSON::Schema.new(addprop[key] || addprop, current_schema.uri, validator)
17
+ additional_property_schema = JSON::Schema.new(addprop, current_schema.uri, validator)
18
18
  additional_property_schema.validate(data[key], fragments + [key], processor, options)
19
19
  end
20
20
  extra_properties -= matching_properties
@@ -24,8 +24,6 @@ module JSON
24
24
  def self.get_extended_uri_and_schema(s, current_schema, validator)
25
25
  uri,schema = nil,nil
26
26
 
27
- s = {'$ref' => s} if s.is_a?(String)
28
-
29
27
  if s.is_a?(Hash)
30
28
  uri = current_schema.uri
31
29
  if s['$ref']
@@ -16,6 +16,7 @@ module JSON
16
16
 
17
17
  when Array
18
18
  items.each_with_index do |item_schema, i|
19
+ break if i >= data.length
19
20
  schema = JSON::Schema.new(item_schema, current_schema.uri, validator)
20
21
  schema.validate(data[i], fragments + [i.to_s], processor, options)
21
22
  end
@@ -3,9 +3,8 @@ require 'pathname'
3
3
 
4
4
  module JSON
5
5
  class Schema
6
- # Raised by {JSON::Schema::Reader} when one of its settings indicate
7
- # a schema should not be readed.
8
- class ReadRefused < StandardError
6
+ # Base for any reading exceptions encountered by {JSON::Schema::Reader}
7
+ class ReadError < StandardError
9
8
  # @return [String] the requested schema location which was refused
10
9
  attr_reader :location
11
10
 
@@ -15,7 +14,30 @@ module JSON
15
14
  def initialize(location, type)
16
15
  @location = location
17
16
  @type = type
18
- super("Read of #{type == :uri ? 'URI' : type} at #{location} refused!")
17
+ super(error_message)
18
+ end
19
+
20
+ private
21
+
22
+ def type_string
23
+ type == :uri ? 'URI' : type.to_s
24
+ end
25
+ end
26
+
27
+ # Raised by {JSON::Schema::Reader} when one of its settings indicate
28
+ # a schema should not be read.
29
+ class ReadRefused < ReadError
30
+ private
31
+ def error_message
32
+ "Read of #{type_string} at #{location} refused"
33
+ end
34
+ end
35
+
36
+ # Raised by {JSON::Schema::Reader} when an attempt to read a schema fails
37
+ class ReadFailed < ReadError
38
+ private
39
+ def error_message
40
+ "Read of #{type_string} at #{location} failed"
19
41
  end
20
42
  end
21
43
 
@@ -58,6 +80,8 @@ module JSON
58
80
  # @raise [JSON::Schema::ReadRefused] if +accept_uri+ or +accept_file+
59
81
  # indicated the schema could not be read
60
82
  # @raise [JSON::Schema::ParseError] if the schema was not a valid JSON object
83
+ # @raise [JSON::Schema::ReadFailed] if reading the location was acceptable but the
84
+ # attempt to retrieve it failed
61
85
  def read(location)
62
86
  uri = JSON::Util::URI.parse(location.to_s)
63
87
  body = if uri.scheme.nil? || uri.scheme == 'file'
@@ -98,6 +122,8 @@ module JSON
98
122
  else
99
123
  raise JSON::Schema::ReadRefused.new(uri.to_s, :uri)
100
124
  end
125
+ rescue OpenURI::HTTPError, SocketError
126
+ raise JSON::Schema::ReadFailed.new(uri.to_s, :uri)
101
127
  end
102
128
 
103
129
  def read_file(pathname)
@@ -106,6 +132,8 @@ module JSON
106
132
  else
107
133
  raise JSON::Schema::ReadRefused.new(pathname.to_s, :file)
108
134
  end
135
+ rescue Errno::ENOENT
136
+ raise JSON::Schema::ReadFailed.new(pathname.to_s, :file)
109
137
  end
110
138
  end
111
139
  end
@@ -18,7 +18,7 @@ module JSON
18
18
  class Validator
19
19
 
20
20
  @@schemas = {}
21
- @@cache_schemas = false
21
+ @@cache_schemas = true
22
22
  @@default_opts = {
23
23
  :list => false,
24
24
  :version => nil,
@@ -26,7 +26,7 @@ module JSON
26
26
  :record_errors => false,
27
27
  :errors_as_objects => false,
28
28
  :insert_defaults => false,
29
- :clear_cache => true,
29
+ :clear_cache => false,
30
30
  :strict => false,
31
31
  :parse_data => true
32
32
  }
@@ -48,7 +48,7 @@ module JSON
48
48
  @validation_options = @options[:record_errors] ? {:record_errors => true} : {}
49
49
  @validation_options[:insert_defaults] = true if @options[:insert_defaults]
50
50
  @validation_options[:strict] = true if @options[:strict] == true
51
- @validation_options[:clear_cache] = false if @options[:clear_cache] == false
51
+ @validation_options[:clear_cache] = true if !@@cache_schemas || @options[:clear_cache]
52
52
 
53
53
  @@mutex.synchronize { @base_schema = initialize_schema(schema_data) }
54
54
  @original_data = data
@@ -62,8 +62,7 @@ module JSON
62
62
  end
63
63
  metaschema = base_validator ? base_validator.metaschema : validator.metaschema
64
64
  # Don't clear the cache during metaschema validation!
65
- meta_validator = JSON::Validator.new(metaschema, @base_schema.schema, {:clear_cache => false})
66
- meta_validator.validate
65
+ self.class.validate!(metaschema, @base_schema.schema, {:clear_cache => false})
67
66
  end
68
67
 
69
68
  # If the :fragment option is set, try and validate against the fragment
@@ -110,12 +109,17 @@ module JSON
110
109
  end
111
110
 
112
111
  # Run a simple true/false validation of data against a schema
113
- def validate()
112
+ def validate
114
113
  @base_schema.validate(@data,[],self,@validation_options)
115
- if @options[:errors_as_objects]
116
- return @errors.map{|e| e.to_hash}
114
+
115
+ if @options[:record_errors]
116
+ if @options[:errors_as_objects]
117
+ @errors.map{|e| e.to_hash}
118
+ else
119
+ @errors.map{|e| e.to_string}
120
+ end
117
121
  else
118
- return @errors.map{|e| e.to_string}
122
+ true
119
123
  end
120
124
  ensure
121
125
  if @validation_options[:clear_cache] == true
@@ -239,9 +243,7 @@ module JSON
239
243
  class << self
240
244
  def validate(schema, data,opts={})
241
245
  begin
242
- validator = JSON::Validator.new(schema, data, opts)
243
- validator.validate
244
- return true
246
+ validate!(schema, data, opts)
245
247
  rescue JSON::Schema::ValidationError, JSON::Schema::SchemaError
246
248
  return false
247
249
  end
@@ -258,7 +260,6 @@ module JSON
258
260
  def validate!(schema, data,opts={})
259
261
  validator = JSON::Validator.new(schema, data, opts)
260
262
  validator.validate
261
- return true
262
263
  end
263
264
  alias_method 'validate2', 'validate!'
264
265
 
@@ -271,9 +272,7 @@ module JSON
271
272
  end
272
273
 
273
274
  def fully_validate(schema, data, opts={})
274
- opts[:record_errors] = true
275
- validator = JSON::Validator.new(schema, data, opts)
276
- validator.validate
275
+ validate!(schema, data, opts.merge(:record_errors => true))
277
276
  end
278
277
 
279
278
  def fully_validate_schema(schema, opts={})
@@ -299,7 +298,7 @@ module JSON
299
298
  end
300
299
 
301
300
  def clear_cache
302
- @@schemas = {} if @@cache_schemas == false
301
+ @@schemas = {}
303
302
  end
304
303
 
305
304
  def schemas
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.2
4
+ version: 2.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenny Hoxworth
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-13 00:00:00.000000000 Z
11
+ date: 2016-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -70,16 +70,16 @@ dependencies:
70
70
  name: addressable
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: 2.3.8
75
+ version: '2.4'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: 2.3.8
82
+ version: '2.4'
83
83
  description:
84
84
  email: hoxworth@gmail.com
85
85
  executables: []
@@ -161,7 +161,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
161
161
  requirements:
162
162
  - - ">="
163
163
  - !ruby/object:Gem::Version
164
- version: 1.8.7
164
+ version: '1.9'
165
165
  required_rubygems_version: !ruby/object:Gem::Requirement
166
166
  requirements:
167
167
  - - ">="