activerecord_json_validator 0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +10 -0
- data/lib/active_record/json_validator/validator.rb +27 -2
- data/lib/active_record/json_validator/version.rb +1 -1
- data/spec/json_validator_spec.rb +6 -1
- 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: 21b3da5123efa04feb0b954cb2300a241de8f091
|
4
|
+
data.tar.gz: ce13d50fd4ef460f12ed57ab732834133425e254
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b47ebab5fb3d2d4575d2c659201d83e300d7627ece382276429bc0eb7e37a0c104976578a645e74c1a2eaf80a017dbc1de78f2f2dd5f9f38e8bc7989fb91756c
|
7
|
+
data.tar.gz: fd32b8a98506390f1fae3b3acb8dd4f073edf4c7ec60f3aaad8d1bd5703e1547c006f3cc4363ba3080facbf20f1383a16005a02b9764eb73e8fcc767eaea662b
|
data/Rakefile
CHANGED
@@ -9,3 +9,13 @@ desc 'Run all specs'
|
|
9
9
|
RSpec::Core::RakeTask.new(:spec) do |task|
|
10
10
|
task.pattern = 'spec/**/*_spec.rb'
|
11
11
|
end
|
12
|
+
|
13
|
+
desc 'Start an IRB session with the gem'
|
14
|
+
task :console do
|
15
|
+
$:.unshift File.expand_path('..', __FILE__)
|
16
|
+
require 'activerecord_json_validator'
|
17
|
+
require 'irb'
|
18
|
+
|
19
|
+
ARGV.clear
|
20
|
+
IRB.start
|
21
|
+
end
|
@@ -2,15 +2,40 @@ class JsonValidator < ActiveModel::EachValidator
|
|
2
2
|
def initialize(options)
|
3
3
|
options.reverse_merge!(message: :invalid_json)
|
4
4
|
options.reverse_merge!(schema: nil)
|
5
|
+
@attributes = options[:attributes]
|
5
6
|
|
6
7
|
super
|
7
8
|
end
|
8
9
|
|
10
|
+
# Redefine the setter method for the attributes, since we want to
|
11
|
+
# catch any MultiJson::LoadError errors.
|
12
|
+
def setup(model)
|
13
|
+
@attributes.each do |attribute|
|
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
|
26
|
+
end
|
27
|
+
|
28
|
+
# Validate the JSON value with a JSON schema path or String
|
9
29
|
def validate_each(record, attribute, value)
|
10
|
-
|
30
|
+
begin
|
31
|
+
json_value = JSON.dump(value)
|
32
|
+
rescue JSON::GeneratorError
|
33
|
+
json_value = ''
|
34
|
+
end
|
35
|
+
|
11
36
|
errors = ::JSON::Validator.fully_validate(options.fetch(:schema), json_value)
|
12
37
|
|
13
|
-
if errors.any?
|
38
|
+
if errors.any? || instance_variable_get(:"@_#{attribute}_sane_json") == false
|
14
39
|
record.errors.add(attribute, options.fetch(:message), value: value)
|
15
40
|
end
|
16
41
|
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 # Change this to `t.json` when Travis supports PostgreSQL 9.2+
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
@@ -39,4 +39,9 @@ describe JsonValidator do
|
|
39
39
|
let(:attributes) { { name: 'Samuel Garneau', profile: { country: 'CA' } } }
|
40
40
|
it { expect(User.new(attributes)).to be_valid }
|
41
41
|
end
|
42
|
+
|
43
|
+
context 'with malformed JSON value' do
|
44
|
+
let(:attributes) { { name: 'Samuel Garneau', profile: 'foo:}bar' } }
|
45
|
+
it { expect(User.new(attributes)).to_not be_valid }
|
46
|
+
end
|
42
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:
|
4
|
+
version: 0.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: 2013-11-
|
11
|
+
date: 2013-11-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|