json_schemer 0.2.15 → 0.2.16

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: 14785b9586c0f152587eedbdea3da0a37f8a766b21db28821ffa374fef6d6235
4
- data.tar.gz: fe50adb42c6b0da48fc43b7c8ebee40e61a42f4ccfa3d87de6723551388b223c
3
+ metadata.gz: 0cc2fef0eeaaab0e1664a9fa5aa39c08ef2e1fbd8fb68d202bf1b50b7c0840ee
4
+ data.tar.gz: 438c507a7d8ffa674d34d22c647af0852eb031e02c491d274cc5d4dec5c185dd
5
5
  SHA512:
6
- metadata.gz: e23c122d31d6a9c38326c90c1ac9af35148ce0b13ff1a576f2fb9bfff7cfdd289d2122b73d5b8b20330d8323e8ec98b11452ec0b70ccd339a26e6dba4d7f33b5
7
- data.tar.gz: a4be1485304ae45f125e6b73494c5a1a5cf98d092c66b25dd2ff81fed0a74833165d8655abc46799faa97af0372d9d3f06e4a5e30554b0c31260b01699aa76be
6
+ metadata.gz: 84ba1ea8a43459ff6c5c835fd5f0aedaf00f8077ecd3ec0aefe88322f09ef868712702122790a56f30c33bb1ab3f2585bd7e9a57c94eb07f553d845322095ae8
7
+ data.tar.gz: 6709666d3f2cf98ba1b2f06712e90f564d33d6e861b15d9ff2cb3c109d9b3ca0816b456f22b837a18062824429c97b8d7ebeb9fbaaa75b67bb203dec8f56b310
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_schemer (0.2.15)
4
+ json_schemer (0.2.16)
5
5
  ecma-re-validator (~> 0.2)
6
6
  hana (~> 1.3)
7
7
  regexp_parser (~> 1.5)
data/README.md CHANGED
@@ -92,6 +92,20 @@ JSONSchemer.schema(
92
92
  # default: false
93
93
  insert_property_defaults: true,
94
94
 
95
+ # modify properties during validation. You can pass one Proc or a list of Procs to modify data.
96
+ # Proc/[Proc]
97
+ # default: nil
98
+ before_property_validation: proc do |data, property, property_schema, _parent|
99
+ data[property] ||= 42
100
+ end,
101
+
102
+ # modify properties after validation. You can pass one Proc or a list of Procs to modify data.
103
+ # Proc/[Proc]
104
+ # default: nil
105
+ after_property_validation: proc do |data, property, property_schema, _parent|
106
+ data[property] = Date.iso8601(data[property]) if property_schema.is_a?(Hash) && property_schema['format'] == 'date'
107
+ end,
108
+
95
109
  # resolve external references
96
110
  # 'net/http'/proc/lambda/respond_to?(:call)
97
111
  # 'net/http': proc { |uri| JSON.parse(Net::HTTP.get(uri)) }
@@ -4,16 +4,17 @@ module JSONSchemer
4
4
  class Base
5
5
  include Format
6
6
 
7
- Instance = Struct.new(:data, :data_pointer, :schema, :schema_pointer, :parent_uri, :insert_property_defaults) do
7
+ Instance = Struct.new(:data, :data_pointer, :schema, :schema_pointer, :parent_uri, :before_property_validation, :after_property_validation) 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
13
  parent_uri: self.parent_uri,
14
- insert_property_defaults: self.insert_property_defaults
14
+ before_property_validation: self.before_property_validation,
15
+ after_property_validation: self.after_property_validation
15
16
  )
16
- self.class.new(data, data_pointer, schema, schema_pointer, parent_uri, insert_property_defaults)
17
+ self.class.new(data, data_pointer, schema, schema_pointer, parent_uri, before_property_validation, after_property_validation)
17
18
  end
18
19
  end
19
20
 
@@ -29,10 +30,18 @@ module JSONSchemer
29
30
  :eol => '\z'
30
31
  }.freeze
31
32
 
