hash_validator 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hash_validator/base.rb +9 -5
- data/lib/hash_validator/validators/hash_validator.rb +6 -0
- data/lib/hash_validator/version.rb +1 -1
- data/spec/hash_validator_spec.rb +111 -75
- data/spec/hash_validator_spec_helper.rb +2 -2
- data/spec/spec_helper.rb +1 -4
- data/spec/validators/boolean_spec.rb +10 -10
- data/spec/validators/email_spec.rb +9 -9
- data/spec/validators/in_enumerable_spec.rb +9 -9
- data/spec/validators/lambda_spec.rb +3 -3
- data/spec/validators/many_spec.rb +10 -10
- data/spec/validators/optional_spec.rb +11 -11
- data/spec/validators/presence_spec.rb +9 -9
- data/spec/validators/simple_spec.rb +2 -2
- data/spec/validators/simple_types_spec.rb +4 -4
- data/spec/validators/user_defined_spec.rb +18 -18
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a411f83813e65b6e88f8e71843fedec735ced2a
|
4
|
+
data.tar.gz: bdfe6c8004a6fd92d7e844b5e21f0c8fc893a2ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c7749ca2745434d37ca9b7cba13475c69d2fc40e9167b9b82cd1666782c324349f771030d78cd76f41a4880e1744360b4fcd5746769a5ccc34b0c813197673a6
|
7
|
+
data.tar.gz: c4ce6d5789409e9ff8d6f1dd6c6fe55791a6f085f66deb735eb8b49a808396d4c42f346ba86ccf9b2f4885f8be54d0ef50622c0ffaec51794bd2b5e05264b468
|
data/lib/hash_validator/base.rb
CHANGED
@@ -6,7 +6,7 @@ class HashValidator::Base
|
|
6
6
|
self.errors = {}
|
7
7
|
self.hash = hash
|
8
8
|
self.validations = validations
|
9
|
-
|
9
|
+
|
10
10
|
validate
|
11
11
|
end
|
12
12
|
|
@@ -14,12 +14,16 @@ class HashValidator::Base
|
|
14
14
|
errors.empty?
|
15
15
|
end
|
16
16
|
|
17
|
-
def self.validate(hash, validations)
|
17
|
+
def self.validate(hash, validations, strict = false)
|
18
|
+
@strict = strict
|
18
19
|
new(hash, validations)
|
19
20
|
end
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
|
22
|
+
def self.strict?
|
23
|
+
@strict
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
23
27
|
def validate
|
24
28
|
HashValidator.validator_for(hash).validate(:base, self.hash, self.validations, self.errors)
|
25
29
|
self.errors = errors[:base]
|
@@ -20,6 +20,12 @@ 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
|
+
|
24
|
+
if HashValidator::Base.strict?
|
25
|
+
value.keys.each do |k|
|
26
|
+
errors[k] = 'key not expected' unless validations[k]
|
27
|
+
end
|
28
|
+
end
|
23
29
|
|
24
30
|
# Cleanup errors (remove any empty nested errors)
|
25
31
|
errors.delete_if { |k,v| v.empty? }
|
data/spec/hash_validator_spec.rb
CHANGED
@@ -28,46 +28,46 @@ describe HashValidator do
|
|
28
28
|
describe '#validate' do
|
29
29
|
describe 'individual type validations' do
|
30
30
|
it 'should validate hash' do
|
31
|
-
validate({ v: {} }, { v: {} }).valid
|
31
|
+
expect(validate({ v: {} }, { v: {} }).valid?).to eq true
|
32
32
|
|
33
|
-
validate({ v: '' }, { v: {} }).valid
|
34
|
-
validate({ v: '' }, { v: {} }).errors.
|
33
|
+
expect(validate({ v: '' }, { v: {} }).valid?).to eq false
|
34
|
+
expect(validate({ v: '' }, { v: {} }).errors).to eq({ v: 'hash required' })
|
35
35
|
end
|
36
36
|
|
37
37
|
it 'should validate presence' do
|
38
|
-
validate({ v: 'test' }, { v: 'required' }).valid
|
39
|
-
validate({ v: 1234 }, { v: 'required' }).valid
|
38
|
+
expect(validate({ v: 'test' }, { v: 'required' }).valid?).to eq true
|
39
|
+
expect(validate({ v: 1234 }, { v: 'required' }).valid?).to eq true
|
40
40
|
|
41
|
-
validate({ v: nil }, { v: 'required' }).valid
|
42
|
-
validate({ v: nil }, { v: 'required' }).errors.
|
41
|
+
expect(validate({ v: nil }, { v: 'required' }).valid?).to eq false
|
42
|
+
expect(validate({ v: nil }, { v: 'required' }).errors).to eq({ v: 'is required' })
|
43
43
|
|
44
|
-
validate({ x: 'test' }, { v: 'required' }).valid
|
45
|
-
validate({ x: 'test' }, { v: 'required' }).errors.
|
44
|
+
expect(validate({ x: 'test' }, { v: 'required' }).valid?).to eq false
|
45
|
+
expect(validate({ x: 'test' }, { v: 'required' }).errors).to eq({ v: 'is required' })
|
46
46
|
|
47
|
-
validate({ x: 1234 }, { v: 'required' }).valid
|
48
|
-
validate({ x: 1234 }, { v: 'required' }).errors.
|
47
|
+
expect(validate({ x: 1234 }, { v: 'required' }).valid?).to eq false
|
48
|
+
expect(validate({ x: 1234 }, { v: 'required' }).errors).to eq({ v: 'is required' })
|
49
49
|
end
|
50
50
|
|
51
51
|
it 'should validate string' do
|
52
|
-
validate({ v: 'test' }, { v: 'string' }).valid
|
52
|
+
expect(validate({ v: 'test' }, { v: 'string' }).valid?).to eq true
|
53
53
|
|
54
|
-
validate({ v: 123456 }, { v: 'string' }).valid
|
55
|
-
validate({ v: 123456 }, { v: 'string' }).errors.
|
54
|
+
expect(validate({ v: 123456 }, { v: 'string' }).valid?).to eq false
|
55
|
+
expect(validate({ v: 123456 }, { v: 'string' }).errors).to eq({ v: 'string required' })
|
56
56
|
end
|
57
57
|
|
58
58
|
it 'should validate numeric' do
|
59
|
-
validate({ v: 1234 }, { v: 'numeric' }).valid
|
60
|
-
validate({ v: '12' }, { v: 'numeric' }).valid
|
59
|
+
expect(validate({ v: 1234 }, { v: 'numeric' }).valid?).to eq true
|
60
|
+
expect(validate({ v: '12' }, { v: 'numeric' }).valid?).to eq false
|
61
61
|
end
|
62
62
|
|
63
63
|
it 'should validate array' do
|
64
|
-
validate({ v: [ 1,2,3 ] }, { v: 'array' }).valid
|
65
|
-
validate({ v: ' 1,2,3 ' }, { v: 'array' }).valid
|
64
|
+
expect(validate({ v: [ 1,2,3 ] }, { v: 'array' }).valid?).to eq true
|
65
|
+
expect(validate({ v: ' 1,2,3 ' }, { v: 'array' }).valid?).to eq false
|
66
66
|
end
|
67
67
|
|
68
68
|
it 'should validate time' do
|
69
|
-
validate({ v: Time.now }, { v: 'time' }).valid
|
70
|
-
validate({ v: '2013-04-12 13:18:05 +0930' }, { v: 'time' }).valid
|
69
|
+
expect(validate({ v: Time.now }, { v: 'time' }).valid?).to eq true
|
70
|
+
expect(validate({ v: '2013-04-12 13:18:05 +0930' }, { v: 'time' }).valid?).to eq false
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
@@ -75,67 +75,67 @@ describe HashValidator do
|
|
75
75
|
let(:empty_hash) {{}}
|
76
76
|
|
77
77
|
let(:simple_hash) {{
|
78
|
-
|
79
|
-
|
80
|
-
|
78
|
+
foo: 1,
|
79
|
+
bar: 'baz'
|
80
|
+
}}
|
81
81
|
|
82
82
|
let(:invalid_simple_hash) {{
|
83
|
-
|
84
|
-
|
85
|
-
|
83
|
+
foo: 1,
|
84
|
+
bar: 2
|
85
|
+
}}
|
86
86
|
|
87
87
|
let(:complex_hash) {{
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
88
|
+
foo: 1,
|
89
|
+
bar: 'baz',
|
90
|
+
user: {
|
91
|
+
first_name: 'James',
|
92
|
+
last_name: 'Brooks',
|
93
|
+
age: 27,
|
94
|
+
likes: [ 'Ruby', 'Kendo', 'Board Games' ]
|
95
|
+
}
|
96
|
+
}}
|
97
97
|
|
98
98
|
let(:invalid_complex_hash) {{
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
99
|
+
foo: 1,
|
100
|
+
bar: 2,
|
101
|
+
user: {
|
102
|
+
first_name: 'James',
|
103
|
+
last_name: 'Brooks',
|
104
|
+
likes: 'Ruby, Kendo, Board Games'
|
105
|
+
}
|
106
|
+
}}
|
107
107
|
|
108
108
|
describe 'no validations' do
|
109
109
|
let(:validations) {{}}
|
110
110
|
|
111
111
|
it 'should validate an empty hash' do
|
112
112
|
v = validate(empty_hash, validations)
|
113
|
-
v.valid
|
114
|
-
v.errors.
|
113
|
+
expect(v.valid?).to eq true
|
114
|
+
expect(v.errors).to be_empty
|
115
115
|
end
|
116
116
|
|
117
117
|
it 'should validate a simple hash' do
|
118
118
|
v = validate(simple_hash, validations)
|
119
|
-
v.valid
|
120
|
-
v.errors.
|
119
|
+
expect(v.valid?).to eq true
|
120
|
+
expect(v.errors).to be_empty
|
121
121
|
end
|
122
122
|
|
123
123
|
it 'should validate a simple hash 2' do
|
124
124
|
v = validate(invalid_simple_hash, validations)
|
125
|
-
v.valid
|
126
|
-
v.errors.
|
125
|
+
expect(v.valid?).to eq true
|
126
|
+
expect(v.errors).to be_empty
|
127
127
|
end
|
128
128
|
|
129
129
|
it 'should validate a complex hash' do
|
130
130
|
v = validate(complex_hash, validations)
|
131
|
-
v.valid
|
132
|
-
v.errors.
|
131
|
+
expect(v.valid?).to eq true
|
132
|
+
expect(v.errors).to be_empty
|
133
133
|
end
|
134
134
|
|
135
135
|
it 'should validate a complex hash 2' do
|
136
136
|
v = validate(invalid_complex_hash, validations)
|
137
|
-
v.valid
|
138
|
-
v.errors.
|
137
|
+
expect(v.valid?).to eq true
|
138
|
+
expect(v.errors).to be_empty
|
139
139
|
end
|
140
140
|
end
|
141
141
|
|
@@ -144,32 +144,32 @@ describe HashValidator do
|
|
144
144
|
|
145
145
|
it 'should not validate an empty hash (stating missing with required)' do
|
146
146
|
v = validate(empty_hash, validations)
|
147
|
-
v.valid
|
148
|
-
v.errors.
|
147
|
+
expect(v.valid?).to eq false
|
148
|
+
expect(v.errors).to eq({ foo: 'numeric required', bar: 'string required' })
|
149
149
|
end
|
150
150
|
|
151
151
|
it 'should validate a simple hash' do
|
152
152
|
v = validate(simple_hash, validations)
|
153
|
-
v.valid
|
154
|
-
v.errors.
|
153
|
+
expect(v.valid?).to eq true
|
154
|
+
expect(v.errors).to be_empty
|
155
155
|
end
|
156
156
|
|
157
157
|
it 'should not validate a simple hash 2' do
|
158
158
|
v = validate(invalid_simple_hash, validations)
|
159
|
-
v.valid
|
160
|
-
v.errors.
|
159
|
+
expect(v.valid?).to eq false
|
160
|
+
expect(v.errors).to eq({ bar: 'string required' })
|
161
161
|
end
|
162
162
|
|
163
163
|
it 'should validate a complex hash' do
|
164
164
|
v = validate(complex_hash, validations)
|
165
|
-
v.valid
|
166
|
-
v.errors.
|
165
|
+
expect(v.valid?).to eq true
|
166
|
+
expect(v.errors).to be_empty
|
167
167
|
end
|
168
168
|
|
169
169
|
it 'should not validate a complex hash 2' do
|
170
170
|
v = validate(invalid_complex_hash, validations)
|
171
|
-
v.valid
|
172
|
-
v.errors.
|
171
|
+
expect(v.valid?).to eq false
|
172
|
+
expect(v.errors).to eq({ bar: 'string required' })
|
173
173
|
end
|
174
174
|
end
|
175
175
|
|
@@ -178,14 +178,14 @@ describe HashValidator do
|
|
178
178
|
|
179
179
|
it 'should validate a complex hash' do
|
180
180
|
v = validate(complex_hash, validations)
|
181
|
-
v.valid
|
182
|
-
v.errors.
|
181
|
+
expect(v.valid?).to eq true
|
182
|
+
expect(v.errors).to be_empty
|
183
183
|
end
|
184
184
|
|
185
185
|
it 'should not validate a complex hash 2' do
|
186
186
|
v = validate(invalid_complex_hash, validations)
|
187
|
-
v.valid
|
188
|
-
v.errors.
|
187
|
+
expect(v.valid?).to eq false
|
188
|
+
expect(v.errors).to eq({ bar: 'string required', user: { age: 'is required', likes: 'array required' } })
|
189
189
|
end
|
190
190
|
end
|
191
191
|
|
@@ -194,14 +194,14 @@ describe HashValidator do
|
|
194
194
|
|
195
195
|
it 'should validate a complex hash' do
|
196
196
|
v = validate(complex_hash, validations)
|
197
|
-
v.valid
|
198
|
-
v.errors.
|
197
|
+
expect(v.valid?).to eq true
|
198
|
+
expect(v.errors).to be_empty
|
199
199
|
end
|
200
200
|
|
201
201
|
it 'should not validate a complex hash 2' do
|
202
202
|
v = validate(invalid_complex_hash, validations)
|
203
|
-
v.valid
|
204
|
-
v.errors.
|
203
|
+
expect(v.valid?).to eq false
|
204
|
+
expect(v.errors).to eq({ bar: 'string required' })
|
205
205
|
end
|
206
206
|
end
|
207
207
|
|
@@ -210,16 +210,52 @@ describe HashValidator do
|
|
210
210
|
|
211
211
|
it 'should validate a complex hash' do
|
212
212
|
v = validate(complex_hash, validations)
|
213
|
-
v.valid
|
214
|
-
v.errors.
|
213
|
+
expect(v.valid?).to eq true
|
214
|
+
expect(v.errors).to be_empty
|
215
215
|
end
|
216
216
|
|
217
217
|
it 'should not validate a complex hash 2' do
|
218
218
|
v = validate(invalid_complex_hash, validations)
|
219
|
-
v.valid
|
220
|
-
v.errors.
|
219
|
+
expect(v.valid?).to eq false
|
220
|
+
expect(v.errors).to eq({ bar: 'string required', user: { likes: 'enumerable required' } })
|
221
221
|
end
|
222
222
|
end
|
223
223
|
end
|
224
224
|
end
|
225
225
|
end
|
226
|
+
|
227
|
+
describe 'Strict Validation' do
|
228
|
+
let(:simple_hash) { { foo: 'bar', bar: 'foo' } }
|
229
|
+
|
230
|
+
let(:complex_hash) {{
|
231
|
+
foo: 1,
|
232
|
+
user: {
|
233
|
+
first_name: 'James',
|
234
|
+
last_name: 'Brooks',
|
235
|
+
age: 27,
|
236
|
+
likes: [ 'Ruby', 'Kendo', 'Board Games' ]
|
237
|
+
}
|
238
|
+
}}
|
239
|
+
|
240
|
+
let(:validations) { { foo: 'string' } }
|
241
|
+
|
242
|
+
let(:complex_validations) {{
|
243
|
+
foo: 'integer',
|
244
|
+
user: {
|
245
|
+
first_name: 'string', age: 'integer'
|
246
|
+
}
|
247
|
+
}}
|
248
|
+
|
249
|
+
it 'reports which keys are not expected for a simple hash' do
|
250
|
+
v = validate(simple_hash, validations, true)
|
251
|
+
expect(v.valid?).to eq false
|
252
|
+
expect(v.errors).to eq({ bar: 'key not expected' })
|
253
|
+
end
|
254
|
+
|
255
|
+
it 'reports which keys are not expected for a complex hash' do
|
256
|
+
v = validate(complex_hash, complex_validations, true)
|
257
|
+
expect(v.valid?).to eq false
|
258
|
+
expect(v.errors).to eq(user: { last_name: 'key not expected', likes: 'key not expected' })
|
259
|
+
end
|
260
|
+
|
261
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,16 +2,13 @@ require 'coveralls'
|
|
2
2
|
Coveralls.wear!
|
3
3
|
|
4
4
|
require 'rubygems'
|
5
|
-
require 'bundler/setup'
|
6
5
|
require 'hash_validator'
|
7
6
|
require 'hash_validator_spec_helper'
|
8
7
|
|
9
8
|
RSpec.configure do |config|
|
10
|
-
config.treat_symbols_as_metadata_keys_with_true_values = true
|
11
9
|
config.run_all_when_everything_filtered = true
|
12
10
|
config.filter_run :focus
|
13
|
-
|
14
11
|
config.order = 'random'
|
15
|
-
|
12
|
+
|
16
13
|
config.include HashValidatorSpecHelper
|
17
14
|
end
|
@@ -6,13 +6,13 @@ describe HashValidator::Validator::Base do
|
|
6
6
|
|
7
7
|
describe '#should_validate?' do
|
8
8
|
it 'should validate the name "boolean"' do
|
9
|
-
validator.should_validate?('boolean').
|
9
|
+
expect(validator.should_validate?('boolean')).to eq true
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'should not validate other names' do
|
13
|
-
validator.should_validate?('string').
|
14
|
-
validator.should_validate?('array').
|
15
|
-
validator.should_validate?(nil).
|
13
|
+
expect(validator.should_validate?('string')).to eq false
|
14
|
+
expect(validator.should_validate?('array')).to eq false
|
15
|
+
expect(validator.should_validate?(nil)).to eq false
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -20,27 +20,27 @@ describe HashValidator::Validator::Base do
|
|
20
20
|
it 'should validate a true boolean with true' do
|
21
21
|
validator.validate(:key, true, {}, errors)
|
22
22
|
|
23
|
-
errors.
|
23
|
+
expect(errors).to be_empty
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'should validate a false boolean with true' do
|
27
27
|
validator.validate(:key, false, {}, errors)
|
28
28
|
|
29
|
-
errors.
|
29
|
+
expect(errors).to be_empty
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'should validate a nil with false' do
|
33
33
|
validator.validate(:key, nil, {}, errors)
|
34
34
|
|
35
|
-
errors.
|
36
|
-
errors.
|
35
|
+
expect(errors).not_to be_empty
|
36
|
+
expect(errors).to eq({ key: 'boolean required' })
|
37
37
|
end
|
38
38
|
|
39
39
|
it 'should validate a number with false' do
|
40
40
|
validator.validate(:key, 123, {}, errors)
|
41
41
|
|
42
|
-
errors.
|
43
|
-
errors.
|
42
|
+
expect(errors).not_to be_empty
|
43
|
+
expect(errors).to eq({ key: 'boolean required' })
|
44
44
|
end
|
45
45
|
end
|
46
46
|
end
|
@@ -6,13 +6,13 @@ describe HashValidator::Validator::Base do
|
|
6
6
|
|
7
7
|
describe '#should_validate?' do
|
8
8
|
it 'should validate the name "email"' do
|
9
|
-
validator.should_validate?('email').
|
9
|
+
expect(validator.should_validate?('email')).to eq true
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'should not validate other names' do
|
13
|
-
validator.should_validate?('string').
|
14
|
-
validator.should_validate?('array').
|
15
|
-
validator.should_validate?(nil).
|
13
|
+
expect(validator.should_validate?('string')).to eq false
|
14
|
+
expect(validator.should_validate?('array')).to eq false
|
15
|
+
expect(validator.should_validate?(nil)).to eq false
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -20,21 +20,21 @@ describe HashValidator::Validator::Base do
|
|
20
20
|
it 'should validate an email with true' do
|
21
21
|
validator.validate(:key, "johndoe@gmail.com", {}, errors)
|
22
22
|
|
23
|
-
errors.
|
23
|
+
expect(errors).to be_empty
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'should validate a string without an @ symbol with false' do
|
27
27
|
validator.validate(:key, 'test', {}, errors)
|
28
28
|
|
29
|
-
errors.
|
30
|
-
errors.
|
29
|
+
expect(errors).not_to be_empty
|
30
|
+
expect(errors).to eq({ key: 'is not a valid email' })
|
31
31
|
end
|
32
32
|
|
33
33
|
it 'should validate a number with false' do
|
34
34
|
validator.validate(:key, 123, {}, errors)
|
35
35
|
|
36
|
-
errors.
|
37
|
-
errors.
|
36
|
+
expect(errors).not_to be_empty
|
37
|
+
expect(errors).to eq({ key: 'is not a valid email' })
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
@@ -20,19 +20,19 @@ describe 'Enumerable validator' do
|
|
20
20
|
let(:validations) {{ fruit: [ 'apple', 'banana', 'carrot' ] }}
|
21
21
|
|
22
22
|
it 'should validate true if the value is present' do
|
23
|
-
validate({ fruit: 'apple' }, validations).valid
|
23
|
+
expect(validate({ fruit: 'apple' }, validations).valid?).to eq true
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'should validate false if the value is not present' do
|
27
|
-
validate({ fruit: 'pear' }, validations).valid
|
27
|
+
expect(validate({ fruit: 'pear' }, validations).valid?).to eq false
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'should validate false if the key is not present' do
|
31
|
-
validate({ something: 'apple' }, validations).valid
|
31
|
+
expect(validate({ something: 'apple' }, validations).valid?).to eq false
|
32
32
|
end
|
33
33
|
|
34
34
|
it 'should provide an appropriate error message is the value is not present' do
|
35
|
-
validate({ fruit: 'pear' }, validations).errors.
|
35
|
+
expect(validate({ fruit: 'pear' }, validations).errors).to eq({ fruit: 'value from list required' })
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
@@ -40,10 +40,10 @@ describe 'Enumerable validator' do
|
|
40
40
|
let(:validations) {{ number: 1..10 }}
|
41
41
|
|
42
42
|
it 'should validate true if the value is present' do
|
43
|
-
validate({ number: 5 }, validations).valid
|
43
|
+
expect(validate({ number: 5 }, validations).valid?).to eq true
|
44
44
|
end
|
45
45
|
it 'should validate false if the value is not present' do
|
46
|
-
validate({ number: 15 }, validations).valid
|
46
|
+
expect(validate({ number: 15 }, validations).valid?).to eq false
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
@@ -51,10 +51,10 @@ describe 'Enumerable validator' do
|
|
51
51
|
let(:validations) {{ number: 1..Float::INFINITY }}
|
52
52
|
|
53
53
|
it 'should validate true if the value is present' do
|
54
|
-
validate({ number: 5 }, validations).valid
|
54
|
+
expect(validate({ number: 5 }, validations).valid?).to eq true
|
55
55
|
end
|
56
56
|
it 'should validate false if the value is not present' do
|
57
|
-
validate({ number: -5 }, validations).valid
|
57
|
+
expect(validate({ number: -5 }, validations).valid?).to eq false
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
@@ -62,7 +62,7 @@ describe 'Enumerable validator' do
|
|
62
62
|
let(:validations) {{ fruit: [ nil, :apple, :banana ] }}
|
63
63
|
|
64
64
|
it 'should validate true if a nil value is present' do
|
65
|
-
validate({ fruit: nil }, validations).valid
|
65
|
+
expect(validate({ fruit: nil }, validations).valid?).to eq true
|
66
66
|
end
|
67
67
|
end
|
68
68
|
end
|
@@ -29,11 +29,11 @@ describe 'Functional validator' do
|
|
29
29
|
let(:validations) {{ number: lambda { |n| n.odd? } }}
|
30
30
|
|
31
31
|
it 'should validate true when the number is odd' do
|
32
|
-
validate({ number: 1 }, validations).valid
|
32
|
+
expect(validate({ number: 1 }, validations).valid?).to eq true
|
33
33
|
end
|
34
34
|
|
35
35
|
it 'should validate false when the number is even' do
|
36
|
-
validate({ number: 2 }, validations).valid
|
36
|
+
expect(validate({ number: 2 }, validations).valid?).to eq false
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
@@ -41,7 +41,7 @@ describe 'Functional validator' do
|
|
41
41
|
let(:validations) {{ number: lambda { |n| n.odd? } }}
|
42
42
|
|
43
43
|
it 'should validate false when an exception occurs within the lambda' do
|
44
|
-
validate({ number: '2' }, validations).valid
|
44
|
+
expect(validate({ number: '2' }, validations).valid?).to eq false
|
45
45
|
end
|
46
46
|
end
|
47
47
|
end
|
@@ -10,13 +10,13 @@ describe HashValidator::Validator::Base do
|
|
10
10
|
|
11
11
|
describe '#should_validate?' do
|
12
12
|
it 'should validate an Many validation' do
|
13
|
-
validator.should_validate?(many('string')).
|
13
|
+
expect(validator.should_validate?(many('string'))).to eq true
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'should not validate other things' do
|
17
|
-
validator.should_validate?('string').
|
18
|
-
validator.should_validate?('array').
|
19
|
-
validator.should_validate?(nil).
|
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
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -24,37 +24,37 @@ describe HashValidator::Validator::Base do
|
|
24
24
|
it 'should accept an empty array' do
|
25
25
|
validator.validate(:key, [], many('string'), errors)
|
26
26
|
|
27
|
-
errors.
|
27
|
+
expect(errors).to be_empty
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'should accept an array of matching elements' do
|
31
31
|
validator.validate(:key, ['a', 'b'], many('string'), errors)
|
32
32
|
|
33
|
-
errors.
|
33
|
+
expect(errors).to be_empty
|
34
34
|
end
|
35
35
|
|
36
36
|
it 'should not accept an array including a non-matching element' do
|
37
37
|
validator.validate(:key, ['a', 2], many('string'), errors)
|
38
38
|
|
39
|
-
errors.
|
39
|
+
expect(errors).to eq({ key: [nil, 'string required'] })
|
40
40
|
end
|
41
41
|
|
42
42
|
it 'should accept an array of matching hashes' do
|
43
43
|
validator.validate(:key, [{v: 'a'}, {v: 'b'}], many({v: 'string'}), errors)
|
44
44
|
|
45
|
-
errors.
|
45
|
+
expect(errors).to be_empty
|
46
46
|
end
|
47
47
|
|
48
48
|
it 'should not accept an array including a non-matching element' do
|
49
49
|
validator.validate(:key, [{v: 'a'}, {v: 2}], many({v: 'string'}), errors)
|
50
50
|
|
51
|
-
errors.
|
51
|
+
expect(errors).to eq({ key: [nil, {v: 'string required'}] })
|
52
52
|
end
|
53
53
|
|
54
54
|
it 'should not accept a non-enumerable' do
|
55
55
|
validator.validate(:key, 'a', many({v: 'string'}), errors)
|
56
56
|
|
57
|
-
errors.
|
57
|
+
expect(errors).to eq({ key: 'enumerable required' })
|
58
58
|
end
|
59
59
|
end
|
60
60
|
end
|
@@ -10,13 +10,13 @@ describe HashValidator::Validator::Base do
|
|
10
10
|
|
11
11
|
describe '#should_validate?' do
|
12
12
|
it 'should validate an Optional validation' do
|
13
|
-
validator.should_validate?(optional('string')).
|
13
|
+
expect(validator.should_validate?(optional('string'))).to eq true
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'should not validate other things' do
|
17
|
-
validator.should_validate?('string').
|
18
|
-
validator.should_validate?('array').
|
19
|
-
validator.should_validate?(nil).
|
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
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -24,33 +24,33 @@ describe HashValidator::Validator::Base do
|
|
24
24
|
it 'should accept a missing value' do
|
25
25
|
validator.validate(:key, nil, optional('string'), errors)
|
26
26
|
|
27
|
-
errors.
|
27
|
+
expect(errors).to be_empty
|
28
28
|
end
|
29
29
|
|
30
30
|
it 'should accept a present, matching value' do
|
31
31
|
validator.validate(:key, 'foo', optional('string'), errors)
|
32
32
|
|
33
|
-
errors.
|
33
|
+
expect(errors).to be_empty
|
34
34
|
end
|
35
35
|
|
36
36
|
it 'should reject a present, non-matching value' do
|
37
37
|
validator.validate(:key, 123, optional('string'), errors)
|
38
38
|
|
39
|
-
errors.
|
40
|
-
errors.
|
39
|
+
expect(errors).not_to be_empty
|
40
|
+
expect(errors).to eq({ key: 'string required' })
|
41
41
|
end
|
42
42
|
|
43
43
|
it 'should accept a present, matching hash' do
|
44
44
|
validator.validate(:key, {v: 'foo'}, optional({v: 'string'}), errors)
|
45
45
|
|
46
|
-
errors.
|
46
|
+
expect(errors).to be_empty
|
47
47
|
end
|
48
48
|
|
49
49
|
it 'should reject a present, non-matching hash' do
|
50
50
|
validator.validate(:key, {}, optional({v: 'string'}), errors)
|
51
51
|
|
52
|
-
errors.
|
53
|
-
errors.
|
52
|
+
expect(errors).not_to be_empty
|
53
|
+
expect(errors).to eq({ key: {v: 'string required'} })
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
@@ -6,13 +6,13 @@ describe HashValidator::Validator::Base do
|
|
6
6
|
|
7
7
|
describe '#should_validate?' do
|
8
8
|
it 'should validate the name "required"' do
|
9
|
-
validator.should_validate?('required').
|
9
|
+
expect(validator.should_validate?('required')).to eq true
|
10
10
|
end
|
11
11
|
|
12
12
|
it 'should not validate other names' do
|
13
|
-
validator.should_validate?('string').
|
14
|
-
validator.should_validate?('array').
|
15
|
-
validator.should_validate?(nil).
|
13
|
+
expect(validator.should_validate?('string')).to eq false
|
14
|
+
expect(validator.should_validate?('array')).to eq false
|
15
|
+
expect(validator.should_validate?(nil)).to eq false
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -20,26 +20,26 @@ describe HashValidator::Validator::Base do
|
|
20
20
|
it 'should validate a string with true' do
|
21
21
|
validator.validate(:key, 'test', {}, errors)
|
22
22
|
|
23
|
-
errors.
|
23
|
+
expect(errors).to be_empty
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'should validate a number with true' do
|
27
27
|
validator.validate(:key, 123, {}, errors)
|
28
28
|
|
29
|
-
errors.
|
29
|
+
expect(errors).to be_empty
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'should validate a time with true' do
|
33
33
|
validator.validate(:key, Time.now, {}, errors)
|
34
34
|
|
35
|
-
errors.
|
35
|
+
expect(errors).to be_empty
|
36
36
|
end
|
37
37
|
|
38
38
|
it 'should validate nil with false' do
|
39
39
|
validator.validate(:key, nil, {}, errors)
|
40
40
|
|
41
|
-
errors.
|
42
|
-
errors.
|
41
|
+
expect(errors).not_to be_empty
|
42
|
+
expect(errors).to eq({ key: 'is required' })
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
@@ -31,7 +31,7 @@ describe HashValidator::Validator::SimpleValidator do
|
|
31
31
|
it "validates the string '#{value}'" do
|
32
32
|
short_string_validator.validate(:key, value, {}, errors)
|
33
33
|
|
34
|
-
errors.
|
34
|
+
expect(errors).to be_empty
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -39,7 +39,7 @@ describe HashValidator::Validator::SimpleValidator do
|
|
39
39
|
it "does not validate bad value '#{value}'" do
|
40
40
|
short_string_validator.validate(:key, value, {}, errors)
|
41
41
|
|
42
|
-
errors.
|
42
|
+
expect(errors).to eq({ key: 'short_string required' })
|
43
43
|
end
|
44
44
|
end
|
45
45
|
end
|
@@ -59,8 +59,8 @@ describe 'Simple validator types' do
|
|
59
59
|
it "validates '#{value}' successful" do
|
60
60
|
validator = HashValidator.validate({ v: value }, { v: type.to_s })
|
61
61
|
|
62
|
-
validator.valid
|
63
|
-
validator.errors.
|
62
|
+
expect(validator.valid?).to eq true
|
63
|
+
expect(validator.errors).to be_empty
|
64
64
|
end
|
65
65
|
end
|
66
66
|
|
@@ -68,8 +68,8 @@ describe 'Simple validator types' do
|
|
68
68
|
it "validates '#{value}' with failure" do
|
69
69
|
validator = HashValidator.validate({ v: value }, { v: type.to_s })
|
70
70
|
|
71
|
-
validator.valid
|
72
|
-
validator.errors.
|
71
|
+
expect(validator.valid?).to eq false
|
72
|
+
expect(validator.errors).to eq({ v: "#{type} required" })
|
73
73
|
end
|
74
74
|
end
|
75
75
|
end
|
@@ -25,13 +25,13 @@ describe 'User-defined validator' do
|
|
25
25
|
|
26
26
|
describe '#should_validate?' do
|
27
27
|
it 'validates the name "odd"' do
|
28
|
-
validator.should_validate?('odd').
|
28
|
+
expect(validator.should_validate?('odd')).to eq true
|
29
29
|
end
|
30
30
|
|
31
31
|
it 'does not validate other names' do
|
32
|
-
validator.should_validate?('string').
|
33
|
-
validator.should_validate?('array').
|
34
|
-
validator.should_validate?(nil).
|
32
|
+
expect(validator.should_validate?('string')).to eq false
|
33
|
+
expect(validator.should_validate?('array')).to eq false
|
34
|
+
expect(validator.should_validate?(nil)).to eq false
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
@@ -39,35 +39,35 @@ describe 'User-defined validator' do
|
|
39
39
|
it 'validates odd integers with true' do
|
40
40
|
validator.validate(:key, 1, {}, errors)
|
41
41
|
|
42
|
-
errors.
|
42
|
+
expect(errors).to be_empty
|
43
43
|
end
|
44
44
|
|
45
45
|
it 'validates even integers with errrors' do
|
46
46
|
validator.validate(:key, 2, {}, errors)
|
47
47
|
|
48
|
-
errors.
|
49
|
-
errors.
|
48
|
+
expect(errors).not_to be_empty
|
49
|
+
expect(errors).to eq({ key: 'odd required' })
|
50
50
|
end
|
51
51
|
|
52
52
|
it 'validates even floats with errrors' do
|
53
53
|
validator.validate(:key, 2.0, {}, errors)
|
54
54
|
|
55
|
-
errors.
|
56
|
-
errors.
|
55
|
+
expect(errors).not_to be_empty
|
56
|
+
expect(errors).to eq({ key: 'odd required' })
|
57
57
|
end
|
58
58
|
|
59
59
|
it 'validates odd floats with errrors' do
|
60
60
|
validator.validate(:key, 1.0, {}, errors)
|
61
61
|
|
62
|
-
errors.
|
63
|
-
errors.
|
62
|
+
expect(errors).not_to be_empty
|
63
|
+
expect(errors).to eq({ key: 'odd required' })
|
64
64
|
end
|
65
65
|
|
66
66
|
it 'validates an odd integer string with errrors' do
|
67
67
|
validator.validate(:key, '1', {}, errors)
|
68
68
|
|
69
|
-
errors.
|
70
|
-
errors.
|
69
|
+
expect(errors).not_to be_empty
|
70
|
+
expect(errors).to eq({ key: 'odd required' })
|
71
71
|
end
|
72
72
|
end
|
73
73
|
|
@@ -75,21 +75,21 @@ describe 'User-defined validator' do
|
|
75
75
|
before { HashValidator.append_validator(validator) rescue nil } # rescue to prevent: validators need to have unique names
|
76
76
|
|
77
77
|
it 'can be looked up using #validator_for' do
|
78
|
-
HashValidator.validator_for('odd').
|
78
|
+
expect(HashValidator.validator_for('odd')).to be_a_kind_of(HashValidator::Validator::OddValidator)
|
79
79
|
end
|
80
80
|
|
81
81
|
it 'can be used in validations - test 1' do
|
82
82
|
validator = HashValidator.validate({ age: 27 }, { age: 'odd' })
|
83
83
|
|
84
|
-
validator.valid
|
85
|
-
validator.errors.
|
84
|
+
expect(validator.valid?).to eq true
|
85
|
+
expect(validator.errors).to be_empty
|
86
86
|
end
|
87
87
|
|
88
88
|
it 'can be used in validations - test 2' do
|
89
89
|
validator = HashValidator.validate({ age: 40 }, { age: 'odd' })
|
90
90
|
|
91
|
-
validator.valid
|
92
|
-
validator.errors.
|
91
|
+
expect(validator.valid?).to eq false
|
92
|
+
expect(validator.errors).to eq({ age: 'odd required' })
|
93
93
|
end
|
94
94
|
end
|
95
95
|
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.4.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:
|
11
|
+
date: 2015-04-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -133,7 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
133
133
|
version: '0'
|
134
134
|
requirements: []
|
135
135
|
rubyforge_project:
|
136
|
-
rubygems_version: 2.
|
136
|
+
rubygems_version: 2.4.6
|
137
137
|
signing_key:
|
138
138
|
specification_version: 4
|
139
139
|
summary: Ruby library to validate hashes (Hash) against user-defined requirements
|