activerecord_json_validator 2.0.0 → 2.1.1

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: 0f5b518bda8ab28520f561dc840f47223e252530a9182ef31a164c12fb831451
4
- data.tar.gz: 79e01fcce3c8cb0e27f6e3e24df4db54a312f6cef7285f38cec05689e897d042
3
+ metadata.gz: 1ebef8de77a6adb6c1ea4ff00c49c9e91b843952c4d226f4b149c04df9d585a6
4
+ data.tar.gz: 7efc4fa80a8dc82343bf1c7148e20f06df2acf127cbdbf9bc98e591e706940db
5
5
  SHA512:
6
- metadata.gz: b4e5420f0c6aaad0e1e13ed296bc598e88b16974bfbaa7e55cc1680ad4ca499ba0b570de3ee26d72a993cbb9e712370f7e23fcf14e56e46ba40d7c601c5695f1
7
- data.tar.gz: 93cf5acde18b71f512f2f68eaec1ba8191a721e1ace7853604d9e6ac4c6c7d03ae88a19c402a4c8b8545e8bfa5546de4ced97dfa032536f18747a5658419b752
6
+ metadata.gz: 1f096c0becc38bcdfb81cf70f59174509ea463f088ae02e514c612b397200aaef4dc76e7fd5cfe44ff0bf7c17fe2715a77d41d02101bac55afad19bbb076497a
7
+ data.tar.gz: e94f0da67f515a20eeb5699be8823c267144eeb36d09c6a1a892d24faab9bc65ca8d2d70edbe4388f088505d75875ede5cd91366393f2b71d42675713f8423ed
data/.travis.yml CHANGED
@@ -3,16 +3,24 @@ language: ruby
3
3
  rvm:
4
4
  - 2.4.6
5
5
  - 2.6.3
6
+ - 2.7.5
6
7
 
7
8
  gemfile:
8
9
  - gemfiles/Gemfile.activerecord-4.2.x
9
10
  - gemfiles/Gemfile.activerecord-5.0.x
10
11
  - gemfiles/Gemfile.activerecord-6.0.x
12
+ - gemfiles/Gemfile.activerecord-7.0.x
11
13
 
12
14
  matrix:
13
15
  exclude:
14
16
  - gemfile: gemfiles/Gemfile.activerecord-6.0.x
15
17
  rvm: 2.4.6
18
+ - gemfile: gemfiles/Gemfile.activerecord-7.0.x
19
+ rvm: 2.4.6
20
+ - gemfile: gemfiles/Gemfile.activerecord-7.0.x
21
+ rvm: 2.6.3
22
+ - gemfile: gemfiles/Gemfile.activerecord-4.2.x
23
+ rvm: 2.7.5
16
24
 
17
25
  sudo: false
18
26
 
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  <p align="center">
2
2
  <a href="https://github.com/mirego/activerecord_json_validator">
3
- <img src="https://cloud.githubusercontent.com/assets/11348/6099354/cffcf35e-afc3-11e4-9a4d-d872941bbcf6.png" alt="" />
3
+ <img src="https://user-images.githubusercontent.com/11348/126779905-3468eb15-d554-46d5-925b-235f68169d86.png" alt="" />
4
4
  </a>
5
5
  <br />
6
6
  <code>ActiveRecord::JSONValidator</code> makes it easy to validate<br /> JSON attributes against a <a href="http://json-schema.org/">JSON schema</a>.
@@ -16,16 +16,17 @@
16
16
  Add this line to your application's Gemfile:
17
17
 
18
18
  ```ruby
19
- gem 'activerecord_json_validator'
19
+ gem 'activerecord_json_validator', '~> 2.0.0'
20
20
  ```
21
21
 
22
22
  ## Usage
23
23
 
24
24
  ### JSON Schema
25
- Schemas must use be a JSON string or use string keys.
25
+
26
+ Schemas should be a JSON file
26
27
 
27
28
  ```json
28
- '{
29
+ {
29
30
  "type": "object",
30
31
  "$schema": "http://json-schema.org/draft-04/schema#",
31
32
  "properties": {
@@ -33,20 +34,6 @@ Schemas must use be a JSON string or use string keys.
33
34
  "country": { "type": "string" }
34
35
  },
35
36
  "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"]
50
37
  }
51
38
  ```
52
39
 