33
+ INSERT_DEFAULT_PROPERTY = proc do |data, property, property_schema, _parent|
34
+ if !data.key?(property) && property_schema.is_a?(Hash) && property_schema.key?('default')
35
+ data[property] = property_schema.fetch('default').clone
36
+ end
37
+ end
38
+
32
39
  def initialize(
33
40
  schema,
34
41
  format: true,
35
42
  insert_property_defaults: false,
43
+ before_property_validation: nil,
44
+ after_property_validation: nil,
36
45
  formats: nil,
37
46
  keywords: nil,
38
47
  ref_resolver: DEFAULT_REF_RESOLVER
@@ -40,18 +49,20 @@ module JSONSchemer
40
49
  raise InvalidSymbolKey, 'schemas must use string keys' if schema.is_a?(Hash) && !schema.empty? && !schema.first.first.is_a?(String)
41
50
  @root = schema
42
51
  @format = format
43
- @insert_property_defaults = insert_property_defaults
52
+ @before_property_validation = [*before_property_validation]
53
+ @before_property_validation.unshift(INSERT_DEFAULT_PROPERTY) if insert_property_defaults
54
+ @after_property_validation = [*after_property_validation]
44
55
  @formats = formats
45
56
  @keywords = keywords
46
57
  @ref_resolver = ref_resolver == 'net/http' ? CachedRefResolver.new(&NET_HTTP_REF_RESOLVER) : ref_resolver
47
58
  end
48
59
 
49
60
  def valid?(data)
50
- valid_instance?(Instance.new(data, '', root, '', nil, !!@insert_property_defaults))
61
+ valid_instance?(Instance.new(data, '', root, '', nil, @before_property_validation, @after_property_validation))
51
62
  end
52
63
 
53
64
  def validate(data)
54
- validate_instance(Instance.new(data, '', root, '', nil, !!@insert_property_defaults))
65
+ validate_instance(Instance.new(data, '', root, '', nil, @before_property_validation, @after_property_validation))
55
66
  end
56
67
 
57
68
  protected
@@ -101,7 +112,7 @@ module JSONSchemer
101
112
  if keywords
102
113
  keywords.each do |keyword, callable|
103
114
  if schema.key?(keyword)
104
- result = callable.call(data, schema, instance.pointer)
115
+ result = callable.call(data, schema, instance.data_pointer)
105
116
  if result.is_a?(Array)
106
117
  result.each(&block)
107
118
  elsif !result
@@ -119,7 +130,8 @@ module JSONSchemer
119
130
  subinstance = instance.merge(
120
131
  schema: subschema,
121
132
  schema_pointer: "#{instance.schema_pointer}/allOf/#{index}",
122
- insert_property_defaults: false
133
+ before_property_validation: false,
134
+ after_property_validation: false
123
135
  )
124
136
  validate_instance(subinstance, &block)
125
137
  end
@@ -130,7 +142,8 @@ module JSONSchemer
130
142
  subinstance = instance.merge(
131
143
  schema: subschema,
132
144
  schema_pointer: "#{instance.schema_pointer}/anyOf/#{index}",
133
- insert_property_defaults: false
145
+ before_property_validation: false,
146
+ after_property_validation: false
134
147
  )
135
148
  validate_instance(subinstance)
136
149
  end
@@ -142,7 +155,8 @@ module JSONSchemer
142
155
  subinstance = instance.merge(
143
156
  schema: subschema,
144
157
  schema_pointer: "#{instance.schema_pointer}/oneOf/#{index}",
145
- insert_property_defaults: false
158
+ before_property_validation: false,
159
+ after_property_validation: false
146
160
  )
147
161
  validate_instance(subinstance)
148
162
  end
@@ -158,12 +172,13 @@ module JSONSchemer
158
172
  subinstance = instance.merge(
159
173
  schema: not_schema,
160
174
  schema_pointer: "#{instance.schema_pointer}/not",
161
- insert_property_defaults: false
175
+ before_property_validation: false,
176
+ after_property_validation: false
162
177
  )
163
178
  yield error(subinstance, 'not') if valid_instance?(subinstance)
164
179
  end
165
180
 
166
- if if_schema && valid_instance?(instance.merge(schema: if_schema, insert_property_defaults: false))
181
+ if if_schema && valid_instance?(instance.merge(schema: if_schema, before_property_validation: false, after_property_validation: false))
167
182
  validate_instance(instance.merge(schema: then_schema, schema_pointer: "#{instance.schema_pointer}/then"), &block) unless then_schema.nil?
168
183
  elsif if_schema
169
184
  validate_instance(instance.merge(schema: else_schema, schema_pointer: "#{instance.schema_pointer}/else"), &block) unless else_schema.nil?
@@ -487,10 +502,10 @@ module JSONSchemer
487
502
  dependencies = schema['dependencies']
488
503
  property_names = schema['propertyNames']
489
504
 
490
- if instance.insert_property_defaults && properties
505
+ if instance.before_property_validation && properties
491
506
  properties.each do |property, property_schema|
492
- if !data.key?(property) && property_schema.is_a?(Hash) && property_schema.key?('default')
493
- data[property] = property_schema.fetch('default').clone
507
+ instance.before_property_validation.each do |hook|
508
+ hook.call(data, property, property_schema, schema)
494
509
  end
495
510
  end
496
511
  end
@@ -565,6 +580,14 @@ module JSONSchemer
565
580
  validate_instance(subinstance, &block)
566
581
  end
567
582
  end
583
+
584
+ if instance.after_property_validation && properties
585
+ properties.each do |property, property_schema|
586
+ instance.after_property_validation.each do |hook|
587
+ hook.call(data, property, property_schema, schema)
588
+ end
589
+ end
590
+ end
568
591
  end
569
592
 
570
593
  def safe_strict_decode64(data)
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module JSONSchemer
3
- VERSION = '0.2.15'
3
+ VERSION = '0.2.16'
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.15
4
+ version: 0.2.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Harsha
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-03 00:00:00.000000000 Z
11
+ date: 2020-10-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler