activerecord_json_validator 2.1.4 → 2.1.5

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 851b94edf54cbfdcd0a66a80dab95bcdeeea0e1c4a8831f634bc55aad5cba226
4
- data.tar.gz: bf3a8280ffa8300469e793422adc68e83ab444047db67d9e6cbafec528f69803
3
+ metadata.gz: 46e15a1f3a280bc1f2a5d197d0631a6b0e09a4beed596e971d66e1102e3a9c65
4
+ data.tar.gz: dfc7b7fd68462c50be8fb565d20d965fad13a29d487013df54d5cfab708b431d
5
5
  SHA512:
6
- metadata.gz: 33766dfbea4cf464c499709f76b742f3bfe9b37dcc656206a15794f93733ec6711f127057ff2efb67bb75b6e439e64e75ada856d62fcdb2fece42e4ddd81b6ff
7
- data.tar.gz: 7192194e8663dfbd9d343b66866ebab63d843200581bbe59be2c3f3bd485819fb13bba58bfb29241afa661c180399acce0d6fa3cf24a9d6fd5852b1d300a1be8
6
+ metadata.gz: a6b1c7770312b89c64889d8a1abeb6b59603b1be24a431e1a68cb9800268fbe36e617a1812d85c3f508ff603ffb1672c8c13a7c136847dd5b18959d815ef3304
7
+ data.tar.gz: 7aaf73c426c7918abb1323cc5812c04d5d8a3700192d7a0222dee9c23f2faa638692764926d34947a752ac6cb8cb14223c414f34a2595a101839ccbb11bb2552
data/README.md CHANGED
@@ -164,6 +164,15 @@ user.errors.full_messages
164
164
  # ]
165
165
  ```
166
166
 
167
+ ## Development
168
+
169
+ The tests require a database. We've provided a simple `docker-compose.yml` that will make
170
+ it trivial to run the tests against PostgreSQL. Simply run `docker compose up -d`
171
+ followed by `rake spec`. When you're done, run `docker compose down` to stop the database.
172
+
173
+ In order to use another database, simply define the `DATABASE_URL` environment variable
174
+ appropriately.
175
+
167
176
  ## License
168
177
 
169
178
  `ActiveRecord::JSONValidator` is © 2013-2022 [Mirego](https://www.mirego.com) and may be freely distributed under the [New BSD license](https://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/activerecord_json_validator/blob/master/LICENSE.md) file.
@@ -0,0 +1,11 @@
1
+ version: '3.2'
2
+
3
+ services:
4
+ postgres:
5
+ image: postgres:10
6
+ ports:
7
+ - 5432:5432
8
+ restart: on-failure
9
+ environment:
10
+ POSTGRES_DB: activerecord_json_validator_test
11
+ POSTGRES_HOST_AUTH_METHOD: trust # don't require password
@@ -37,21 +37,23 @@ protected
37
37
  # Redefine the setter method for the attributes, since we want to
38
38
  # catch JSON parsing errors.
39
39
  def inject_setter_method(klass, attributes)
40
+ return if klass.nil?
41
+
40
42
  attributes.each do |attribute|
41
- klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
43
+ klass.prepend(Module.new do
42
44
  attr_reader :"#{attribute}_invalid_json"
43
45
 
44
46
  define_method "#{attribute}=" do |args|
45
47
  begin
46
- @#{attribute}_invalid_json = nil
48
+ instance_variable_set("@#{attribute}_invalid_json", nil)
47
49
  args = ::ActiveSupport::JSON.decode(args) if args.is_a?(::String)
48
50
  super(args)
49
51
  rescue ActiveSupport::JSON.parse_error
50
- @#{attribute}_invalid_json = args
52
+ instance_variable_set("@#{attribute}_invalid_json", args)
51
53
  super({})
52
54
  end
53
55
  end
54
- RUBY
56
+ end)
55
57
  end
56
58
  end
57
59
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module JSONValidator
5
- VERSION = '2.1.4'
5
+ VERSION = '2.1.5'
6
6
  end
7
7
  end
@@ -3,6 +3,18 @@
3
3
  # rubocop:disable Metrics/BlockLength
4
4
  require 'spec_helper'
5
5
 
6
+ module CountryDefaulter
7
+ extend ActiveSupport::Concern
8
+
9
+ class_methods do
10
+ def default_country_attribute(name, country:)
11
+ define_method("#{name}=") do |value|
12
+ self[name] = { country: country }.merge(value)
13
+ end
14
+ end
15
+ end
16
+ end
17
+
6
18
  describe JsonValidator do
7
19
  describe :validate_each do
8
20
  before do
@@ -14,6 +26,8 @@ describe JsonValidator do
14
26
  end
15
27
 
16
28
  spawn_model 'User' do
29
+ include CountryDefaulter
30
+
17
31
  schema = '
18
32
  {
19
33
  "type": "object",
@@ -24,6 +38,9 @@ describe JsonValidator do
24
38
  "required": ["country"]
25
39
  }
26
40
  '
41
+
42
+ default_country_attribute :smart_data, country: 'Canada'
43
+
27
44
  serialize :data, JSON
28
45
  serialize :other_data, JSON
29
46
  validates :data, json: { schema: schema, message: ->(errors) { errors } }
@@ -41,7 +58,7 @@ describe JsonValidator do
41
58
  User.new(
42
59
  data: '{"city":"Quebec City"}',
43
60
  other_data: '{"city":"Quebec City"}',
44
- smart_data: { country: 'Canada', city: 'Quebec City' }
61
+ smart_data: { country: 'Ireland', city: 'Dublin' }
45
62
  )
46
63
  end
47
64
 
@@ -56,16 +73,38 @@ describe JsonValidator do
56
73
  )
57
74
  expect(user.data).to eql({ 'city' => 'Quebec City' })
58
75
  expect(user.data_invalid_json).to be_nil
76
+ expect(user.smart_data.city).to eql('Dublin')
77
+ expect(user.smart_data.country).to eql('Ireland')
59
78
  end
60
79
  end
61
80
 
62
81
  context 'with invalid JSON data' do
63
82
  let(:data) { 'What? This is not JSON at all.' }
64
- let(:user) { User.new(data: data) }
83
+ let(:user) { User.new(data: data, smart_data: data) }
65
84
 
66
85
  specify do
67
86
  expect(user.data_invalid_json).to eql(data)
68
87
  expect(user.data).to eql({})
88
+
89
+ # Ensure that both setters ran
90
+ expect(user.smart_data_invalid_json).to eql(data)
91
+ expect(user.smart_data).to eql(OpenStruct.new({ country: 'Canada' }))
92
+ end
93
+ end
94
+
95
+ context 'with missing country in smart data' do
96
+ let(:user) do
97
+ User.new(
98
+ data: '{"city":"Quebec City","country":"Canada"}',
99
+ other_data: '{"city":"Quebec City","country":"Canada"}',
100
+ smart_data: { city: 'Quebec City' }
101
+ )
102
+ end
103
+
104
+ specify do
105
+ expect(user).to be_valid
106
+ expect(user.smart_data.city).to eql('Quebec City')
107
+ expect(user.smart_data.country).to eql('Canada') # Due to CountryDefaulter
69
108
  end
70
109
  end
71
110
  end
@@ -6,6 +6,6 @@ class DatabaseAdapter
6
6
  end
7
7
 
8
8
  def establish_connection!
9
- ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])
9
+ ActiveRecord::Base.establish_connection(ENV.fetch('DATABASE_URL', 'postgres://postgres@localhost/activerecord_json_validator_test'))
10
10
  end
11
11
  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.4
4
+ version: 2.1.5
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: 2023-05-01 00:00:00.000000000 Z
11
+ date: 2023-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -179,6 +179,7 @@ files:
179
179
  - README.md
180
180
  - Rakefile
181
181
  - activerecord_json_validator.gemspec
182
+ - docker-compose.yml
182
183
  - lib/active_record/json_validator/validator.rb
183
184
  - lib/active_record/json_validator/version.rb
184
185
  - lib/activerecord_json_validator.rb