activerecord_json_validator 0.1.1 → 0.1.2
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/.travis.yml +3 -0
- data/lib/active_record/json_validator/validator.rb +24 -15
- data/lib/active_record/json_validator/version.rb +1 -1
- data/spec/json_validator_spec.rb +5 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2333a21aa351ea4a53b99e11d40f48b9fb6fa145
|
4
|
+
data.tar.gz: d306555e99f6aa8fb57af47c616c75c6f7c4f43a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 163af0746e0533ebbf10a67d38583afc3a77107bbf248e10ea4dda9b4b463616ba3449a0613b4bca0744d71f304b45cd9a7cd981bc98ea596d944ce21baba507
|
7
|
+
data.tar.gz: 4f1635fc39554eda8e821bb81bde46d1df33762938e9b690ddfec2c72cf2541a9cac0eb285c8af6abf34702985779b4e7c095224d586c345468f3058a1be1504
|
data/.travis.yml
CHANGED
@@ -5,24 +5,13 @@ class JsonValidator < ActiveModel::EachValidator
|
|
5
5
|
@attributes = options[:attributes]
|
6
6
|
|
7
7
|
super
|
8
|
+
|
9
|
+
inject_setter_method(options[:class], @attributes) if options[:class]
|
8
10
|
end
|
9
11
|
|
10
|
-
#
|
11
|
-
# catch any MultiJson::LoadError errors.
|
12
|
+
# Only respond to `#setup` if we’re in Rails 4.0 or less
|
12
13
|
def setup(model)
|
13
|
-
@attributes
|
14
|
-
model.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
15
|
-
define_method "#{attribute}=" do |args|
|
16
|
-
begin
|
17
|
-
@_#{attribute}_sane_json = true
|
18
|
-
super(args)
|
19
|
-
rescue MultiJson::LoadError
|
20
|
-
@_#{attribute}_sane_json = false
|
21
|
-
super({})
|
22
|
-
end
|
23
|
-
end
|
24
|
-
RUBY
|
25
|
-
end
|
14
|
+
inject_setter_method(model, @attributes)
|
26
15
|
end
|
27
16
|
|
28
17
|
# Validate the JSON value with a JSON schema path or String
|
@@ -39,4 +28,24 @@ class JsonValidator < ActiveModel::EachValidator
|
|
39
28
|
record.errors.add(attribute, options.fetch(:message), value: value)
|
40
29
|
end
|
41
30
|
end
|
31
|
+
|
32
|
+
protected
|
33
|
+
|
34
|
+
# Redefine the setter method for the attributes, since we want to
|
35
|
+
# catch any MultiJson::LoadError errors.
|
36
|
+
def inject_setter_method(klass, attributes)
|
37
|
+
attributes.each do |attribute|
|
38
|
+
klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
39
|
+
define_method "#{attribute}=" do |args|
|
40
|
+
begin
|
41
|
+
@_#{attribute}_sane_json = true
|
42
|
+
super(args)
|
43
|
+
rescue MultiJson::LoadError
|
44
|
+
@_#{attribute}_sane_json = false
|
45
|
+
super({})
|
46
|
+
end
|
47
|
+
end
|
48
|
+
RUBY
|
49
|
+
end
|
50
|
+
end
|
42
51
|
end
|
data/spec/json_validator_spec.rb
CHANGED
@@ -5,7 +5,7 @@ describe JsonValidator do
|
|
5
5
|
run_migration do
|
6
6
|
create_table(:users, force: true) do |t|
|
7
7
|
t.string :name
|
8
|
-
t.text :profile
|
8
|
+
t.text :profile
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
@@ -27,21 +27,21 @@ describe JsonValidator do
|
|
27
27
|
|
28
28
|
context 'with blank JSON value' do
|
29
29
|
let(:attributes) { { name: 'Samuel Garneau', profile: {} } }
|
30
|
-
it { expect(User.
|
30
|
+
it { expect(User.create(attributes)).to_not be_valid }
|
31
31
|
end
|
32
32
|
|
33
33
|
context 'with invalid JSON value' do
|
34
34
|
let(:attributes) { { name: 'Samuel Garneau', profile: { city: 'Quebec City' } } }
|
35
|
-
it { expect(User.
|
35
|
+
it { expect(User.create(attributes)).to_not be_valid }
|
36
36
|
end
|
37
37
|
|
38
38
|
context 'with valid JSON value' do
|
39
39
|
let(:attributes) { { name: 'Samuel Garneau', profile: { country: 'CA' } } }
|
40
|
-
it { expect(User.
|
40
|
+
it { expect(User.create(attributes)).to be_valid }
|
41
41
|
end
|
42
42
|
|
43
43
|
context 'with malformed JSON value' do
|
44
44
|
let(:attributes) { { name: 'Samuel Garneau', profile: 'foo:}bar' } }
|
45
|
-
it { expect(User.
|
45
|
+
it { expect(User.create(attributes)).to_not be_valid }
|
46
46
|
end
|
47
47
|
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: 0.1.
|
4
|
+
version: 0.1.2
|
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: 2013-
|
11
|
+
date: 2013-12-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|