activerecord_json_validator 2.1.2 → 2.1.3
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 +4 -4
- data/README.md +29 -0
- data/lib/active_record/json_validator/validator.rb +3 -2
- data/lib/active_record/json_validator/version.rb +1 -1
- data/spec/json_validator_spec.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6f85c2656b56c867aa06f583528e3d315026c0537369311d6302adaeda42ad6
|
4
|
+
data.tar.gz: 34894b9ed770db328de8011be097b72f59bb08431b45ddeb3024af01d1cf29b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db0d5e91cbbefb083b7c55d2633ef82e87868943a54dd09254f27b4ab85a5d6474eaa277ab0e23ad988a6b7f6bfc35ca870a0b735d9007b32ef5ce638fe9e198
|
7
|
+
data.tar.gz: a5cc28a84fd2037ec6623fce0cbbb665723a8481b89afb07139c46e6c7a8a047c885dfb1848aca667542196cd0e6c29fabf81081ad7b98c633644dbe4af4de7b
|
data/README.md
CHANGED
@@ -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,
|
17
|
+
def validate_each(record, attribute, value)
|
17
18
|
# Get the _actual_ attribute value, not the getter method value
|
18
|
-
value = record
|
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
|
data/spec/json_validator_spec.rb
CHANGED
@@ -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])
|
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.
|
4
|
+
version: 2.1.3
|
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-
|
11
|
+
date: 2022-11-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|