hash_validator 0.2.7 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ba6e508f307239eeb92f9fbfcdb6bc9723e7cda6
4
- data.tar.gz: 61651a497d2399d28e0b7119051f9edb51e4a1f9
3
+ metadata.gz: 6d8143f1edb6fe62100d9ae106dc46f46fcc423a
4
+ data.tar.gz: 550feb38aa3d4d0b799d64a8ecbe90ea3b09d750
5
5
  SHA512:
6
- metadata.gz: 98b73f5f7efa1b4af4b6c1224826d8a0bd9f9dd63a21007c76bccf773e9a812988f3dc6deb168b1796ab0706164e295c39022f2672d7be320b87a65d887208ce
7
- data.tar.gz: 913c5a8afd5491276ab5a10dd7b0718ec26b5849ac4688bf7f49c4cc0816928abb02e7fc77aecc9daaa78f9e20cb514406133533c1a397a0efa1720d231fca07
6
+ metadata.gz: e328a46e133e0e7ec35cdcc410ae2f829158598e437b663bf50518ff40fca45dc3bfad2f72705a9e72af4b39342ae2b251bf92d74daa74451fdc754751ed5f06
7
+ data.tar.gz: ef7ec9c9e27921e80ed3420ec92294ed3e1b1f01189243a8d000cbd16aa308265f5d25b5a587d32cb070b9c2fa7065768947ee983fdc22e74b10769158402635
data/.travis.yml CHANGED
@@ -3,5 +3,5 @@ rvm:
3
3
  - 1.9.2
4
4
  - 1.9.3
5
5
  - 2.0.0
6
+ - 2.1.0
6
7
  - jruby-19mode
7
- - rbx-19mode
@@ -0,0 +1,8 @@
1
+ module HashValidator::Validations
2
+ class Many
3
+ attr_reader :validation
4
+ def initialize(validation)
5
+ @validation = validation
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module HashValidator::Validations
2
+ class Optional
3
+ attr_reader :validation
4
+ def initialize(validation)
5
+ @validation = validation
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module HashValidator
2
+ module Validations
3
+ end
4
+ end
5
+
6
+ # Load validators
7
+ require 'hash_validator/validations/optional'
8
+ require 'hash_validator/validations/many'
@@ -0,0 +1,40 @@
1
+ module HashValidator
2
+ module Validator
3
+ class ManyValidator < Base
4
+ def initialize
5
+ super('_many') # 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::Many)
10
+ end
11
+
12
+ def presence_error_message
13
+ "enumerable required"
14
+ end
15
+
16
+ def validate(key, value, validations, errors)
17
+ unless value.is_a?(Enumerable)
18
+ errors[key] = presence_error_message
19
+ return
20
+ end
21
+
22
+ element_errors = Array.new
23
+
24
+ value.each_with_index do |element, i|
25
+ ::HashValidator.validator_for(validations.validation).validate(i, element, validations.validation, element_errors)
26
+ end
27
+
28
+ element_errors.each_with_index do |e, i|
29
+ if e.respond_to?(:empty?) && e.empty?
30
+ element_errors[i] = nil
31
+ end
32
+ end
33
+
34
+ errors[key] = element_errors unless element_errors.all?(&:nil?)
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ HashValidator.append_validator(HashValidator::Validator::ManyValidator.new)
@@ -0,0 +1,22 @@
1
+ module HashValidator
2
+ module Validator
3
+ class OptionalValidator < Base
4
+ def initialize
5
+ super('_optional') # 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::Optional)
10
+ end
11
+
12
+ def validate(key, value, validations, errors)
13
+ if value
14
+ ::HashValidator.validator_for(validations.validation).validate(key, value, validations.validation, errors)
15
+ errors.delete(key) if errors[key].respond_to?(:empty?) && errors[key].empty?
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ HashValidator.append_validator(HashValidator::Validator::OptionalValidator.new)
@@ -32,3 +32,5 @@ require 'hash_validator/validators/boolean_validator'
32
32
  require 'hash_validator/validators/email_validator'
33
33
  require 'hash_validator/validators/enumerable_validator'
34
34
  require 'hash_validator/validators/lambda_validator'
35
+ require 'hash_validator/validators/optional_validator'
36
+ require 'hash_validator/validators/many_validator'
@@ -1,3 +1,3 @@
1
1
  module HashValidator
2
- VERSION = '0.2.7'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -2,8 +2,17 @@ module HashValidator
2
2
  def self.validate(*args)
3
3
  Base.validate(*args)
4
4
  end
5
+
6
+ def self.optional(validation)
7
+ Validations::Optional.new(validation)
8
+ end
9
+
10
+ def self.many(validation)
11
+ Validations::Many.new(validation)
12
+ end
5
13
  end
6
14
 
7
15
  require 'hash_validator/base'
8
16
  require 'hash_validator/version'
9
17
  require 'hash_validator/validators'
18
+ require 'hash_validator/validations'
@@ -188,6 +188,38 @@ describe HashValidator do
188
188
  v.errors.should eq({ bar: 'string required', user: { age: 'is required', likes: 'array required' } })
189
189
  end
190
190
  end
191
+
192
+ describe 'optional validations' do
193
+ let(:validations) {{ foo: 'numeric', bar: HashValidator.optional('string') }}
194
+
195
+ it 'should validate a complex hash' do
196
+ v = validate(complex_hash, validations)
197
+ v.valid?.should be_true
198
+ v.errors.should be_empty
199
+ end
200
+
201
+ it 'should not validate a complex hash 2' do
202
+ v = validate(invalid_complex_hash, validations)
203
+ v.valid?.should be_false
204
+ v.errors.should eq({ bar: 'string required' })
205
+ end
206
+ end
207
+
208
+ describe 'many validations' do
209
+ let(:validations) {{ foo: 'numeric', bar: 'string', user: { first_name: 'string', likes: HashValidator.many('string') } }}
210
+
211
+ it 'should validate a complex hash' do
212
+ v = validate(complex_hash, validations)
213
+ v.valid?.should be_true
214
+ v.errors.should be_empty
215
+ end
216
+
217
+ it 'should not validate a complex hash 2' do
218
+ v = validate(invalid_complex_hash, validations)
219
+ v.valid?.should be_false
220
+ v.errors.should eq({ bar: 'string required', user: { likes: 'enumerable required' } })
221
+ end
222
+ end
191
223
  end
192
224
  end
193
225
  end
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+
3
+ describe HashValidator::Validator::Base do
4
+ let(:validator) { HashValidator::Validator::ManyValidator.new }
5
+ let(:errors) { Hash.new }
6
+
7
+ def many(validation)
8
+ HashValidator::Validations::Many.new(validation)
9
+ end
10
+
11
+ describe '#should_validate?' do
12
+ it 'should validate an Many validation' do
13
+ validator.should_validate?(many('string')).should be_true
14
+ end
15
+
16
+ it 'should not validate other things' do
17
+ validator.should_validate?('string').should be_false
18
+ validator.should_validate?('array').should be_false
19
+ validator.should_validate?(nil).should be_false
20
+ end
21
+ end
22
+
23
+ describe '#validate' do
24
+ it 'should accept an empty array' do
25
+ validator.validate(:key, [], many('string'), errors)
26
+
27
+ errors.should be_empty
28
+ end
29
+
30
+ it 'should accept an array of matching elements' do
31
+ validator.validate(:key, ['a', 'b'], many('string'), errors)
32
+
33
+ errors.should be_empty
34
+ end
35
+
36
+ it 'should not accept an array including a non-matching element' do
37
+ validator.validate(:key, ['a', 2], many('string'), errors)
38
+
39
+ errors.should eq({ key: [nil, 'string required'] })
40
+ end
41
+
42
+ it 'should accept an array of matching hashes' do
43
+ validator.validate(:key, [{v: 'a'}, {v: 'b'}], many({v: 'string'}), errors)
44
+
45
+ errors.should be_empty
46
+ end
47
+
48
+ it 'should not accept an array including a non-matching element' do
49
+ validator.validate(:key, [{v: 'a'}, {v: 2}], many({v: 'string'}), errors)
50
+
51
+ errors.should eq({ key: [nil, {v: 'string required'}] })
52
+ end
53
+
54
+ it 'should not accept a non-enumerable' do
55
+ validator.validate(:key, 'a', many({v: 'string'}), errors)
56
+
57
+ errors.should eq({ key: 'enumerable required' })
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe HashValidator::Validator::Base do
4
+ let(:validator) { HashValidator::Validator::OptionalValidator.new }
5
+ let(:errors) { Hash.new }
6
+
7
+ def optional(validation)
8
+ HashValidator::Validations::Optional.new(validation)
9
+ end
10
+
11
+ describe '#should_validate?' do
12
+ it 'should validate an Optional validation' do
13
+ validator.should_validate?(optional('string')).should be_true
14
+ end
15
+
16
+ it 'should not validate other things' do
17
+ validator.should_validate?('string').should be_false
18
+ validator.should_validate?('array').should be_false
19
+ validator.should_validate?(nil).should be_false
20
+ end
21
+ end
22
+
23
+ describe '#validate' do
24
+ it 'should accept a missing value' do
25
+ validator.validate(:key, nil, optional('string'), errors)
26
+
27
+ errors.should be_empty
28
+ end
29
+
30
+ it 'should accept a present, matching value' do
31
+ validator.validate(:key, 'foo', optional('string'), errors)
32
+
33
+ errors.should be_empty
34
+ end
35
+
36
+ it 'should reject a present, non-matching value' do
37
+ validator.validate(:key, 123, optional('string'), errors)
38
+
39
+ errors.should_not be_empty
40
+ errors.should eq({ key: 'string required' })
41
+ end
42
+
43
+ it 'should accept a present, matching hash' do
44
+ validator.validate(:key, {v: 'foo'}, optional({v: 'string'}), errors)
45
+
46
+ errors.should be_empty
47
+ end
48
+
49
+ it 'should reject a present, non-matching hash' do
50
+ validator.validate(:key, {}, optional({v: 'string'}), errors)
51
+
52
+ errors.should_not be_empty
53
+ errors.should eq({ key: {v: 'string required'} })
54
+ end
55
+ end
56
+ end
metadata CHANGED
@@ -1,69 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.3.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: 2013-07-25 00:00:00.000000000 Z
11
+ date: 2014-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: 2.13.0
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 2.13.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: coveralls
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: Ruby library to validate hashes (Hash) against user-defined requirements
@@ -73,9 +73,9 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
- - .gitignore
77
- - .rspec
78
- - .travis.yml
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
79
  - Gemfile
