json_validator 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +35 -17
- data/json_validator.gemspec +3 -1
- data/lib/json_validator.rb +4 -0
- data/lib/json_validator_meta.rb +1 -1
- data/spec/lib/json_validator_spec.rb +3 -3
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 58260c1220dfc3554bbb0cdc9bae74eef8be77b3
|
4
|
+
data.tar.gz: 980b78f82e9c9b8364aeae6c9c4d0f433e83bda3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c349848ed5105462d6079f49f6b949fd80f876ff4224ea701ebfc2e4b5cf88cf6d9b400dd9aaf77dfd3add75bb916f147304a47905250f0da93d87cfef36277f
|
7
|
+
data.tar.gz: ab6563f6110fb58a38d0ad8f9538fc12879d700b659dd5f8ddcbad44ff48e5acf082994516160730e38a98ab45587c9e3079d515b7aad4130b913550fb7d837b
|
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# JsonValidator
|
2
2
|
|
3
|
+
[![Gem Version](http://img.shields.io/gem/v/json_validator.svg)](https://rubygems.org/gems/json_validator)
|
3
4
|
[![Build Status](http://img.shields.io/travis/iainbeeston/json_validator/master.svg)](https://travis-ci.org/iainbeeston/json_validator)
|
4
5
|
[![Code Climate](http://img.shields.io/codeclimate/github/iainbeeston/json_validator.svg)](https://codeclimate.com/github/iainbeeston/json_validator)
|
5
6
|
|
7
|
+
|
6
8
|
JsonValidator is an ActiveModel validator that validates any hash field against [JSONSchema](http://json-schema.org), returning errors in the model's own `errors` attribute.
|
7
9
|
|
8
10
|
This gem was originally written to provide deep validation of JSON attributes, which are available alongside primative types in recent versions of [PostgreSQL](http://www.postgresql.org), but it works equally well with ActiveModel objects.
|
@@ -13,22 +15,38 @@ Most of the functionality is dependent on the wonderful [json-schema](https://gi
|
|
13
15
|
|
14
16
|
If you're using Ruby on Rails and ActiveRecord, add a validation to your model like this:
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
18
|
+
~~~ruby
|
19
|
+
class Foo < ActiveRecord::Base
|
20
|
+
validates :bar, json: {
|
21
|
+
schema: JSON.parse(File.read('foo_schema.json'))
|
22
|
+
}
|
23
|
+
end
|
24
|
+
~~~
|
25
|
+
|
26
|
+
And you have a schema file (ie. `foo_schema.json`) like this:
|
27
|
+
|
28
|
+
~~~json
|
29
|
+
{
|
30
|
+
"$schema": "http://json-schema.org/draft-04/schema#",
|
31
|
+
"title": "Universal spoons schema",
|
32
|
+
"properties": {
|
33
|
+
"handleSize": {
|
34
|
+
"type": "integer",
|
35
|
+
"minimum": 0
|
36
|
+
}
|
37
|
+
},
|
38
|
+
"required": ["handleSize"]
|
39
|
+
}
|
40
|
+
~~~
|
41
|
+
|
42
|
+
Then whenever an instance of `Foo` is saved, `Foo.bar` (assumed to be a hash) will be validated against the JSON schema specified. So, for example:
|
43
|
+
|
44
|
+
~~~ruby
|
45
|
+
|
46
|
+
f = Foo.new(bar: { handleSize: -10 })
|
47
|
+
f.valid? # false
|
48
|
+
f.errors.full_messages # ["Bar is invalid (the property '#/handleSize' did not have a minimum value of 0, inclusively)"]
|
49
|
+
|
50
|
+
~~~
|
33
51
|
|
34
52
|
The attribute being validated can be either a hash or a string (which will be parsed as JSON). The schema can be either a hash or a Proc that returns a hash (if you'd like to decide on the schema at runtime), and there's no reason why you could not load your schema from a .json file.
|
data/json_validator.gemspec
CHANGED
@@ -14,9 +14,11 @@ Gem::Specification.new do |spec|
|
|
14
14
|
|
15
15
|
spec.files = `git ls-files -z`.split("\x0")
|
16
16
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
-
spec.test_files = spec.files.grep(%r{^
|
17
|
+
spec.test_files = spec.files.grep(%r{^spec/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
+
spec.required_ruby_version = ">= 1.9.3"
|
21
|
+
|
20
22
|
spec.add_dependency "activesupport", ">= 3.2"
|
21
23
|
spec.add_dependency "activemodel", ">= 3.2"
|
22
24
|
spec.add_dependency "json-schema"
|
data/lib/json_validator.rb
CHANGED
@@ -37,6 +37,10 @@ class JsonValidator < ActiveModel::EachValidator
|
|
37
37
|
def translate_message(msg)
|
38
38
|
# remove suffix
|
39
39
|
msg.gsub!(/ in schema .*$/, '')
|
40
|
+
# lowercase first letter (eg. 'The' becomes 'the')
|
41
|
+
msg.gsub!(/^./) { |m| m.downcase }
|
42
|
+
# prefix with 'is invalid'
|
43
|
+
msg.gsub!(/^(.*)$/, 'is invalid (\1)')
|
40
44
|
end
|
41
45
|
|
42
46
|
def check_validity!
|
data/lib/json_validator_meta.rb
CHANGED
@@ -52,7 +52,7 @@ describe JsonValidator do
|
|
52
52
|
{}
|
53
53
|
).to(
|
54
54
|
{
|
55
|
-
json_data: ["
|
55
|
+
json_data: ["is invalid (the property '#/' did not contain a required property of 'soup')"]
|
56
56
|
}
|
57
57
|
)
|
58
58
|
end
|
@@ -90,7 +90,7 @@ describe JsonValidator do
|
|
90
90
|
{}
|
91
91
|
).to(
|
92
92
|
{
|
93
|
-
json_data: ["
|
93
|
+
json_data: ["is invalid (the property '#/menu' was not of a minimum string length of 200)"]
|
94
94
|
}
|
95
95
|
)
|
96
96
|
end
|
@@ -150,7 +150,7 @@ describe JsonValidator do
|
|
150
150
|
|
151
151
|
it 'translates json-schema messages to slightly more readable ones' do
|
152
152
|
msg = "The property '#/menu' was not of a minimum string length of 200 in schema 40148e2f-45d6-51b7-972a-179bd9de61d6#"
|
153
|
-
expect(subject.translate_message(msg)).to eq("
|
153
|
+
expect(subject.translate_message(msg)).to eq("is invalid (the property '#/menu' was not of a minimum string length of 200)")
|
154
154
|
end
|
155
155
|
end
|
156
156
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Iain Beeston
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -143,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
143
143
|
requirements:
|
144
144
|
- - ">="
|
145
145
|
- !ruby/object:Gem::Version
|
146
|
-
version:
|
146
|
+
version: 1.9.3
|
147
147
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
148
|
requirements:
|
149
149
|
- - ">="
|