activerecord_json_validator 1.3.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -6
- data/activerecord_json_validator.gemspec +1 -1
- data/lib/active_record/json_validator/validator.rb +3 -9
- data/lib/active_record/json_validator/version.rb +1 -1
- data/lib/activerecord_json_validator.rb +1 -1
- data/spec/json_validator_spec.rb +9 -23
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f5b518bda8ab28520f561dc840f47223e252530a9182ef31a164c12fb831451
|
4
|
+
data.tar.gz: 79e01fcce3c8cb0e27f6e3e24df4db54a312f6cef7285f38cec05689e897d042
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b4e5420f0c6aaad0e1e13ed296bc598e88b16974bfbaa7e55cc1680ad4ca499ba0b570de3ee26d72a993cbb9e712370f7e23fcf14e56e46ba40d7c601c5695f1
|
7
|
+
data.tar.gz: 93cf5acde18b71f512f2f68eaec1ba8191a721e1ace7853604d9e6ac4c6c7d03ae88a19c402a4c8b8545e8bfa5546de4ced97dfa032536f18747a5658419b752
|
data/README.md
CHANGED
@@ -22,16 +22,31 @@ gem 'activerecord_json_validator'
|
|
22
22
|
## Usage
|
23
23
|
|
24
24
|
### JSON Schema
|
25
|
+
Schemas must use be a JSON string or use string keys.
|
25
26
|
|
26
27
|
```json
|
27
|
-
{
|
28
|
+
'{
|
28
29
|
"type": "object",
|
29
|
-
"$schema": "http://json-schema.org/draft-04/schema",
|
30
|
+
"$schema": "http://json-schema.org/draft-04/schema#",
|
30
31
|
"properties": {
|
31
32
|
"city": { "type": "string" },
|
32
33
|
"country": { "type": "string" }
|
33
34
|
},
|
34
35
|
"required": ["country"]
|
36
|
+
}'
|
37
|
+
```
|
38
|
+
|
39
|
+
or
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
{
|
43
|
+
"type" => "object",
|
44
|
+
"$schema" => "http://json-schema.org/draft-04/schema#",
|
45
|
+
"properties" => {
|
46
|
+
"city" => { "type" => "string" },
|
47
|
+
"country" => { "type" => "string" }
|
48
|
+
},
|
49
|
+
"required" => ["country"]
|
35
50
|
}
|
36
51
|
```
|
37
52
|
|
@@ -69,13 +84,12 @@ user.profile_invalid_json # => '{invalid JSON":}'
|
|
69
84
|
|------------|-----------------------------------------------------
|
70
85
|
| `:schema` | The JSON schema to validate the data against (see **Schema** section)
|
71
86
|
| `:message` | The ActiveRecord message added to the record errors (see **Message** section)
|
72
|
-
| `:options` | A `Hash` of [`
|
87
|
+
| `:options` | A `Hash` of [`json_schemer`](https://github.com/davishmcclurg/json_schemer#options)-supported options to pass to the validator
|
73
88
|
|
74
89
|
##### Schema
|
75
90
|
|
76
|
-
`ActiveRecord::JSONValidator` uses the
|
77
|
-
data against a JSON schema.
|
78
|
-
`JSON::Validator.validate` would take as the `schema` argument.
|
91
|
+
`ActiveRecord::JSONValidator` uses the [json_schemer](https://github.com/davishmcclurg/json_schemer) gem to validate the JSON
|
92
|
+
data against a JSON schema.
|
79
93
|
|
80
94
|
Additionally, you can use a `Symbol` or a `Proc`. Both will be executed in the
|
81
95
|
context of the validated record (`Symbol` will be sent as a method and the
|
@@ -27,6 +27,6 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.add_development_dependency 'phare'
|
28
28
|
spec.add_development_dependency 'rubocop', '~> 0.28'
|
29
29
|
|
30
|
-
spec.add_dependency '
|
30
|
+
spec.add_dependency 'json_schemer', '~> 0.2.18'
|
31
31
|
spec.add_dependency 'activerecord', '>= 4.2.0', '< 7'
|
32
32
|
end
|
@@ -14,8 +14,8 @@ class JsonValidator < ActiveModel::EachValidator
|
|
14
14
|
|
15
15
|
# Validate the JSON value with a JSON schema path or String
|
16
16
|
def validate_each(record, attribute, value)
|
17
|
-
# Validate value with JSON
|
18
|
-
errors =
|
17
|
+
# Validate value with JSON Schemer
|
18
|
+
errors = JSONSchemer.schema(schema(record), options.fetch(:options)).validate(value).to_a
|
19
19
|
|
20
20
|
# Everything is good if we don’t have any errors and we got valid JSON value
|
21
21
|
return if errors.empty? && record.send(:"#{attribute}_invalid_json").blank?
|
@@ -49,7 +49,7 @@ protected
|
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
|
-
# Return a valid schema
|
52
|
+
# Return a valid schema, recursively calling
|
53
53
|
# itself until it gets a non-Proc/non-Symbol value.
|
54
54
|
def schema(record, schema = nil)
|
55
55
|
schema ||= options.fetch(:schema)
|
@@ -61,12 +61,6 @@ protected
|
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
|
-
def validatable_value(value)
|
65
|
-
return value if value.is_a?(String)
|
66
|
-
|
67
|
-
::ActiveSupport::JSON.encode(value)
|
68
|
-
end
|
69
|
-
|
70
64
|
def message(errors)
|
71
65
|
message = options.fetch(:message)
|
72
66
|
|
data/spec/json_validator_spec.rb
CHANGED
@@ -42,7 +42,7 @@ describe JsonValidator do
|
|
42
42
|
|
43
43
|
describe :validate_each do
|
44
44
|
let(:validator) { JsonValidator.new(options) }
|
45
|
-
let(:options) { { attributes: [attribute], options: {
|
45
|
+
let(:options) { { attributes: [attribute], options: { format: true } } }
|
46
46
|
let(:validate_each!) { validator.validate_each(record, attribute, value) }
|
47
47
|
|
48
48
|
# Doubles
|
@@ -51,16 +51,18 @@ describe JsonValidator do
|
|
51
51
|
let(:record_errors) { double(:errors) }
|
52
52
|
let(:value) { double(:value) }
|
53
53
|
let(:schema) { double(:schema) }
|
54
|
-
let(:
|
54
|
+
let(:schema_validator) { double(:schema_validator) }
|
55
|
+
let(:raw_errors) { double(:raw_errors) }
|
55
56
|
let(:validator_errors) { double(:validator_errors) }
|
56
57
|
|
57
58
|
before do
|
58
59
|
expect(validator).to receive(:schema).with(record).and_return(schema)
|
59
|
-
expect(
|
60
|
-
expect(
|
60
|
+
expect(JSONSchemer).to receive(:schema).with(schema, options[:options]).and_return(schema_validator)
|
61
|
+
expect(schema_validator).to receive(:validate).with(value).and_return(raw_errors)
|
62
|
+
expect(raw_errors).to receive(:to_a).and_return(validator_errors)
|
61
63
|
end
|
62
64
|
|
63
|
-
context 'with JSON
|
65
|
+
context 'with JSON Schemer errors' do
|
64
66
|
before do
|
65
67
|
expect(validator_errors).to receive(:empty?).and_return(false)
|
66
68
|
expect(record).not_to receive(:"#{attribute}_invalid_json")
|
@@ -70,7 +72,7 @@ describe JsonValidator do
|
|
70
72
|
specify { validate_each! }
|
71
73
|
end
|
72
74
|
|
73
|
-
context 'without JSON
|
75
|
+
context 'without JSON Schemer errors but with invalid JSON data' do
|
74
76
|
before do
|
75
77
|
expect(validator_errors).to receive(:empty?).and_return(true)
|
76
78
|
expect(record).to receive(:"#{attribute}_invalid_json").and_return('foo"{]')
|
@@ -80,7 +82,7 @@ describe JsonValidator do
|
|
80
82
|
specify { validate_each! }
|
81
83
|
end
|
82
84
|
|
83
|
-
context 'without JSON
|
85
|
+
context 'without JSON Schemer errors and valid JSON data' do
|
84
86
|
before do
|
85
87
|
expect(validator_errors).to receive(:empty?).and_return(true)
|
86
88
|
expect(record).to receive(:"#{attribute}_invalid_json").and_return(nil)
|
@@ -155,22 +157,6 @@ describe JsonValidator do
|
|
155
157
|
end
|
156
158
|
end
|
157
159
|
|
158
|
-
describe :validatable_value do
|
159
|
-
let(:validator) { JsonValidator.new(options) }
|
160
|
-
let(:options) { { attributes: [:foo] } }
|
161
|
-
let(:validatable_value) { validator.send(:validatable_value, value) }
|
162
|
-
|
163
|
-
context 'with non-String value' do
|
164
|
-
let(:value) { { foo: 'bar' } }
|
165
|
-
it { expect(validatable_value).to eql('{"foo":"bar"}') }
|
166
|
-
end
|
167
|
-
|
168
|
-
context 'with String value' do
|
169
|
-
let(:value) { '{\"foo\":\"bar\"}' }
|
170
|
-
it { expect(validatable_value).to eql(value) }
|
171
|
-
end
|
172
|
-
end
|
173
|
-
|
174
160
|
describe :message do
|
175
161
|
let(:validator) { JsonValidator.new(options) }
|
176
162
|
let(:options) { { attributes: [:foo], message: message_option } }
|
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:
|
4
|
+
version: 2.0.0
|
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:
|
11
|
+
date: 2021-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -129,19 +129,19 @@ dependencies:
|
|
129
129
|
- !ruby/object:Gem::Version
|
130
130
|
version: '0.28'
|
131
131
|
- !ruby/object:Gem::Dependency
|
132
|
-
name:
|
132
|
+
name: json_schemer
|
133
133
|
requirement: !ruby/object:Gem::Requirement
|
134
134
|
requirements:
|
135
135
|
- - "~>"
|
136
136
|
- !ruby/object:Gem::Version
|
137
|
-
version:
|
137
|
+
version: 0.2.18
|
138
138
|
type: :runtime
|
139
139
|
prerelease: false
|
140
140
|
version_requirements: !ruby/object:Gem::Requirement
|
141
141
|
requirements:
|
142
142
|
- - "~>"
|
143
143
|
- !ruby/object:Gem::Version
|
144
|
-
version:
|
144
|
+
version: 0.2.18
|
145
145
|
- !ruby/object:Gem::Dependency
|
146
146
|
name: activerecord
|
147
147
|
requirement: !ruby/object:Gem::Requirement
|
@@ -211,7 +211,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
211
211
|
- !ruby/object:Gem::Version
|
212
212
|
version: '0'
|
213
213
|
requirements: []
|
214
|
-
rubygems_version: 3.
|
214
|
+
rubygems_version: 3.1.6
|
215
215
|
signing_key:
|
216
216
|
specification_version: 4
|
217
217
|
summary: ActiveRecord::JSONValidator makes it easy to validate JSON attributes with
|