hash_validator 0.7.1 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -0
- data/lib/hash_validator.rb +4 -0
- data/lib/hash_validator/validations.rb +1 -0
- data/lib/hash_validator/validations/multiple.rb +8 -0
- data/lib/hash_validator/validators.rb +1 -0
- data/lib/hash_validator/validators/base.rb +1 -1
- data/lib/hash_validator/validators/boolean_validator.rb +1 -1
- data/lib/hash_validator/validators/email_validator.rb +2 -2
- data/lib/hash_validator/validators/hash_validator.rb +2 -2
- data/lib/hash_validator/validators/multiple_validator.rb +27 -0
- data/lib/hash_validator/validators/presence_validator.rb +1 -1
- data/lib/hash_validator/validators/simple_validator.rb +1 -1
- data/lib/hash_validator/version.rb +1 -1
- data/spec/hash_validator_spec.rb +16 -0
- data/spec/validators/multiple_spec.rb +36 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3f14e426a06042aa90d83cd7addfc6453f759e4
|
4
|
+
data.tar.gz: 7fd7178c50fc0491bf276696b2c42b1cca24d3a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44fc5928ebd086077dac64d7a53e91a3c14b144e9831b07389aa5b086ffdd63f6271659ae3fc3bc695e73d6d06767077147e48748ddcf32bb149b82aa1fdbe97
|
7
|
+
data.tar.gz: 2589013673495b01ac126c5a250528059e69c5f0610003d9db0ab5c852d3b837a51efd2eb18d22f3b81058a2b07acf292db0c199ad9da637a363ddc5ff171fc1
|
data/README.md
CHANGED
@@ -116,6 +116,19 @@ validator.valid? # => true
|
|
116
116
|
validator.errors # => {}
|
117
117
|
```
|
118
118
|
|
119
|
+
## Multiple validators
|
120
|
+
|
121
|
+
Multiple validators can be applied to a single key, e.g.
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
HashValidator.validate(
|
125
|
+
{ foo: 73 },
|
126
|
+
{ foo: HashValidator.multiple('numeric', 1..100) }
|
127
|
+
)
|
128
|
+
```
|
129
|
+
|
130
|
+
This is particularly useful when defining custom validators.
|
131
|
+
|
119
132
|
## Contributing
|
120
133
|
|
121
134
|
1. Fork it
|
data/lib/hash_validator.rb
CHANGED
@@ -35,3 +35,4 @@ require 'hash_validator/validators/regex_validator'
|
|
35
35
|
require 'hash_validator/validators/lambda_validator'
|
36
36
|
require 'hash_validator/validators/optional_validator'
|
37
37
|
require 'hash_validator/validators/many_validator'
|
38
|
+
require 'hash_validator/validators/multiple_validator'
|
@@ -3,7 +3,7 @@ class HashValidator::Validator::BooleanValidator < HashValidator::Validator::Bas
|
|
3
3
|
super('boolean') # The name of the validator
|
4
4
|
end
|
5
5
|
|
6
|
-
def validate(key, value,
|
6
|
+
def validate(key, value, _validations, errors)
|
7
7
|
unless [TrueClass, FalseClass].include?(value.class)
|
8
8
|
errors[key] = presence_error_message
|
9
9
|
end
|
@@ -7,11 +7,11 @@ class HashValidator::Validator::EmailValidator < HashValidator::Validator::Base
|
|
7
7
|
'is not a valid email'
|
8
8
|
end
|
9
9
|
|
10
|
-
def validate(key, value,
|
10
|
+
def validate(key, value, _validations, errors)
|
11
11
|
unless value.is_a?(String) && value.include?("@")
|
12
12
|
errors[key] = presence_error_message
|
13
13
|
end
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
HashValidator.append_validator(HashValidator::Validator::EmailValidator.new)
|
17
|
+
HashValidator.append_validator(HashValidator::Validator::EmailValidator.new)
|
@@ -20,7 +20,7 @@ class HashValidator::Validator::HashValidator < HashValidator::Validator::Base
|
|
20
20
|
validations.each do |v_key, v_value|
|
21
21
|
HashValidator.validator_for(v_value).validate(v_key, value[v_key], v_value, errors)
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
if HashValidator::Base.strict?
|
25
25
|
value.keys.each do |k|
|
26
26
|
errors[k] = 'key not expected' unless validations[k]
|
@@ -28,7 +28,7 @@ class HashValidator::Validator::HashValidator < HashValidator::Validator::Base
|
|
28
28
|
end
|
29
29
|
|
30
30
|
# Cleanup errors (remove any empty nested errors)
|
31
|
-
errors.delete_if { |
|
31
|
+
errors.delete_if { |_,v| v.empty? }
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module HashValidator
|
2
|
+
module Validator
|
3
|
+
class MultipleValidator < Base
|
4
|
+
def initialize
|
5
|
+
super('_multiple') # The name of the validator, underscored as it won't usually be directly invoked (invoked through use of validator)
|
6
|
+
end
|
7
|
+
|
8
|
+
def should_validate?(validation)
|
9
|
+
validation.is_a?(Validations::Multiple)
|
10
|
+
end
|
11
|
+
|
12
|
+
def validate(key, value, validations, errors)
|
13
|
+
multiple_errors = []
|
14
|
+
|
15
|
+
validations.validations.each do |validation|
|
16
|
+
validation_error = {}
|
17
|
+
::HashValidator.validator_for(validation).validate(key, value, validation, validation_error)
|
18
|
+
multiple_errors << validation_error[key] if validation_error[key]
|
19
|
+
end
|
20
|
+
|
21
|
+
errors[key] = multiple_errors.join(', ') if multiple_errors.any?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
HashValidator.append_validator(HashValidator::Validator::MultipleValidator.new)
|
@@ -7,7 +7,7 @@ class HashValidator::Validator::PresenceValidator < HashValidator::Validator::Ba
|
|
7
7
|
'is required'
|
8
8
|
end
|
9
9
|
|
10
|
-
def validate(key, value,
|
10
|
+
def validate(key, value, _validations, errors)
|
11
11
|
if value.nil?
|
12
12
|
errors[key] = presence_error_message
|
13
13
|
end
|
@@ -12,7 +12,7 @@ class HashValidator::Validator::SimpleValidator < HashValidator::Validator::Base
|
|
12
12
|
self.lambda = lambda
|
13
13
|
end
|
14
14
|
|
15
|
-
def validate(key, value,
|
15
|
+
def validate(key, value, _validations, errors)
|
16
16
|
unless lambda.call(value)
|
17
17
|
errors[key] = presence_error_message
|
18
18
|
end
|
data/spec/hash_validator_spec.rb
CHANGED
@@ -239,6 +239,22 @@ describe HashValidator do
|
|
239
239
|
expect(v.errors).to eq({ bar: 'string required', user: { likes: 'enumerable required' } })
|
240
240
|
end
|
241
241
|
end
|
242
|
+
|
243
|
+
describe 'multiple validations' do
|
244
|
+
let(:validations) {{ foo: 'numeric', user: { age: HashValidator.multiple('numeric', 1..100) } }}
|
245
|
+
|
246
|
+
it 'should validate a complex hash' do
|
247
|
+
v = validate(complex_hash, validations)
|
248
|
+
expect(v.valid?).to eq true
|
249
|
+
expect(v.errors).to be_empty
|
250
|
+
end
|
251
|
+
|
252
|
+
it 'should not validate a complex hash 2' do
|
253
|
+
v = validate(invalid_complex_hash, validations)
|
254
|
+
expect(v.valid?).to eq false
|
255
|
+
expect(v.errors).to eq({ user: { age: 'numeric required, value from list required' } })
|
256
|
+
end
|
257
|
+
end
|
242
258
|
end
|
243
259
|
end
|
244
260
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe HashValidator::Validator::Base do
|
4
|
+
let(:validator) { HashValidator::Validator::MultipleValidator.new }
|
5
|
+
let(:errors) { Hash.new }
|
6
|
+
|
7
|
+
def multiple(*validations)
|
8
|
+
HashValidator::Validations::Multiple.new(validations)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#should_validate?' do
|
12
|
+
it 'should validate an Multiple validation' do
|
13
|
+
expect(validator.should_validate?(multiple('numeric', 1..10))).to eq true
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should not validate other things' do
|
17
|
+
expect(validator.should_validate?('string')).to eq false
|
18
|
+
expect(validator.should_validate?('array')).to eq false
|
19
|
+
expect(validator.should_validate?(nil)).to eq false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#validate' do
|
24
|
+
it 'should accept an empty collection of validators' do
|
25
|
+
validator.validate(:key, 73, multiple(), errors)
|
26
|
+
|
27
|
+
expect(errors).to be_empty
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'should accept an collection of validators' do
|
31
|
+
validator.validate(:key, 73, multiple('numeric', 1..100), errors)
|
32
|
+
|
33
|
+
expect(errors).to be_empty
|
34
|
+
end
|
35
|
+
end
|
36
|
+
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.
|
4
|
+
version: 0.8.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: 2017-
|
11
|
+
date: 2017-06-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -85,6 +85,7 @@ files:
|
|
85
85
|
- lib/hash_validator/base.rb
|
86
86
|
- lib/hash_validator/validations.rb
|
87
87
|
- lib/hash_validator/validations/many.rb
|
88
|
+
- lib/hash_validator/validations/multiple.rb
|
88
89
|
- lib/hash_validator/validations/optional.rb
|
89
90
|
- lib/hash_validator/validators.rb
|
90
91
|
- lib/hash_validator/validators/base.rb
|
@@ -94,6 +95,7 @@ files:
|
|
94
95
|
- lib/hash_validator/validators/hash_validator.rb
|
95
96
|
- lib/hash_validator/validators/lambda_validator.rb
|
96
97
|
- lib/hash_validator/validators/many_validator.rb
|
98
|
+
- lib/hash_validator/validators/multiple_validator.rb
|
97
99
|
- lib/hash_validator/validators/optional_validator.rb
|
98
100
|
- lib/hash_validator/validators/presence_validator.rb
|
99
101
|
- lib/hash_validator/validators/regex_validator.rb
|
@@ -109,6 +111,7 @@ files:
|
|
109
111
|
- spec/validators/in_enumerable_spec.rb
|
110
112
|
- spec/validators/lambda_spec.rb
|
111
113
|
- spec/validators/many_spec.rb
|
114
|
+
- spec/validators/multiple_spec.rb
|
112
115
|
- spec/validators/optional_spec.rb
|
113
116
|
- spec/validators/presence_spec.rb
|
114
117
|
- spec/validators/regexp_spec.rb
|
@@ -149,6 +152,7 @@ test_files:
|
|
149
152
|
- spec/validators/in_enumerable_spec.rb
|
150
153
|
- spec/validators/lambda_spec.rb
|
151
154
|
- spec/validators/many_spec.rb
|
155
|
+
- spec/validators/multiple_spec.rb
|
152
156
|
- spec/validators/optional_spec.rb
|
153
157
|
- spec/validators/presence_spec.rb
|
154
158
|
- spec/validators/regexp_spec.rb
|