hash_validator 0.2.2 → 0.2.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 09fcad8b45bef6d76eff9d52feec81e116383136
4
- data.tar.gz: 0be312504f4b8365d39cf4641a9533c2d010c209
3
+ metadata.gz: ef7bafe9d1c4d43bec0450101f7849224c43ccbe
4
+ data.tar.gz: bf2cf47a021f8a01d84a1cbb7344e73be0cf0026
5
5
  SHA512:
6
- metadata.gz: 1c2f78699dddc2bd3652ae9ef371cebdf0ad560b361621c882c8c06bc7ebdd89ad3588f18fc29f9aa7348119ea0f02b588c6cf71eff58dc9da357d04301eadc4
7
- data.tar.gz: fb82f77a46c71ca978b19919aa511e467d9ea45265bb652f4fadbf069b5f47801582e8372f1ccd382602582375d871ffd7782e92fae7121fbc2c8b881a0c5f80
6
+ metadata.gz: 2b5ac97feaac7377ae57fc21cf1bc1910a532f0e1e413d1a2d40c10efc1ac45431544a207fd2585533216d310da8cdfe2cdbc0b5ca727435cd271c4b20d318db
7
+ data.tar.gz: 8978b24518ef2cfdf99d86ea2dd1b3380cc04c3f60c1931f119854648c51a37f1ca1ba6735c19cc80b4ced4e29a975453a1178b4631830be10e94c9a75240463
data/README.md CHANGED
@@ -65,6 +65,7 @@ validator.errors
65
65
  Define a validation hash which will be used to validate. This has can be nested as deeply as required using the following values to validate specific value types:
66
66
 
67
67
  * `array`
68
+ * `boolean`
68
69
  * `complex`
69
70
  * `float`
70
71
  * `integer`
@@ -28,4 +28,5 @@ require 'hash_validator/validators/simple_validator'
28
28
  require 'hash_validator/validators/hash_validator'
29
29
  require 'hash_validator/validators/presence_validator'
30
30
  require 'hash_validator/validators/simple_type_validators'
31
+ require 'hash_validator/validators/boolean_validator'
31
32
  require 'hash_validator/validators/email_validator'
@@ -0,0 +1,13 @@
1
+ class HashValidator::Validator::BooleanValidator < HashValidator::Validator::Base
2
+ def initialize
3
+ super('boolean') # The name of the validator
4
+ end
5
+
6
+ def validate(key, value, validations, errors)
7
+ unless [TrueClass, FalseClass].include?(value.class)
8
+ errors[key] = presence_error_message
9
+ end
10
+ end
11
+ end
12
+
13
+ HashValidator.append_validator(HashValidator::Validator::BooleanValidator.new)
@@ -1,3 +1,3 @@
1
1
  module HashValidator
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe HashValidator::Validator::Base do
4
+ let(:validator) { HashValidator::Validator::BooleanValidator.new }
5
+ let(:errors) { Hash.new }
6
+
7
+ describe '#should_validate?' do
8
+ it 'should validate the name "boolean"' do
9
+ validator.should_validate?('boolean').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 true boolean with true' do
21
+ validator.validate(:key, true, {}, errors)
22
+
23
+ errors.should be_empty
24
+ end
25
+
26
+ it 'should validate a false boolean with true' do
27
+ validator.validate(:key, false, {}, errors)
28
+
29
+ errors.should be_empty
30
+ end
31
+
32
+ it 'should validate a nil with false' do
33
+ validator.validate(:key, nil, {}, errors)
34
+
35
+ errors.should_not be_empty
36
+ errors.should eq({ key: 'boolean required' })
37
+ end
38
+
39
+ it 'should validate a number with false' do
40
+ validator.validate(:key, 123, {}, errors)
41
+
42
+ errors.should_not be_empty
43
+ errors.should eq({ key: 'boolean required' })
44
+ end
45
+ end
46
+ 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.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Brooks
@@ -85,6 +85,7 @@ files:
85
85
  - lib/hash_validator/base.rb
86
86
  - lib/hash_validator/validators.rb
87
87
  - lib/hash_validator/validators/base.rb
88
+ - lib/hash_validator/validators/boolean_validator.rb
88
89
  - lib/hash_validator/validators/email_validator.rb
89
90
  - lib/hash_validator/validators/hash_validator.rb
90
91
  - lib/hash_validator/validators/presence_validator.rb
@@ -95,6 +96,7 @@ files:
95
96
  - spec/hash_validator_spec_helper.rb
96
97
  - spec/spec_helper.rb
97
98
  - spec/validators/base_spec.rb
99
+ - spec/validators/boolean_spec.rb
98
100
  - spec/validators/email_spec.rb
99
101
  - spec/validators/presence_spec.rb
100
102
  - spec/validators/simple_spec.rb
@@ -129,6 +131,7 @@ test_files:
129
131
  - spec/hash_validator_spec_helper.rb
130
132
  - spec/spec_helper.rb
131
133
  - spec/validators/base_spec.rb
134
+ - spec/validators/boolean_spec.rb
132
135
  - spec/validators/email_spec.rb
133
136
  - spec/validators/presence_spec.rb
134
137
  - spec/validators/simple_spec.rb