hash_validator 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/lib/hash_validator/validators.rb +1 -0
- data/lib/hash_validator/validators/lambda_validator.rb +35 -0
- data/lib/hash_validator/version.rb +1 -1
- data/spec/validators/lambda_spec.rb +51 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b0b6578e955c0313a9b21da8f3283ca64c5a292d
|
4
|
+
data.tar.gz: f359b808a6471c3699b5bd7cf73e46ed120f81d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 634064627003e36fd25b50b8a6235a5d37dc635465c0427a4427743580b87b26e29f1df8b52ebe938d8565eaf83582aa696d151716a298db161c3edb86926097
|
7
|
+
data.tar.gz: 190e54cedb8e9745bc4fe7dc0f01875f39f875bfe7f36d275ef76c5d9aa2d0115c5081918a6d86612da3bdb2a519b0880546d0d8ab50c7c00fd453a004e6334a
|
data/README.md
CHANGED
@@ -83,6 +83,7 @@ Define a validation hash which will be used to validate. This has can be nested
|
|
83
83
|
Additional validations exist to validate beyond simple typing, such as:
|
84
84
|
|
85
85
|
* An Enumerable instance: validates that the value is contained within the supplied enumerable.
|
86
|
+
* A lambda/Proc instance: validates that the lambda/proc returns true when the value is supplied (lambdas must accept only one argument).
|
86
87
|
* `email`: email address validation (string + email address).
|
87
88
|
|
88
89
|
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).
|
@@ -31,3 +31,4 @@ 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/lambda_validator'
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class HashValidator::Validator::LambdaValidator < HashValidator::Validator::Base
|
2
|
+
def initialize
|
3
|
+
super('_lambda') # 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
|
+
if rhs.is_a?(Proc)
|
8
|
+
if rhs.arity == 1
|
9
|
+
true
|
10
|
+
else
|
11
|
+
raise HashValidator::Validator::LambdaValidator::InvalidArgumentCount.new("Lambda validator should only accept one argument, supplied lambda accepts #{rhs.arity}.")
|
12
|
+
end
|
13
|
+
else
|
14
|
+
false
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def presence_error_message
|
19
|
+
'is not valid'
|
20
|
+
end
|
21
|
+
|
22
|
+
def validate(key, value, lambda, errors)
|
23
|
+
unless lambda.call(value)
|
24
|
+
errors[key] = presence_error_message
|
25
|
+
end
|
26
|
+
|
27
|
+
rescue
|
28
|
+
errors[key] = presence_error_message
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class HashValidator::Validator::LambdaValidator::InvalidArgumentCount < StandardError
|
33
|
+
end
|
34
|
+
|
35
|
+
HashValidator.append_validator(HashValidator::Validator::LambdaValidator.new)
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Functional validator' do
|
4
|
+
describe 'Accepting Lambdas in validations' do
|
5
|
+
it 'should accept a lambda' do
|
6
|
+
validate({}, { foo: lambda { |arg| } })
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should accept a lambda (new syntax)' do
|
10
|
+
validate({}, { foo: -> (arg) {} })
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should accept a proc' do
|
14
|
+
validate({}, { foo: Proc.new { |arg| } })
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'Correct number of arguments for lambads in validations' do
|
19
|
+
it 'should accept a lambda with one argument' do
|
20
|
+
expect { validate({}, { foo: -> (arg) {} }) }.to_not raise_error
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should not accept a lambda with no arguments' do
|
24
|
+
expect { validate({}, { foo: -> {} }) }.to raise_error(HashValidator::Validator::LambdaValidator::InvalidArgumentCount)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'should not accept a lambda with two arguments' do
|
28
|
+
expect { validate({}, { foo: -> (a,b) {} }) }.to raise_error(HashValidator::Validator::LambdaValidator::InvalidArgumentCount)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#validate' do
|
33
|
+
let(:validations) {{ number: -> (n) { n.odd? } }}
|
34
|
+
|
35
|
+
it 'should validate true when the number is odd' do
|
36
|
+
validate({ number: 1 }, validations).valid?.should be_true
|
37
|
+
end
|
38
|
+
|
39
|
+
it 'should validate false when the number is even' do
|
40
|
+
validate({ number: 2 }, validations).valid?.should be_false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe 'Thrown exceptions from within the lambda' do
|
45
|
+
let(:validations) {{ number: -> (n) { n.odd? } }}
|
46
|
+
|
47
|
+
it 'should validate false when an exception occurs within the lambda' do
|
48
|
+
validate({ number: '2' }, validations).valid?.should be_false
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Brooks
|
@@ -89,6 +89,7 @@ files:
|
|
89
89
|
- lib/hash_validator/validators/email_validator.rb
|
90
90
|
- lib/hash_validator/validators/enumerable_validator.rb
|
91
91
|
- lib/hash_validator/validators/hash_validator.rb
|
92
|
+
- lib/hash_validator/validators/lambda_validator.rb
|
92
93
|
- lib/hash_validator/validators/presence_validator.rb
|
93
94
|
- lib/hash_validator/validators/simple_type_validators.rb
|
94
95
|
- lib/hash_validator/validators/simple_validator.rb
|
@@ -100,6 +101,7 @@ files:
|
|
100
101
|
- spec/validators/boolean_spec.rb
|
101
102
|
- spec/validators/email_spec.rb
|
102
103
|
- spec/validators/in_enumerable_spec.rb
|
104
|
+
- spec/validators/lambda_spec.rb
|
103
105
|
- spec/validators/presence_spec.rb
|
104
106
|
- spec/validators/simple_spec.rb
|
105
107
|
- spec/validators/simple_types_spec.rb
|
@@ -136,6 +138,7 @@ test_files:
|
|
136
138
|
- spec/validators/boolean_spec.rb
|
137
139
|
- spec/validators/email_spec.rb
|
138
140
|
- spec/validators/in_enumerable_spec.rb
|
141
|
+
- spec/validators/lambda_spec.rb
|
139
142
|
- spec/validators/presence_spec.rb
|
140
143
|
- spec/validators/simple_spec.rb
|
141
144
|
- spec/validators/simple_types_spec.rb
|