hash_validator 0.2.3 → 0.2.4
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 +3 -1
- data/lib/hash_validator/validators.rb +1 -0
- data/lib/hash_validator/validators/enumerable_validator.rb +21 -0
- data/lib/hash_validator/validators/simple_type_validators.rb +1 -0
- data/lib/hash_validator/version.rb +1 -1
- data/spec/validators/in_enumerable_spec.rb +61 -0
- data/spec/validators/simple_types_spec.rb +4 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 14c3d859ad35ec3b2ac6b4750bd10a5f6d0d1784
|
4
|
+
data.tar.gz: 024a49a7d98e43fe7476fd4f7e87b4dbef88393f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5bf1fe8300640b5d0f5e3a0a01429526d25f012a031add4594702439ee5e89c7c01f942f7f55b5a6cf86ddd57a61b9d0f84280c7106a2c4336b2b0826f17a318
|
7
|
+
data.tar.gz: 28d376b771b6e4e38f0c69b1bfb3430053aa7b0f01fe37c2fb7f104d8797c9b7a4b76de98f9f8ee41d1b6a1eba4d43558c4958aab06bb4f1c3c044e921137364
|
data/README.md
CHANGED
@@ -67,6 +67,7 @@ Define a validation hash which will be used to validate. This has can be nested
|
|
67
67
|
* `array`
|
68
68
|
* `boolean`
|
69
69
|
* `complex`
|
70
|
+
* `enumerable`
|
70
71
|
* `float`
|
71
72
|
* `integer`
|
72
73
|
* `numeric`
|
@@ -81,7 +82,8 @@ Define a validation hash which will be used to validate. This has can be nested
|
|
81
82
|
|
82
83
|
Additional validations exist to validate beyond simple typing, such as:
|
83
84
|
|
84
|
-
*
|
85
|
+
* An Enumerable instance: validates that the value is contained within the supplied enumerable.
|
86
|
+
* `email`: email address validation (string + email address).
|
85
87
|
|
86
88
|
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).
|
87
89
|
|
@@ -30,3 +30,4 @@ require 'hash_validator/validators/presence_validator'
|
|
30
30
|
require 'hash_validator/validators/simple_type_validators'
|
31
31
|
require 'hash_validator/validators/boolean_validator'
|
32
32
|
require 'hash_validator/validators/email_validator'
|
33
|
+
require 'hash_validator/validators/enumerable_validator'
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class HashValidator::Validator::EnumerableValidator < HashValidator::Validator::Base
|
2
|
+
def initialize
|
3
|
+
super('_enumerable') # The name of the validator, underscored as it won't usually be directly invoked (invoked through use of validator)
|
4
|
+
end
|
5
|
+
|
6
|
+
def should_validate?(rhs)
|
7
|
+
rhs.is_a?(Enumerable)
|
8
|
+
end
|
9
|
+
|
10
|
+
def presence_error_message
|
11
|
+
'value from list required'
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate(key, value, validations, errors)
|
15
|
+
unless validations.include?(value)
|
16
|
+
errors[key] = presence_error_message
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
HashValidator.append_validator(HashValidator::Validator::EnumerableValidator.new)
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Enumerable validator' do
|
4
|
+
describe 'Accepting Enumerables in validations' do
|
5
|
+
it 'should accept an empty array' do
|
6
|
+
validate({}, { foo: [] })
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should accept an array' do
|
10
|
+
validate({}, { foo: [ 'apple', 'banana', 'carrot' ] })
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should accept a hash' do
|
14
|
+
validate({}, { foo: { apple: 'apple', banana: 'banana', carrot: 'cattot' } })
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#validate' do
|
19
|
+
describe 'Simple Array' do
|
20
|
+
let(:validations) {{ fruit: [ 'apple', 'banana', 'carrot' ] }}
|
21
|
+
|
22
|
+
it 'should validate true if the value is present' do
|
23
|
+
validate({ fruit: 'apple' }, validations).valid?.should be_true
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should validate false if the value is not present' do
|
27
|
+
validate({ fruit: 'pear' }, validations).valid?.should be_false
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should validate false if the key is not present' do
|
31
|
+
validate({ something: 'apple' }, validations).valid?.should be_false
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should provide an appropriate error message is the value is not present' do
|
35
|
+
validate({ fruit: 'pear' }, validations).errors.should eq({ fruit: 'value from list required' })
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe 'Range' do
|
40
|
+
let(:validations) {{ number: 1..10 }}
|
41
|
+
|
42
|
+
it 'should validate true if the value is present' do
|
43
|
+
validate({ number: 5 }, validations).valid?.should be_true
|
44
|
+
end
|
45
|
+
it 'should validate false if the value is not present' do
|
46
|
+
validate({ number: 15 }, validations).valid?.should be_false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe 'Infinite Range' do
|
51
|
+
let(:validations) {{ number: 1..Float::INFINITY }}
|
52
|
+
|
53
|
+
it 'should validate true if the value is present' do
|
54
|
+
validate({ number: 5 }, validations).valid?.should be_true
|
55
|
+
end
|
56
|
+
it 'should validate false if the value is not present' do
|
57
|
+
validate({ number: -5 }, validations).valid?.should be_false
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -13,6 +13,10 @@ describe 'Simple validator types' do
|
|
13
13
|
valid: [ Complex(1), Complex(2, 3), Complex('2/3+3/4i'), 0.3.to_c ],
|
14
14
|
invalid: [ nil, '', 123, '123', Time.now, '[1]', [1], '2/3+3/4i', Rational(2, 3) ]
|
15
15
|
},
|
16
|
+
enumerable: {
|
17
|
+
valid: [ [], [ 1, 2, 3 ], 1..Float::INFINITY ],
|
18
|
+
invalid: [ nil, '', 123, '123', Time.now, Float::INFINITY ]
|
19
|
+
},
|
16
20
|
float: {
|
17
21
|
valid: [ 0.0, 1.1, 1.23, Float::INFINITY, Float::EPSILON ],
|
18
22
|
invalid: [ nil, '', 0, 123, '123', Time.now, '[1]', '2013-03-04' ]
|
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.2.
|
4
|
+
version: 0.2.4
|
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-07-
|
11
|
+
date: 2013-07-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -87,6 +87,7 @@ files:
|
|
87
87
|
- lib/hash_validator/validators/base.rb
|
88
88
|
- lib/hash_validator/validators/boolean_validator.rb
|
89
89
|
- lib/hash_validator/validators/email_validator.rb
|
90
|
+
- lib/hash_validator/validators/enumerable_validator.rb
|
90
91
|
- lib/hash_validator/validators/hash_validator.rb
|
91
92
|
- lib/hash_validator/validators/presence_validator.rb
|
92
93
|
- lib/hash_validator/validators/simple_type_validators.rb
|
@@ -98,6 +99,7 @@ files:
|
|
98
99
|
- spec/validators/base_spec.rb
|
99
100
|
- spec/validators/boolean_spec.rb
|
100
101
|
- spec/validators/email_spec.rb
|
102
|
+
- spec/validators/in_enumerable_spec.rb
|
101
103
|
- spec/validators/presence_spec.rb
|
102
104
|
- spec/validators/simple_spec.rb
|
103
105
|
- spec/validators/simple_types_spec.rb
|
@@ -133,6 +135,7 @@ test_files:
|
|
133
135
|
- spec/validators/base_spec.rb
|
134
136
|
- spec/validators/boolean_spec.rb
|
135
137
|
- spec/validators/email_spec.rb
|
138
|
+
- spec/validators/in_enumerable_spec.rb
|
136
139
|
- spec/validators/presence_spec.rb
|
137
140
|
- spec/validators/simple_spec.rb
|
138
141
|
- spec/validators/simple_types_spec.rb
|