activerecord_json_validator 2.1.2 → 2.1.4

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: 79e0a6948c155c358b3345637b6df84dc3b77ac1ea22ded1db53b35eefa4ad7c
4
- data.tar.gz: 1a4c6092ee7635dd0b43767d08272a5b389c10ce9daa7ff379e89e04d4ee6766
3
+ metadata.gz: 851b94edf54cbfdcd0a66a80dab95bcdeeea0e1c4a8831f634bc55aad5cba226
4
+ data.tar.gz: bf3a8280ffa8300469e793422adc68e83ab444047db67d9e6cbafec528f69803
5
5
  SHA512:
6
- metadata.gz: 1f4f46c2db2c1b5f808fc98e003c4728b07a958a233218b1c565cb71a0bcf50118b7c1fb921735b4ee4e071f51fe458dca0e85e2267f262f30815c7b816afaac
7
- data.tar.gz: 823a2e6c404e9939976177d6c0221d2ed1d0762315ec1767721492ce764302f734c0563c4e04d2f1ccf114ac40aeeb07027550dc48f1977616df548882fcf4d7
6
+ metadata.gz: 33766dfbea4cf464c499709f76b742f3bfe9b37dcc656206a15794f93733ec6711f127057ff2efb67bb75b6e439e64e75ada856d62fcdb2fece42e4ddd81b6ff
7
+ data.tar.gz: 7192194e8663dfbd9d343b66866ebab63d843200581bbe59be2c3f3bd485819fb13bba58bfb29241afa661c180399acce0d6fa3cf24a9d6fd5852b1d300a1be8
data/README.md CHANGED
@@ -16,7 +16,7 @@
16
16
  Add this line to your application's Gemfile:
17
17
 
18
18
  ```ruby
19
- gem 'activerecord_json_validator', '~> 2.0.0'
19
+ gem 'activerecord_json_validator', '~> 2.1.0'
20
20
  ```
21
21
 
22
22
  ## Usage
@@ -70,6 +70,7 @@ user.profile_invalid_json # => '{invalid JSON":}'
70
70
  | Option | Description |
71
71
  | ---------- | ------------------------------------------------------------------------------------------------------------------------------ |
72
72
  | `:schema` | The JSON schema to validate the data against (see **Schema** section) |
73
+ | `:value` | The actual value to use when validating (see **Value** section) |
73
74
  | `:message` | The ActiveRecord message added to the record errors (see **Message** section) |
74
75
  | `:options` | A `Hash` of [`json_schemer`](https://github.com/davishmcclurg/json_schemer#options)-supported options to pass to the validator |
75
76
 
@@ -111,6 +112,34 @@ class User < ActiveRecord::Base
111
112
  end
112
113
  ```
113
114
 
115
+ ##### Value
116
+
117
+ By default, the validator will use the “getter” method to the fetch attribute
118
+ value and validate the schema against it.
119
+
120
+ ```ruby
121
+ # Will validate `self.foo`
122
+ validates :foo, json: { schema: SCHEMA }
123
+ ```
124
+
125
+ But you can change this behavior if the getter method doesn’t return raw JSON data (a `Hash`):
126
+
127
+ ```ruby
128
+ # Will validate `self[:foo]`
129
+ validates :foo, json: { schema: SCHEMA, value: ->(record, _, _) { record[:foo] } }
130
+ ```
131
+
132
+ You could also implement a “raw getter” if you want to avoid the `value` option:
133
+
134
+ ```ruby
135
+ # Will validate `self[:foo]`
136
+ validates :raw_foo, json: { schema: SCHEMA }
137
+
138
+ def raw_foo
139
+ self[:foo]
140
+ end
141
+ ```
142
+
114
143
  ##### Message
115
144
 
116
145
  Like any other ActiveModel validation, you can specify either a `Symbol` or
@@ -5,6 +5,7 @@ class JsonValidator < ActiveModel::EachValidator
5
5
  options.reverse_merge!(message: :invalid_json)
6
6
  options.reverse_merge!(schema: nil)
7
7
  options.reverse_merge!(options: {})
8
+ options.reverse_merge!(value: ->(_record, _attribute, value) { value })
8
9
  @attributes = options[:attributes]
9
10
 
10
11
  super
@@ -13,9 +14,9 @@ class JsonValidator < ActiveModel::EachValidator
13
14
  end
14
15
 
15
16
  # Validate the JSON value with a JSON schema path or String
16
- def validate_each(record, attribute, _value)
17
+ def validate_each(record, attribute, value)
17
18
  # Get the _actual_ attribute value, not the getter method value
18
- value = record[attribute]
19
+ value = options.fetch(:value).call(record, attribute, value)
19
20
 
20
21
  # Validate value with JSON Schemer
21
22
  errors = JSONSchemer.schema(schema(record), **options.fetch(:options)).validate(value).to_a
@@ -24,9 +25,10 @@ class JsonValidator < ActiveModel::EachValidator
24
25
  return if errors.empty? && record.send(:"#{attribute}_invalid_json").blank?
25
26
 
26
27
  # Add error message to the attribute
28
+ details = errors.map { |e| JSONSchemer::Errors.pretty(e) }
27
29
  message(errors).each do |error|
28
- error = error.is_a?(Hash) ? JSONSchemer::Errors.pretty(error) : error
29
- record.errors.add(attribute, error, value: value)
30
+ error = JSONSchemer::Errors.pretty(error) if error.is_a?(Hash)
31
+ record.errors.add(attribute, error, errors: details, value: value)
30
32
  end
31
33
  end
32
34
 
@@ -67,10 +69,7 @@ protected
67
69
 
68
70
  def message(errors)
69
71
  message = options.fetch(:message)
70
-
71
- case message
72
- when Proc then [message.call(errors)].flatten if message.is_a?(Proc)
73
- else [message]
74
- end
72
+ message = message.call(errors) if message.is_a?(Proc)
73
+ [message].flatten
75
74
  end
76
75
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module JSONValidator
5
- VERSION = '2.1.2'
5
+ VERSION = '2.1.4'
6
6
  end
7
7
  end
@@ -28,7 +28,7 @@ describe JsonValidator do
28
28
  serialize :other_data, JSON
29
29
  validates :data, json: { schema: schema, message: ->(errors) { errors } }
30
30
  validates :other_data, json: { schema: schema, message: ->(errors) { errors.map { |error| error['details'].to_a.flatten.join(' ') } } }
31
- validates :smart_data, json: { schema: schema, message: ->(errors) { errors } }
31
+ validates :smart_data, json: { value: ->(record, _, _) { record[:smart_data] }, schema: schema, message: ->(errors) { errors } }
32
32
 
33
33
  def smart_data
34
34
  OpenStruct.new(self[:smart_data])
@@ -48,6 +48,12 @@ describe JsonValidator do
48
48
  specify do
49
49
  expect(user).not_to be_valid
50
50
  expect(user.errors.full_messages).to eql(['Data root is missing required keys: country', 'Other data missing_keys country'])
51
+ expect(user.errors.group_by_attribute[:data].first).to have_attributes(
52
+ options: include(errors: ['root is missing required keys: country'])
53
+ )
54
+ expect(user.errors.group_by_attribute[:other_data].first).to have_attributes(
55
+ options: include(errors: ['root is missing required keys: country'])
56
+ )
51
57
  expect(user.data).to eql({ 'city' => 'Quebec City' })
52
58
  expect(user.data_invalid_json).to be_nil
53
59
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_json_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 2.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rémi Prévost
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-11-23 00:00:00.000000000 Z
11
+ date: 2023-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler