json_schemer 0.2.8 → 0.2.9

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ac13fecc37f430b393d875cd262bf5186974b42573f50b8e8b5d90298e41916f
4
- data.tar.gz: 388e7eeda51e99d0454c3832f04f342b69102982d7b559bc37c92ab312332c67
3
+ metadata.gz: 6849b76f5d056da509ff2cf6961f8727d84fd00e4f0dd8c8e843cb4efaa5dbb1
4
+ data.tar.gz: 28a1573883545ffe2cf48cf5d80f0ea76c357556d9373e286a92d545f3f50afe
5
5
  SHA512:
6
- metadata.gz: 9372627c0131ecfcf88904050f0b85e1fa7e3f3ca1152ce20be7750146e0804b59d199540a560f04df6d912aba02229016432d9c2015008804f7cbe8b949034d
7
- data.tar.gz: d9ea545392112f48393d78508ef383e807ffe34c041018bf461cfce51b46f638a57b3c8fd8b498beb8f3d4a1573cfc6719b16e4231d47639e7cc21e6d607d981
6
+ metadata.gz: 19a38974481d9f13512bb9d2df317b0529cd25171e298019eeb6ab216e535b1c4ab15f196f8ab94b86825818df87d2b5d5498dc92077658a3920e30398d44003
7
+ data.tar.gz: 0bbebb72912b7ce4686699941b4ade4f734f1b728cb94a20a8a320f4b23a0950d047b7068ebcdc61ee81304e534989eb91113ed35e378a6f49426444e15b0517
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_schemer (0.2.8)
4
+ json_schemer (0.2.9)
5
5
  ecma-re-validator (~> 0.2)
6
6
  hana (~> 1.3)
7
7
  regexp_parser (~> 1.5)
@@ -28,4 +28,4 @@ DEPENDENCIES
28
28
  rake (~> 10.0)
29
29
 
30
30
  BUNDLED WITH
31
- 2.0.2
31
+ 2.1.4
@@ -4,15 +4,16 @@ module JSONSchemer
4
4
  class Base
5
5
  include Format
6
6
 
7
- Instance = Struct.new(:data, :data_pointer, :schema, :schema_pointer, :parent_uri) do
7
+ Instance = Struct.new(:data, :data_pointer, :schema, :schema_pointer, :parent_uri, :insert_property_defaults) do
8
8
  def merge(
9
9
  data: self.data,
10
10
  data_pointer: self.data_pointer,
11
11
  schema: self.schema,
12
12
  schema_pointer: self.schema_pointer,
13
- parent_uri: self.parent_uri
13
+ parent_uri: self.parent_uri,
14
+ insert_property_defaults: self.insert_property_defaults
14
15
  )
15
- self.class.new(data, data_pointer, schema, schema_pointer, parent_uri)
16
+ self.class.new(data, data_pointer, schema, schema_pointer, parent_uri, insert_property_defaults)
16
17
  end
17
18
  end
18
19
 
@@ -46,11 +47,11 @@ module JSONSchemer
46
47
  end
47
48
 
48
49
  def valid?(data)
49
- valid_instance?(Instance.new(data, '', root, '', nil))
50
+ valid_instance?(Instance.new(data, '', root, '', nil, !!@insert_property_defaults))
50
51
  end
51
52
 
52
53
  def validate(data)
53
- validate_instance(Instance.new(data, '', root, '', nil))
54
+ validate_instance(Instance.new(data, '', root, '', nil, !!@insert_property_defaults))
54
55
  end
55
56
 
56
57
  protected
@@ -115,20 +116,35 @@ module JSONSchemer
115
116
 
116
117
  if all_of
117
118
  all_of.each_with_index do |subschema, index|
118
- validate_instance(instance.merge(schema: subschema, schema_pointer: "#{instance.schema_pointer}/allOf/#{index}"), &block)
119
+ subinstance = instance.merge(
120
+ schema: subschema,
121
+ schema_pointer: "#{instance.schema_pointer}/allOf/#{index}",
122
+ insert_property_defaults: false
123
+ )
124
+ validate_instance(subinstance, &block)
119
125
  end
120
126
  end
121
127
 
122
128
  if any_of
123
129
  subschemas = any_of.lazy.with_index.map do |subschema, index|
124
- validate_instance(instance.merge(schema: subschema, schema_pointer: "#{instance.schema_pointer}/anyOf/#{index}"))
130
+ subinstance = instance.merge(
131
+ schema: subschema,
132
+ schema_pointer: "#{instance.schema_pointer}/anyOf/#{index}",
133
+ insert_property_defaults: false
134
+ )
135
+ validate_instance(subinstance)
125
136
  end
126
137
  subschemas.each { |subschema| subschema.each(&block) } unless subschemas.any?(&:none?)
127
138
  end
128
139
 
129
140
  if one_of
130
141
  subschemas = one_of.map.with_index do |subschema, index|
131
- validate_instance(instance.merge(schema: subschema, schema_pointer: "#{instance.schema_pointer}/oneOf/#{index}"))
142
+ subinstance = instance.merge(
143
+ schema: subschema,
144
+ schema_pointer: "#{instance.schema_pointer}/oneOf/#{index}",
145
+ insert_property_defaults: false
146
+ )
147
+ validate_instance(subinstance)
132
148
  end
133
149
  valid_subschema_count = subschemas.count(&:none?)
134
150
  if valid_subschema_count > 1
@@ -139,11 +155,15 @@ module JSONSchemer
139
155
  end
140
156
 
141
157
  unless not_schema.nil?
142
- subinstance = instance.merge(schema: not_schema, schema_pointer: "#{instance.schema_pointer}/not")
158
+ subinstance = instance.merge(
159
+ schema: not_schema,
160
+ schema_pointer: "#{instance.schema_pointer}/not",
161
+ insert_property_defaults: false
162
+ )
143
163
  yield error(subinstance, 'not') if valid_instance?(subinstance)
144
164
  end
145
165
 
146
- if if_schema && valid_instance?(instance.merge(schema: if_schema))
166
+ if if_schema && valid_instance?(instance.merge(schema: if_schema, insert_property_defaults: false))
147
167
  validate_instance(instance.merge(schema: then_schema, schema_pointer: "#{instance.schema_pointer}/then"), &block) unless then_schema.nil?
148
168
  elsif if_schema
149
169
  validate_instance(instance.merge(schema: else_schema, schema_pointer: "#{instance.schema_pointer}/else"), &block) unless else_schema.nil?
@@ -179,10 +199,6 @@ module JSONSchemer
179
199
  !!@format
180
200
  end
181
201
 
182
- def insert_property_defaults?
183
- !!@insert_property_defaults
184
- end
185
-
186
202
  def custom_format?(format)
187
203
  !!(formats && formats.key?(format))
188
204
  end
@@ -195,7 +211,6 @@ module JSONSchemer
195
211
  JSONSchemer.schema(
196
212
  schema,
197
213
  format: format?,
198
- insert_property_defaults: insert_property_defaults?,
199
214
  formats: formats,
200
215
  keywords: keywords,
201
216
  ref_resolver: ref_resolver
@@ -250,27 +265,32 @@ module JSONSchemer
250
265
  end
251
266
 
252
267
  def validate_ref(instance, ref, &block)
253
- ref_uri = join_uri(instance.parent_uri, ref)
254
-
255
- if valid_json_pointer?(ref_uri.fragment)
256
- ref_pointer = Hana::Pointer.new(URI.decode_www_form_component(ref_uri.fragment))
257
- if ref.start_with?('#')
268
+ if ref.start_with?('#')
269
+ schema_pointer = ref.slice(1..-1)
270
+ if valid_json_pointer?(schema_pointer)
271
+ ref_pointer = Hana::Pointer.new(URI.decode_www_form_component(schema_pointer))
258
272
  subinstance = instance.merge(
259
273
  schema: ref_pointer.eval(root),
260
- schema_pointer: ref_uri.fragment,
261
- parent_uri: (pointer_uri(root, ref_pointer) || ref_uri)
274
+ schema_pointer: schema_pointer,
275
+ parent_uri: (pointer_uri(root, ref_pointer) || instance.parent_uri)
262
276
  )
263
277
  validate_instance(subinstance, &block)
264
- else
265
- ref_root = resolve_ref(ref_uri)
266
- ref_object = child(ref_root)
267
- subinstance = instance.merge(
268
- schema: ref_pointer.eval(ref_root),
269
- schema_pointer: ref_uri.fragment,
270
- parent_uri: (pointer_uri(ref_root, ref_pointer) || ref_uri)
271
- )
272
- ref_object.validate_instance(subinstance, &block)
278
+ return
273
279
  end
280
+ end
281
+
282
+ ref_uri = join_uri(instance.parent_uri, ref)
283
+
284
+ if valid_json_pointer?(ref_uri.fragment)
285
+ ref_pointer = Hana::Pointer.new(URI.decode_www_form_component(ref_uri.fragment))
286
+ ref_root = resolve_ref(ref_uri)
287
+ ref_object = child(ref_root)
288
+ subinstance = instance.merge(
289
+ schema: ref_pointer.eval(ref_root),
290
+ schema_pointer: ref_uri.fragment,
291
+ parent_uri: (pointer_uri(ref_root, ref_pointer) || ref_uri)
292
+ )
293
+ ref_object.validate_instance(subinstance, &block)
274
294
  elsif id = ids[ref_uri.to_s]
275
295
  subinstance = instance.merge(
276
296
  schema: id.fetch(:schema),
@@ -467,7 +487,7 @@ module JSONSchemer
467
487
  dependencies = schema['dependencies']
468
488
  property_names = schema['propertyNames']
469
489
 
470
- if insert_property_defaults? && properties
490
+ if instance.insert_property_defaults && properties
471
491
  properties.each do |property, property_schema|
472
492
  if !data.key?(property) && property_schema.is_a?(Hash) && property_schema.key?('default')
473
493
  data[property] = property_schema.fetch('default').clone
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module JSONSchemer
3
- VERSION = '0.2.8'
3
+ VERSION = '0.2.9'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_schemer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Harsha
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-20 00:00:00.000000000 Z
11
+ date: 2020-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -382,7 +382,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
382
382
  - !ruby/object:Gem::Version
383
383
  version: '0'
384
384
  requirements: []
385
- rubygems_version: 3.0.3
385
+ rubyforge_project:
386
+ rubygems_version: 2.7.6
386
387
  signing_key:
387
388
  specification_version: 4
388
389
  summary: JSON Schema validator. Supports drafts 4, 6, and 7.