@@ -60,7 +47,7 @@ end
60
47
 
61
48
  class User < ActiveRecord::Base
62
49
  # Constants
63
- PROFILE_JSON_SCHEMA = Rails.root.join('config', 'schemas', 'profile.json_schema').to_s
50
+ PROFILE_JSON_SCHEMA = Rails.root.join('config', 'schemas', 'profile.json')
64
51
 
65
52
  # Validations
66
53
  validates :name, presence: true
@@ -80,16 +67,16 @@ user.profile_invalid_json # => '{invalid JSON":}'
80
67
 
81
68
  #### Options
82
69
 
83
- | Option | Description
84
- |------------|-----------------------------------------------------
85
- | `:schema` | The JSON schema to validate the data against (see **Schema** section)
86
- | `:message` | The ActiveRecord message added to the record errors (see **Message** section)
87
- | `:options` | A `Hash` of [`json_schemer`](https://github.com/davishmcclurg/json_schemer#options)-supported options to pass to the validator
70
+ | Option | Description |
71
+ | ---------- | ------------------------------------------------------------------------------------------------------------------------------ |
72
+ | `:schema` | The JSON schema to validate the data against (see **Schema** section) |
73
+ | `:message` | The ActiveRecord message added to the record errors (see **Message** section) |
74
+ | `:options` | A `Hash` of [`json_schemer`](https://github.com/davishmcclurg/json_schemer#options)-supported options to pass to the validator |
88
75
 
89
76
  ##### Schema
90
77
 
91
78
  `ActiveRecord::JSONValidator` uses the [json_schemer](https://github.com/davishmcclurg/json_schemer) gem to validate the JSON
92
- data against a JSON schema.
79
+ data against a JSON schema.
93
80
 
94
81
  Additionally, you can use a `Symbol` or a `Proc`. Both will be executed in the
95
82
  context of the validated record (`Symbol` will be sent as a method and the
@@ -98,8 +85,8 @@ context of the validated record (`Symbol` will be sent as a method and the
98
85
  ```ruby
99
86
  class User < ActiveRecord::Base
100
87
  # Constants
101
- PROFILE_REGULAR_JSON_SCHEMA = Rails.root.join('config', 'schemas', 'profile.json_schema').to_s
102
- PROFILE_ADMIN_JSON_SCHEMA = Rails.root.join('config', 'schemas', 'profile_admin.json_schema').to_s
88
+ PROFILE_REGULAR_JSON_SCHEMA = Rails.root.join('config', 'schemas', 'profile.json_schema')
89
+ PROFILE_ADMIN_JSON_SCHEMA = Rails.root.join('config', 'schemas', 'profile_admin.json_schema')
103
90
 
104
91
  # Validations
105
92
  validates :profile, presence: true, json: { schema: lambda { dynamic_profile_schema } } # `schema: :dynamic_profile_schema` would also work
@@ -136,7 +123,7 @@ user.errors.full_messages
136
123
 
137
124
  ## License
138
125
 
139
- `ActiveRecord::JSONValidator` is © 2013-2016 [Mirego](http://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/activerecord_json_validator/blob/master/LICENSE.md) file.
126
+ `ActiveRecord::JSONValidator` is © 2013-2016 [Mirego](http://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/activerecord_json_validator/blob/master/LICENSE.md) file.
140
127
 
141
128
  The tree logo is based on [this lovely icon](http://thenounproject.com/term/tree/51004/) by [Sara Quintana](http://thenounproject.com/sara.quintana.75), from The Noun Project. Used under a [Creative Commons BY 3.0](http://creativecommons.org/licenses/by/3.0/) license.
142
129
 
@@ -18,15 +18,15 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency 'bundler', '~> 1.12'
21
+ spec.add_development_dependency 'bundler', '>= 1.12'
22
22
  spec.add_development_dependency 'rake'
23
23
  spec.add_development_dependency 'rspec', '~> 3.5'
24
24
  spec.add_development_dependency 'pg'
25
25
  spec.add_development_dependency 'mysql2'
26
- spec.add_development_dependency 'activesupport', '>= 4.2.0', '< 7'
26
+ spec.add_development_dependency 'activesupport', '>= 4.2.0', '< 8'
27
27
  spec.add_development_dependency 'phare'
28
28
  spec.add_development_dependency 'rubocop', '~> 0.28'
29
29
 
30
30
  spec.add_dependency 'json_schemer', '~> 0.2.18'
31
- spec.add_dependency 'activerecord', '>= 4.2.0', '< 7'
31
+ spec.add_dependency 'activerecord', '>= 4.2.0', '< 8'
32
32
  end
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec path: '../'
4
+
5
+ gem 'activerecord', '~> 7.0.0.rc1'
@@ -15,13 +15,14 @@ class JsonValidator < ActiveModel::EachValidator
15
15
  # Validate the JSON value with a JSON schema path or String
16
16
  def validate_each(record, attribute, value)
17
17
  # Validate value with JSON Schemer
18
- errors = JSONSchemer.schema(schema(record), options.fetch(:options)).validate(value).to_a
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?
22
22
 
23
23
  # Add error message to the attribute
24
24
  message(errors).each do |error|
25
+ error = error.is_a?(Hash) ? JSONSchemer::Errors.pretty(error) : error
25
26
  record.errors.add(attribute, error, value: value)
26
27
  end
27
28
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module JSONValidator
5
- VERSION = '2.0.0'
5
+ VERSION = '2.1.1'
6
6
  end
7
7
  end
@@ -4,107 +4,51 @@
4
4
  require 'spec_helper'
5
5
 
6
6
  describe JsonValidator do
7
- describe :initialize do
8
- # NOTE: We do not explicitely call `JsonValidator.new` in the tests,
9
- # because we let Rails (ActiveModel::Validations) do that when we call
10
- # `validates … json: true` on the model.
11
- #
12
- # This allows us to test the constructor behavior when executed in
13
- # different Rails versions that do not pass the same arguments to it.
7
+ describe :validate_each do
14
8
  before do
15
9
  run_migration do
16
10
  create_table(:users, force: true) do |t|
17
- t.string :name
18
11
  t.text :data
19
12
  end
20
13
  end
21
14
 
22
15
  spawn_model 'User' do
16
+ schema = '
17
+ {
18
+ "type": "object",
19
+ "properties": {
20
+ "city": { "type": "string" },
21
+ "country": { "type": "string" }
22
+ },
23
+ "required": ["country"]
24
+ }
25
+ '
23
26
  serialize :data, JSON
24
- validates :data, json: true
27
+ serialize :other_data, JSON
28
+ validates :data, json: { schema: schema, message: ->(errors) { errors } }
29
+ validates :other_data, json: { schema: schema, message: ->(errors) { errors.map { |error| error['details'].to_a.flatten.join(' ') } } }
25
30
  end
26
-
27
- record.data = data
28
- end
29
-
30
- let(:record) { User.new }
31
-
32
- context 'with valid JSON data' do
33
- let(:data) { 'What? This is not JSON at all.' }
34
- it { expect(record.data_invalid_json).to eql(data) }
35
- end
36
-
37
- context 'with invalid JSON data' do
38
- let(:data) { { foo: 'bar' } }
39
- it { expect(record.data_invalid_json).to be_nil }
40
31
  end
41
- end
42
32
 
43
- describe :validate_each do
44
- let(:validator) { JsonValidator.new(options) }
45
- let(:options) { { attributes: [attribute], options: { format: true } } }
46
- let(:validate_each!) { validator.validate_each(record, attribute, value) }
47
-
48
- # Doubles
49
- let(:attribute) { double(:attribute, to_s: 'attribute_name') }
50
- let(:record) { double(:record, errors: record_errors) }
51
- let(:record_errors) { double(:errors) }
52
- let(:value) { double(:value) }
53
- let(:schema) { double(:schema) }
54
- let(:schema_validator) { double(:schema_validator) }
55
- let(:raw_errors) { double(:raw_errors) }
56
- let(:validator_errors) { double(:validator_errors) }
33
+ context 'with valid JSON data but schema errors' do
34
+ let(:user) { User.new(data: '{"city":"Quebec City"}', other_data: '{"city":"Quebec City"}') }
57
35
 
58
- before do
59
- expect(validator).to receive(:schema).with(record).and_return(schema)
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)
63
- end
64
-
65
- context 'with JSON Schemer errors' do
66
- before do
67
- expect(validator_errors).to receive(:empty?).and_return(false)
68
- expect(record).not_to receive(:"#{attribute}_invalid_json")
69
- expect(record_errors).to receive(:add).with(attribute, options[:message], value: value)
36
+ specify do
37
+ expect(user).not_to be_valid
38
+ expect(user.errors.full_messages).to eql(['Data root is missing required keys: country', 'Other data missing_keys country'])
39
+ expect(user.data).to eql({ 'city' => 'Quebec City' })
40
+ expect(user.data_invalid_json).to be_nil
70
41
  end
71
-
72
- specify { validate_each! }
73
42
  end
74
43
 
75
- context 'without JSON Schemer errors but with invalid JSON data' do
76
- before do
77
- expect(validator_errors).to receive(:empty?).and_return(true)
78
- expect(record).to receive(:"#{attribute}_invalid_json").and_return('foo"{]')
79
- expect(record_errors).to receive(:add).with(attribute, options[:message], value: value)
80
- end
81
-
82
- specify { validate_each! }
83
- end
84
-
85
- context 'without JSON Schemer errors and valid JSON data' do
86
- before do
87
- expect(validator_errors).to receive(:empty?).and_return(true)
88
- expect(record).to receive(:"#{attribute}_invalid_json").and_return(nil)
89
- expect(record_errors).not_to receive(:add)
90
- end
91
-
92
- specify { validate_each! }
93
- end
94
-
95
- context 'with multiple error messages' do
96
- let(:options) { { attributes: [attribute], message: message, options: { strict: true } } }
97
- let(:message) { ->(errors) { errors.to_a } }
44
+ context 'with invalid JSON data' do
45
+ let(:data) { 'What? This is not JSON at all.' }
46
+ let(:user) { User.new(data: data) }
98
47
 
99
- before do
100
- expect(validator_errors).to receive(:empty?).and_return(false)
101
- expect(validator_errors).to receive(:to_a).and_return(%i[first_error second_error])
102
- expect(record).not_to receive(:"#{attribute}_invalid_json")
103
- expect(record_errors).to receive(:add).with(attribute, :first_error, value: value)
104
- expect(record_errors).to receive(:add).with(attribute, :second_error, value: value)
48
+ specify do
49
+ expect(user.data_invalid_json).to eql(data)
50
+ expect(user.data).to eql({})
105
51
  end
106
-
107
- specify { validate_each! }
108
52
  end
109
53
  end
110
54
 
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_json_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.1
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: 2021-06-17 00:00:00.000000000 Z
11
+ date: 2022-06-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.12'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.12'
27
27
  - !ruby/object:Gem::Dependency
@@ -89,7 +89,7 @@ dependencies:
89
89
  version: 4.2.0
90
90
  - - "<"
91
91
  - !ruby/object:Gem::Version
92
- version: '7'
92
+ version: '8'
93
93
  type: :development
94
94
  prerelease: false
95
95
  version_requirements: !ruby/object:Gem::Requirement
@@ -99,7 +99,7 @@ dependencies:
99
99
  version: 4.2.0
100
100
  - - "<"
101
101
  - !ruby/object:Gem::Version
102
- version: '7'
102
+ version: '8'
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: phare
105
105
  requirement: !ruby/object:Gem::Requirement
@@ -151,7 +151,7 @@ dependencies:
151
151
  version: 4.2.0
152
152
  - - "<"
153
153
  - !ruby/object:Gem::Version
154
- version: '7'
154
+ version: '8'
155
155
  type: :runtime
156
156
  prerelease: false
157
157
  version_requirements: !ruby/object:Gem::Requirement
@@ -161,7 +161,7 @@ dependencies:
161
161
  version: 4.2.0
162
162
  - - "<"
163
163
  - !ruby/object:Gem::Version
164
- version: '7'
164
+ version: '8'
165
165
  description: ActiveRecord::JSONValidator makes it easy to validate JSON attributes
166
166
  with a JSON schema.
167
167
  email:
@@ -182,6 +182,7 @@ files:
182
182
  - gemfiles/Gemfile.activerecord-4.2.x
183
183
  - gemfiles/Gemfile.activerecord-5.0.x
184
184
  - gemfiles/Gemfile.activerecord-6.0.x
185
+ - gemfiles/Gemfile.activerecord-7.0.x
185
186
  - lib/active_record/json_validator/validator.rb
186
187
  - lib/active_record/json_validator/version.rb
187
188
  - lib/activerecord_json_validator.rb