80
80
  - LICENSE.txt
81
81
  - README.md
@@ -83,6 +83,9 @@ files:
83
83
  - hash_validator.gemspec
84
84
  - lib/hash_validator.rb
85
85
  - lib/hash_validator/base.rb
86
+ - lib/hash_validator/validations.rb
87
+ - lib/hash_validator/validations/many.rb
88
+ - lib/hash_validator/validations/optional.rb
86
89
  - lib/hash_validator/validators.rb
87
90
  - lib/hash_validator/validators/base.rb
88
91
  - lib/hash_validator/validators/boolean_validator.rb
@@ -90,6 +93,8 @@ files:
90
93
  - lib/hash_validator/validators/enumerable_validator.rb
91
94
  - lib/hash_validator/validators/hash_validator.rb
92
95
  - lib/hash_validator/validators/lambda_validator.rb
96
+ - lib/hash_validator/validators/many_validator.rb
97
+ - lib/hash_validator/validators/optional_validator.rb
93
98
  - lib/hash_validator/validators/presence_validator.rb
94
99
  - lib/hash_validator/validators/simple_type_validators.rb
95
100
  - lib/hash_validator/validators/simple_validator.rb
@@ -102,6 +107,8 @@ files:
102
107
  - spec/validators/email_spec.rb
103
108
  - spec/validators/in_enumerable_spec.rb
104
109
  - spec/validators/lambda_spec.rb
110
+ - spec/validators/many_spec.rb
111
+ - spec/validators/optional_spec.rb
105
112
  - spec/validators/presence_spec.rb
106
113
  - spec/validators/simple_spec.rb
107
114
  - spec/validators/simple_types_spec.rb
@@ -116,17 +123,17 @@ require_paths:
116
123
  - lib
117
124
  required_ruby_version: !ruby/object:Gem::Requirement
118
125
  requirements:
119
- - - '>='
126
+ - - ">="
120
127
  - !ruby/object:Gem::Version
121
128
  version: '0'
122
129
  required_rubygems_version: !ruby/object:Gem::Requirement
123
130
  requirements:
124
- - - '>='
131
+ - - ">="
125
132
  - !ruby/object:Gem::Version
126
133
  version: '0'
127
134
  requirements: []
128
135
  rubyforge_project:
129
- rubygems_version: 2.0.0
136
+ rubygems_version: 2.2.2
130
137
  signing_key:
131
138
  specification_version: 4
132
139
  summary: Ruby library to validate hashes (Hash) against user-defined requirements
@@ -139,6 +146,8 @@ test_files:
139
146
  - spec/validators/email_spec.rb
140
147
  - spec/validators/in_enumerable_spec.rb
141
148
  - spec/validators/lambda_spec.rb
149
+ - spec/validators/many_spec.rb
150
+ - spec/validators/optional_spec.rb
142
151
  - spec/validators/presence_spec.rb
143
152
  - spec/validators/simple_spec.rb
144
153
  - spec/validators/simple_types_spec.rb