hash_validator 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -6
- data/lib/hash_validator/validators/regex_validator.rb +21 -0
- data/lib/hash_validator/validators.rb +1 -0
- data/lib/hash_validator/version.rb +1 -1
- data/spec/hash_validator_spec.rb +10 -1
- data/spec/validators/regexp_spec.rb +26 -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: 6d59a9fcf0e3e3d923d612488af9ff65634ec245
|
4
|
+
data.tar.gz: 5930e9f250b9782b934bb96911af050fcf298caf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d027e7bb81c1cd5c5da47d8d50a3c5f5e7fc6d580f28e6e4726d8c6f83d179330a5b7a2826c1d99e383b61d33d1de4a88322de13e124b32cc9a59da2d672e59b
|
7
|
+
data.tar.gz: 8f282ee2705a7fd84f63675c1ce186984325a52a5552b4cc449ff04a5fd32514d9a8941452358845570496742d9f8b6fd33308b188d4816a49522b2531b4517b
|
data/.travis.yml
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
class HashValidator::Validator::RegexpValidator < HashValidator::Validator::Base
|
2
|
+
def initialize
|
3
|
+
super('_regex') # 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?(Regexp)
|
8
|
+
end
|
9
|
+
|
10
|
+
def presence_error_message
|
11
|
+
'does not match regular expression'
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate(key, value, regexp, errors)
|
15
|
+
unless regexp.match(value.to_s)
|
16
|
+
errors[key] = presence_error_message
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
HashValidator.append_validator(HashValidator::Validator::RegexpValidator.new)
|
@@ -31,6 +31,7 @@ require 'hash_validator/validators/simple_type_validators'
|
|
31
31
|
require 'hash_validator/validators/boolean_validator'
|
32
32
|
require 'hash_validator/validators/email_validator'
|
33
33
|
require 'hash_validator/validators/enumerable_validator'
|
34
|
+
require 'hash_validator/validators/regex_validator'
|
34
35
|
require 'hash_validator/validators/lambda_validator'
|
35
36
|
require 'hash_validator/validators/optional_validator'
|
36
37
|
require 'hash_validator/validators/many_validator'
|
data/spec/hash_validator_spec.rb
CHANGED
@@ -184,7 +184,16 @@ describe HashValidator do
|
|
184
184
|
end
|
185
185
|
|
186
186
|
describe 'nested validations' do
|
187
|
-
let(:validations) {{
|
187
|
+
let(:validations) {{
|
188
|
+
foo: 'numeric',
|
189
|
+
bar: 'string',
|
190
|
+
user: {
|
191
|
+
first_name: 'string',
|
192
|
+
last_name: /^(Brooks|Smith)$/,
|
193
|
+
age: 'required',
|
194
|
+
likes: 'array'
|
195
|
+
}
|
196
|
+
}}
|
188
197
|
|
189
198
|
it 'should validate a complex hash' do
|
190
199
|
v = validate(complex_hash, validations)
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Regular expression validator' do
|
4
|
+
describe 'Accepting RegExps in validations' do
|
5
|
+
it 'should accept a regexp' do
|
6
|
+
validate({}, { foo: // })
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
describe '#validate' do
|
11
|
+
let(:validations) {{ string: /^foo$/ }}
|
12
|
+
|
13
|
+
it 'should validate true when the value is foo' do
|
14
|
+
expect(validate({ string: 'foo' }, validations).valid?).to eq true
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should validate false when the value is not foo' do
|
18
|
+
expect(validate({ string: 'bar' }, validations).valid?).to eq false
|
19
|
+
expect(validate({ string: ' foo' }, validations).valid?).to eq false
|
20
|
+
expect(validate({ string: 'foo ' }, validations).valid?).to eq false
|
21
|
+
expect(validate({ string: nil }, validations).valid?).to eq false
|
22
|
+
expect(validate({ string: 0 }, validations).valid?).to eq false
|
23
|
+
expect(validate({ string: true }, validations).valid?).to eq false
|
24
|
+
end
|
25
|
+
end
|
26
|
+
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.7.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: 2016-
|
11
|
+
date: 2016-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -96,6 +96,7 @@ files:
|
|
96
96
|
- lib/hash_validator/validators/many_validator.rb
|
97
97
|
- lib/hash_validator/validators/optional_validator.rb
|
98
98
|
- lib/hash_validator/validators/presence_validator.rb
|
99
|
+
- lib/hash_validator/validators/regex_validator.rb
|
99
100
|
- lib/hash_validator/validators/simple_type_validators.rb
|
100
101
|
- lib/hash_validator/validators/simple_validator.rb
|
101
102
|
- lib/hash_validator/version.rb
|
@@ -110,6 +111,7 @@ files:
|
|
110
111
|
- spec/validators/many_spec.rb
|
111
112
|
- spec/validators/optional_spec.rb
|
112
113
|
- spec/validators/presence_spec.rb
|
114
|
+
- spec/validators/regexp_spec.rb
|
113
115
|
- spec/validators/simple_spec.rb
|
114
116
|
- spec/validators/simple_types_spec.rb
|
115
117
|
- spec/validators/user_defined_spec.rb
|
@@ -149,6 +151,7 @@ test_files:
|
|
149
151
|
- spec/validators/many_spec.rb
|
150
152
|
- spec/validators/optional_spec.rb
|
151
153
|
- spec/validators/presence_spec.rb
|
154
|
+
- spec/validators/regexp_spec.rb
|
152
155
|
- spec/validators/simple_spec.rb
|
153
156
|
- spec/validators/simple_types_spec.rb
|
154
157
|
- spec/validators/user_defined_spec.rb
|