hash_validator 0.0.2 → 0.1.0
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/README.md +2 -0
- data/hash_validator.gemspec +1 -0
- data/lib/hash_validator/validators.rb +1 -0
- data/lib/hash_validator/validators/hash_validator.rb +6 -2
- data/lib/hash_validator/validators/presence_validator.rb +22 -0
- data/lib/hash_validator/validators/simple_type_validator.rb +5 -1
- data/lib/hash_validator/version.rb +1 -1
- data/spec/hash_validator_spec.rb +22 -4
- data/spec/spec_helper.rb +3 -0
- data/spec/validators/base_spec.rb +17 -0
- data/spec/validators/presence_spec.rb +45 -0
- data/spec/validators/simple_type_spec.rb +34 -0
- metadata +23 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1db341e997808db63ae406e44a49d51b1ca45287
|
4
|
+
data.tar.gz: b1d8af971d7c0fcdcdfb839bab7aec52092a4fc2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 949b8a43860f1ec57f5fd3c23d0f18bb41dabf4615a215f636d6af7cbeb22cf69c78e20646bf6d4836ab38da28aaefe0e1fcd690e0df8867a3215a0b65b6e5a7
|
7
|
+
data.tar.gz: 61e7ca533f2b11c352218104382a8d2b04a1db4ac62f11cd4eefe8daab9c95b4bcf2b8eca5f559a54d7d8d7445ca40b8f632fd6625e4938beb0e8d29360c4fea
|
data/README.md
CHANGED
@@ -3,6 +3,7 @@
|
|
3
3
|
[](https://travis-ci.org/JamesBrooks/hash_validator)
|
4
4
|
[](https://codeclimate.com/github/JamesBrooks/hash_validator)
|
5
5
|
[](http://badge.fury.io/rb/hash_validator)
|
6
|
+
[](https://coveralls.io/r/JamesBrooks/hash_validator)
|
6
7
|
[](https://gemnasium.com/JamesBrooks/hash_validator)
|
7
8
|
|
8
9
|
Ruby library to validate hashes (Hash) against user-defined requirements
|
@@ -67,6 +68,7 @@ Define a validation hash which will be used to validate. This has can be nested
|
|
67
68
|
* `numeric`
|
68
69
|
* `array`
|
69
70
|
* `time`
|
71
|
+
* `required`: just requires any value to be present for the designated key.
|
70
72
|
* hashes are validates by nesting validations, or if just the presence of a hash is required `{}` can be used.
|
71
73
|
|
72
74
|
Example use-cases include Ruby APIs (I'm currently using it in a Rails API that I'm building for better error responses to developers).
|
data/hash_validator.gemspec
CHANGED
@@ -3,6 +3,10 @@ class HashValidator::Validator::HashValidator < HashValidator::Validator::Base
|
|
3
3
|
'hash'
|
4
4
|
end
|
5
5
|
|
6
|
+
def presence_error_message
|
7
|
+
"#{name} required"
|
8
|
+
end
|
9
|
+
|
6
10
|
def should_validate?(rhs)
|
7
11
|
rhs.is_a?(Hash)
|
8
12
|
end
|
@@ -10,7 +14,7 @@ class HashValidator::Validator::HashValidator < HashValidator::Validator::Base
|
|
10
14
|
def validate(key, value, validations, errors)
|
11
15
|
# Validate hash
|
12
16
|
unless value.is_a?(Hash)
|
13
|
-
errors[
|
17
|
+
errors[key] = presence_error_message
|
14
18
|
return
|
15
19
|
end
|
16
20
|
|
@@ -22,7 +26,7 @@ class HashValidator::Validator::HashValidator < HashValidator::Validator::Base
|
|
22
26
|
|
23
27
|
# Key presence
|
24
28
|
unless value[v_key]
|
25
|
-
errors[v_key] =
|
29
|
+
errors[v_key] = validator.presence_error_message
|
26
30
|
next
|
27
31
|
end
|
28
32
|
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class HashValidator::Validator::PresenceValidator < HashValidator::Validator::Base
|
2
|
+
def name
|
3
|
+
'required'
|
4
|
+
end
|
5
|
+
|
6
|
+
def presence_error_message
|
7
|
+
'is required'
|
8
|
+
end
|
9
|
+
|
10
|
+
def should_validate?(rhs)
|
11
|
+
rhs == name
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate(key, value, validations, errors)
|
15
|
+
unless value
|
16
|
+
errors[key] = presence_error_message
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
HashValidator.append_validator(HashValidator::Validator::PresenceValidator.new)
|
@@ -7,13 +7,17 @@ class HashValidator::Validator::SimpleTypeValidator < HashValidator::Validator::
|
|
7
7
|
self.klass = klass
|
8
8
|
end
|
9
9
|
|
10
|
+
def presence_error_message
|
11
|
+
"#{name} required"
|
12
|
+
end
|
13
|
+
|
10
14
|
def should_validate?(rhs)
|
11
15
|
rhs == self.name
|
12
16
|
end
|
13
17
|
|
14
18
|
def validate(key, value, validations, errors)
|
15
19
|
unless value.is_a?(klass)
|
16
|
-
errors[key] =
|
20
|
+
errors[key] = presence_error_message
|
17
21
|
end
|
18
22
|
end
|
19
23
|
end
|
data/spec/hash_validator_spec.rb
CHANGED
@@ -4,12 +4,30 @@ describe HashValidator do
|
|
4
4
|
describe 'individual type validations' do
|
5
5
|
it 'should validate hash' do
|
6
6
|
validate({ v: {} }, { v: {} }).valid?.should be_true
|
7
|
+
|
7
8
|
validate({ v: '' }, { v: {} }).valid?.should be_false
|
9
|
+
validate({ v: '' }, { v: {} }).errors.should eq({ v: 'hash required' })
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should validate presence' do
|
13
|
+
validate({ v: 'test' }, { v: 'required' }).valid?.should be_true
|
14
|
+
validate({ v: 1234 }, { v: 'required' }).valid?.should be_true
|
15
|
+
|
16
|
+
validate({ v: nil }, { v: 'required' }).valid?.should be_false
|
17
|
+
validate({ v: nil }, { v: 'required' }).errors.should eq({ v: 'is required' })
|
18
|
+
|
19
|
+
validate({ x: 'test' }, { v: 'required' }).valid?.should be_false
|
20
|
+
validate({ x: 'test' }, { v: 'required' }).errors.should eq({ v: 'is required' })
|
21
|
+
|
22
|
+
validate({ x: 1234 }, { v: 'required' }).valid?.should be_false
|
23
|
+
validate({ x: 1234 }, { v: 'required' }).errors.should eq({ v: 'is required' })
|
8
24
|
end
|
9
25
|
|
10
26
|
it 'should validate string' do
|
11
27
|
validate({ v: 'test' }, { v: 'string' }).valid?.should be_true
|
28
|
+
|
12
29
|
validate({ v: 123456 }, { v: 'string' }).valid?.should be_false
|
30
|
+
validate({ v: 123456 }, { v: 'string' }).errors.should eq({ v: 'string required' })
|
13
31
|
end
|
14
32
|
|
15
33
|
it 'should validate numeric' do
|
@@ -114,7 +132,7 @@ describe HashValidator do
|
|
114
132
|
it 'should not validate a simple hash 2' do
|
115
133
|
v = validate(invalid_simple_hash, validations)
|
116
134
|
v.valid?.should be_false
|
117
|
-
v.errors.should eq({ bar: '
|
135
|
+
v.errors.should eq({ bar: 'string required' })
|
118
136
|
end
|
119
137
|
|
120
138
|
it 'should validate a complex hash' do
|
@@ -126,12 +144,12 @@ describe HashValidator do
|
|
126
144
|
it 'should not validate a complex hash 2' do
|
127
145
|
v = validate(invalid_complex_hash, validations)
|
128
146
|
v.valid?.should be_false
|
129
|
-
v.errors.should eq({ bar: '
|
147
|
+
v.errors.should eq({ bar: 'string required' })
|
130
148
|
end
|
131
149
|
end
|
132
150
|
|
133
151
|
describe 'nested validations' do
|
134
|
-
let(:validations) {{ foo: 'numeric', bar: 'string', user: { first_name: 'string', age: '
|
152
|
+
let(:validations) {{ foo: 'numeric', bar: 'string', user: { first_name: 'string', age: 'required', likes: 'array' } }}
|
135
153
|
|
136
154
|
it 'should validate a complex hash' do
|
137
155
|
v = validate(complex_hash, validations)
|
@@ -142,7 +160,7 @@ describe HashValidator do
|
|
142
160
|
it 'should not validate a complex hash 2' do
|
143
161
|
v = validate(invalid_complex_hash, validations)
|
144
162
|
v.valid?.should be_false
|
145
|
-
v.errors.should eq({ bar: '
|
163
|
+
v.errors.should eq({ bar: 'string required', user: { age: 'is required', likes: 'array required' } })
|
146
164
|
end
|
147
165
|
end
|
148
166
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HashValidator::Validator::Base do
|
4
|
+
let(:validator) { HashValidator::Validator::Base.new }
|
5
|
+
|
6
|
+
describe '#should_validate?' do
|
7
|
+
it 'throws an exception' do
|
8
|
+
expect { validator.should_validate?('name') }.to raise_error(StandardError, 'should_validate? should not be called directly on BaseValidator')
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#validate' do
|
13
|
+
it 'throws an exception' do
|
14
|
+
expect { validator.validate('key', 'value', {}, {}) }.to raise_error(StandardError, 'validate should not be called directly on BaseValidator')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HashValidator::Validator::Base do
|
4
|
+
let(:validator) { HashValidator::Validator::PresenceValidator.new }
|
5
|
+
let(:errors) { Hash.new }
|
6
|
+
|
7
|
+
describe '#should_validate?' do
|
8
|
+
it 'should validate the name "required"' do
|
9
|
+
validator.should_validate?('required').should be_true
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should not validate other names' do
|
13
|
+
validator.should_validate?('string').should be_false
|
14
|
+
validator.should_validate?('array').should be_false
|
15
|
+
validator.should_validate?(nil).should be_false
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#validate' do
|
20
|
+
it 'should validate a string with true' do
|
21
|
+
validator.validate(:key, 'test', {}, errors)
|
22
|
+
|
23
|
+
errors.should be_empty
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should validate a number with true' do
|
27
|
+
validator.validate(:key, 123, {}, errors)
|
28
|
+
|
29
|
+
errors.should be_empty
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'should validate a time with true' do
|
33
|
+
validator.validate(:key, Time.now, {}, errors)
|
34
|
+
|
35
|
+
errors.should be_empty
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should validate nil with false' do
|
39
|
+
validator.validate(:key, nil, {}, errors)
|
40
|
+
|
41
|
+
errors.should_not be_empty
|
42
|
+
errors.should eq({ key: 'is required' })
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HashValidator::Validator::Base do
|
4
|
+
let(:my_class) { Class.new }
|
5
|
+
let(:validator) { HashValidator::Validator::SimpleTypeValidator.new('my_class', my_class) }
|
6
|
+
let(:errors) { Hash.new }
|
7
|
+
|
8
|
+
describe '#should_validate?' do
|
9
|
+
it 'should validate the name "my_class"' do
|
10
|
+
validator.should_validate?('my_class').should be_true
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should not validate other names' do
|
14
|
+
validator.should_validate?('string').should be_false
|
15
|
+
validator.should_validate?('array').should be_false
|
16
|
+
validator.should_validate?(nil).should be_false
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#validate' do
|
21
|
+
it 'should validate the my_class class with true' do
|
22
|
+
validator.validate(:key, my_class.new, {}, errors)
|
23
|
+
|
24
|
+
errors.should be_empty
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should validate other classes with errrors' do
|
28
|
+
validator.validate(:key, "foo bar", {}, errors)
|
29
|
+
|
30
|
+
errors.should_not be_empty
|
31
|
+
errors.should eq({ key: 'my_class required' })
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Brooks
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-04-
|
11
|
+
date: 2013-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -52,6 +52,20 @@ dependencies:
|
|
52
52
|
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: 2.13.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
55
69
|
description: Ruby library to validate hashes (Hash) against user-defined requirements
|
56
70
|
email:
|
57
71
|
- james@gooddogdesign.com
|
@@ -72,11 +86,15 @@ files:
|
|
72
86
|
- lib/hash_validator/validators.rb
|
73
87
|
- lib/hash_validator/validators/base.rb
|
74
88
|
- lib/hash_validator/validators/hash_validator.rb
|
89
|
+
- lib/hash_validator/validators/presence_validator.rb
|
75
90
|
- lib/hash_validator/validators/simple_type_validator.rb
|
76
91
|
- lib/hash_validator/version.rb
|
77
92
|
- spec/hash_validator_spec.rb
|
78
93
|
- spec/hash_validator_spec_helper.rb
|
79
94
|
- spec/spec_helper.rb
|
95
|
+
- spec/validators/base_spec.rb
|
96
|
+
- spec/validators/presence_spec.rb
|
97
|
+
- spec/validators/simple_type_spec.rb
|
80
98
|
homepage: https://github.com/JamesBrooks/hash_validator
|
81
99
|
licenses:
|
82
100
|
- MIT
|
@@ -105,3 +123,6 @@ test_files:
|
|
105
123
|
- spec/hash_validator_spec.rb
|
106
124
|
- spec/hash_validator_spec_helper.rb
|
107
125
|
- spec/spec_helper.rb
|
126
|
+
- spec/validators/base_spec.rb
|
127
|
+
- spec/validators/presence_spec.rb
|
128
|
+
- spec/validators/simple_type_spec.rb
|