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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 53df7dd58f70876c93e8d4289db52ed1639a0a50
4
- data.tar.gz: 61f4c218b92cf98902b4298248955f94f2588131
3
+ metadata.gz: 58260c1220dfc3554bbb0cdc9bae74eef8be77b3
4
+ data.tar.gz: 980b78f82e9c9b8364aeae6c9c4d0f433e83bda3
5
5
  SHA512:
6
- metadata.gz: eb4f3e30be96038c7be8501593b1b463bbe7dc89200a8df5885ece06ca136e0b05945caf571d4d9386c1f44b73215eabf0f7252369166c5e6b43ca47ef08ff98
7
- data.tar.gz: 5f5b5674b0cfbf1191f087ca7dd91059d5eea73565c2ec8c44b827c3f6dfbf985c1be6605e0fbd77350daa7e1b93c666e26c8800dfe23987a90fa8c22799a11d
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
- class Foo < ActiveRecord::Base
17
- validates :bar, json: {
18
- schema: {
19
- '$schema' => 'http://json-schema.org/schema#',
20
- 'title': 'Universal spoons schema',
21
- 'properties': {
22
- 'handleSize': {
23
- 'type': 'integer',
24
- 'minimum': 0
25
- }
26
- },
27
- 'required': ['handleSize']
28
- }
29
- }
30
- end
31
-
32
- Then whenever an instance of `Foo` is saved, `Foo.bar` (assumed to be a hash) will be validated against the JSON schema specified. In this case, `Foo.new(bar: { handleSize: -10 })` would be invalid, but `Foo.new(bar: { handleSize: 10 })` would be valid.
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.
@@ -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{^(test|spec|features)/})
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"
@@ -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!
@@ -1,4 +1,4 @@
1
1
  # can't put this in a JsonValidator module because ActiveModel expects JsonValidator to be a class
2
2
  module JsonValidatorMeta
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
@@ -52,7 +52,7 @@ describe JsonValidator do
52
52
  {}
53
53
  ).to(
54
54
  {
55
- json_data: ["The property '#/' did not contain a required property of 'soup'"]
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: ["The property '#/menu' was not of a minimum string length of 200"]
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("The property '#/menu' was not of a minimum string length of 200")
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.1
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-14 00:00:00.000000000 Z
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: '0'
146
+ version: 1.9.3
147
147
  required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  requirements:
149
149
  - - ">="