veto 0.1.3 → 1.0.0
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 +4 -4
- data/README.md +7 -94
- data/lib/veto/blocks/block.rb +25 -0
- data/lib/veto/blocks/checker.rb +28 -0
- data/lib/veto/blocks/conditional_block.rb +26 -0
- data/lib/veto/blocks/validate_block.rb +18 -0
- data/lib/veto/blocks/validates_block.rb +51 -0
- data/lib/veto/blocks/with_options_block.rb +7 -0
- data/lib/veto/check_context_object.rb +10 -0
- data/lib/veto/checks/attribute_check.rb +19 -0
- data/lib/veto/checks/check.rb +7 -0
- data/lib/veto/checks/check_factory.rb +18 -0
- data/lib/veto/{validators/exact_length_validator.rb → checks/exact_length_check.rb} +5 -4
- data/lib/veto/{validators/format_validator.rb → checks/format_check.rb} +5 -4
- data/lib/veto/{validators/greater_than_validator.rb → checks/greater_than_check.rb} +2 -4
- data/lib/veto/{validators/greater_than_or_equal_to_validator.rb → checks/greater_than_or_equal_to_check.rb} +2 -4
- data/lib/veto/{validators/inclusion_validator.rb → checks/inclusion_check.rb} +2 -4
- data/lib/veto/{validators/integer_validator.rb → checks/integer_check.rb} +2 -4
- data/lib/veto/{validators/length_range_validator.rb → checks/length_range_check.rb} +2 -4
- data/lib/veto/{validators/less_than_validator.rb → checks/less_than_check.rb} +2 -4
- data/lib/veto/{validators/less_than_or_equal_to_validator.rb → checks/less_than_or_equal_to_check.rb} +2 -4
- data/lib/veto/{validators/max_length_validator.rb → checks/max_length_check.rb} +2 -4
- data/lib/veto/checks/method_check.rb +17 -0
- data/lib/veto/{validators/min_length_validator.rb → checks/min_length_check.rb} +2 -4
- data/lib/veto/{validators/not_null_validator.rb → checks/not_null_check.rb} +2 -4
- data/lib/veto/{validators/numeric_validator.rb → checks/numeric_check.rb} +2 -4
- data/lib/veto/{validators/presence_validator.rb → checks/presence_check.rb} +2 -4
- data/lib/veto/conditions/condition.rb +7 -0
- data/lib/veto/conditions/condition_factory.rb +18 -0
- data/lib/veto/conditions/conditions.rb +21 -0
- data/lib/veto/conditions/context_method_condition.rb +11 -0
- data/lib/veto/conditions/entity_eval_condition.rb +11 -0
- data/lib/veto/conditions/if_conditions.rb +8 -0
- data/lib/veto/conditions/if_unless_conditions.rb +21 -0
- data/lib/veto/conditions/passing_condition.rb +7 -0
- data/lib/veto/conditions/primative_condition.rb +11 -0
- data/lib/veto/conditions/proc_condition.rb +11 -0
- data/lib/veto/conditions/unless_conditions.rb +8 -0
- data/lib/veto/configuration.rb +38 -37
- data/lib/veto/errors.rb +27 -27
- data/lib/veto/exceptions.rb +13 -3
- data/lib/veto/model.rb +33 -35
- data/lib/veto/validator.rb +68 -169
- data/lib/veto/version.rb +1 -1
- data/lib/veto.rb +90 -49
- data/spec/blocks/block_spec.rb +31 -0
- data/spec/blocks/check_block_spec.rb +46 -0
- data/spec/blocks/conditional_block_spec.rb +36 -0
- data/spec/blocks/validate_block_spec.rb +18 -0
- data/spec/blocks/validates_block_spec.rb +22 -0
- data/spec/checks/exact_length_check_spec.rb +37 -0
- data/spec/checks/format_validator_spec.rb +37 -0
- data/spec/checks/greater_than_check_spec.rb +48 -0
- data/spec/checks/greater_than_or_equal_to_check_spec.rb +48 -0
- data/spec/checks/inclusion_check_spec.rb +37 -0
- data/spec/checks/integer_check_spec.rb +37 -0
- data/spec/checks/length_range_check_spec.rb +37 -0
- data/spec/checks/less_than_check_spec.rb +48 -0
- data/spec/checks/less_than_or_equal_to_check_spec.rb +48 -0
- data/spec/checks/max_length_check_spec.rb +48 -0
- data/spec/checks/method_check_spec.rb +13 -0
- data/spec/checks/min_length_check_spec.rb +48 -0
- data/spec/checks/not_null_check_spec.rb +37 -0
- data/spec/checks/numeric_check_spec.rb +37 -0
- data/spec/checks/presence_check_spec.rb +48 -0
- data/spec/conditions/condition_factory_spec.rb +31 -0
- data/spec/conditions/conditions_spec.rb +25 -0
- data/spec/conditions/context_method_condition_spec.rb +25 -0
- data/spec/conditions/entity_eval_condition_spec.rb +21 -0
- data/spec/conditions/if_conditions_spec.rb +65 -0
- data/spec/conditions/if_unless_conditions_spec.rb +33 -0
- data/spec/conditions/primative_condition_spec.rb +22 -0
- data/spec/conditions/proc_condition_spec.rb +25 -0
- data/spec/conditions/unless_conditions_spec.rb +65 -0
- data/spec/configuration/message_spec.rb +18 -23
- data/spec/spec_helper.rb +2 -1
- data/spec/validator_spec.rb +224 -115
- metadata +96 -61
- data/lib/veto/attribute_validator_factory.rb +0 -36
- data/lib/veto/builder.rb +0 -65
- data/lib/veto/conditions.rb +0 -23
- data/lib/veto/conditions_evaluator.rb +0 -30
- data/lib/veto/validators/abstract_validator.rb +0 -15
- data/lib/veto/validators/attribute_validator.rb +0 -22
- data/lib/veto/validators/custom_method_validator.rb +0 -19
- data/spec/attribute_validator_factory_spec.rb +0 -72
- data/spec/builder_spec.rb +0 -38
- data/spec/conditions_evaluator_spec.rb +0 -90
- data/spec/conditions_spec.rb +0 -16
- data/spec/configuration_spec.rb +0 -6
- data/spec/errors_spec.rb +0 -22
- data/spec/model_spec.rb +0 -67
- data/spec/system/validator_spec.rb +0 -530
- data/spec/validators/exact_length_validator_spec.rb +0 -37
- data/spec/validators/format_validator_spec.rb +0 -32
- data/spec/validators/inclusion_validator_spec.rb +0 -45
- data/spec/validators/integer_validator_spec.rb +0 -42
- data/spec/validators/length_range_validator_spec.rb +0 -55
- data/spec/validators/max_length_validator_spec.rb +0 -32
- data/spec/validators/min_length_validator_spec.rb +0 -32
- data/spec/validators/not_null_validator_spec.rb +0 -27
- data/spec/validators/numeric_validator_spec.rb +0 -42
- data/spec/validators/presence_validator_spec.rb +0 -47
- data/spec/veto_spec.rb +0 -24
@@ -1,530 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'veto'
|
3
|
-
|
4
|
-
describe Veto do
|
5
|
-
let(:entity){ stub }
|
6
|
-
let(:validator_class) { Class.new{ include Veto.validator }}
|
7
|
-
let(:validator) { validator_class.new(entity) }
|
8
|
-
|
9
|
-
describe 'built-in validations' do
|
10
|
-
let(:value) { 'abc123' }
|
11
|
-
let(:entity) {stub(:name => value)}
|
12
|
-
let(:errors) { validator.valid?; validator.errors.on(:name) }
|
13
|
-
let(:validator_type){ :presence }
|
14
|
-
let(:options) { true }
|
15
|
-
let(:validator_class) do
|
16
|
-
klass = Class.new{ include Veto.validator }
|
17
|
-
klass.validates :name, options
|
18
|
-
klass
|
19
|
-
end
|
20
|
-
|
21
|
-
describe 'exact_length' do
|
22
|
-
let(:options) {{:exact_length => 10}}
|
23
|
-
|
24
|
-
context 'when value exact length' do
|
25
|
-
let(:value) { 'abcdefghij' }
|
26
|
-
it { errors.must_be_nil }
|
27
|
-
end
|
28
|
-
|
29
|
-
context 'when value is too short' do
|
30
|
-
let(:value) { 'short' }
|
31
|
-
it { errors.must_equal ["is not 10 characters"] }
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'when value is too long' do
|
35
|
-
let(:value) { 'this title is wayyyy to long' }
|
36
|
-
it { errors.must_equal ["is not 10 characters"] }
|
37
|
-
end
|
38
|
-
|
39
|
-
context 'when value is nil' do
|
40
|
-
let(:value) { nil }
|
41
|
-
it { errors.must_equal ["is not 10 characters"] }
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
describe 'format' do
|
46
|
-
let(:options) {{:format => /^\d+$/}}
|
47
|
-
|
48
|
-
context 'when value matches pattern' do
|
49
|
-
let(:value) { 123 }
|
50
|
-
it { errors.must_be_nil }
|
51
|
-
end
|
52
|
-
|
53
|
-
context 'when value does not match' do
|
54
|
-
let(:value) { 'abc' }
|
55
|
-
it { errors.must_equal ["is not valid"] }
|
56
|
-
end
|
57
|
-
|
58
|
-
context 'when value is nil' do
|
59
|
-
let(:value) { nil }
|
60
|
-
it { errors.must_equal ["is not valid"] }
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
describe 'greater_than' do
|
65
|
-
let(:options) {{:greater_than => 10}}
|
66
|
-
|
67
|
-
context 'when value is greater than option' do
|
68
|
-
let(:value) { 11 }
|
69
|
-
it { errors.must_be_nil }
|
70
|
-
end
|
71
|
-
|
72
|
-
context 'when float is greater than option' do
|
73
|
-
let(:value) { 11.123 }
|
74
|
-
it { errors.must_be_nil }
|
75
|
-
end
|
76
|
-
|
77
|
-
context 'when value is equal to option' do
|
78
|
-
let(:value) { 10 }
|
79
|
-
it { errors.must_equal ["must be greater than 10"] }
|
80
|
-
end
|
81
|
-
|
82
|
-
context 'when value is less than option' do
|
83
|
-
let(:value) { 9 }
|
84
|
-
it { errors.must_equal ["must be greater than 10"] }
|
85
|
-
end
|
86
|
-
|
87
|
-
context 'when value is string' do
|
88
|
-
let(:value) { 'abc' }
|
89
|
-
it { errors.must_equal ["must be greater than 10"] }
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
describe 'greater_than_or_equal_to' do
|
94
|
-
let(:options) {{:greater_than_or_equal_to => 10}}
|
95
|
-
|
96
|
-
context 'when value is greater than option' do
|
97
|
-
let(:value) { 11 }
|
98
|
-
it { errors.must_be_nil }
|
99
|
-
end
|
100
|
-
|
101
|
-
context 'when float value is greater than option' do
|
102
|
-
let(:value) { 11.123 }
|
103
|
-
it { errors.must_be_nil }
|
104
|
-
end
|
105
|
-
|
106
|
-
context 'when value is equal to option' do
|
107
|
-
let(:value) { 10 }
|
108
|
-
it { errors.must_be_nil }
|
109
|
-
end
|
110
|
-
|
111
|
-
context 'when value is less than option' do
|
112
|
-
let(:value) { 9 }
|
113
|
-
it { errors.must_equal ["must be greater than or equal to 10"] }
|
114
|
-
end
|
115
|
-
|
116
|
-
context 'when value is a string' do
|
117
|
-
let(:value) { 'abc' }
|
118
|
-
it { errors.must_equal ["must be greater than or equal to 10"] }
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
describe 'less_than' do
|
123
|
-
let(:options) {{:less_than => 10}}
|
124
|
-
|
125
|
-
context 'when value is less than option' do
|
126
|
-
let(:value) { 9 }
|
127
|
-
it { errors.must_be_nil }
|
128
|
-
end
|
129
|
-
|
130
|
-
context 'when float value is less than option' do
|
131
|
-
let(:value) { 8.123 }
|
132
|
-
it { errors.must_be_nil }
|
133
|
-
end
|
134
|
-
|
135
|
-
context 'when value is equal to option' do
|
136
|
-
let(:value) { 10 }
|
137
|
-
it { errors.must_equal ["must be less than 10"] }
|
138
|
-
end
|
139
|
-
|
140
|
-
context 'when value is greater than option' do
|
141
|
-
let(:value) { 11 }
|
142
|
-
it { errors.must_equal ["must be less than 10"] }
|
143
|
-
end
|
144
|
-
|
145
|
-
context 'when value is a string' do
|
146
|
-
let(:value) { 'abc' }
|
147
|
-
it { errors.must_equal ["must be less than 10"] }
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
describe 'less_than_or_equal_to' do
|
152
|
-
let(:options) {{:less_than_or_equal_to => 10}}
|
153
|
-
|
154
|
-
context 'when value is less than option' do
|
155
|
-
let(:value) { 9 }
|
156
|
-
it { errors.must_be_nil }
|
157
|
-
end
|
158
|
-
|
159
|
-
context 'when float value is less than option' do
|
160
|
-
let(:value) { 8.123 }
|
161
|
-
it { errors.must_be_nil }
|
162
|
-
end
|
163
|
-
|
164
|
-
context 'when value is equal to option' do
|
165
|
-
let(:value) { 10 }
|
166
|
-
it { errors.must_be_nil }
|
167
|
-
end
|
168
|
-
|
169
|
-
context 'when value is greater than option' do
|
170
|
-
let(:value) { 11 }
|
171
|
-
it { errors.must_equal ["must be less than or equal to 10"] }
|
172
|
-
end
|
173
|
-
|
174
|
-
context 'when value is a string' do
|
175
|
-
let(:value) { 'abc' }
|
176
|
-
it { errors.must_equal ["must be less than or equal to 10"] }
|
177
|
-
end
|
178
|
-
end
|
179
|
-
|
180
|
-
describe 'inclusion' do
|
181
|
-
context 'when set is array' do
|
182
|
-
let(:options) {{:inclusion => %w(cat dog bird rabbit)}}
|
183
|
-
|
184
|
-
context 'when value is in set' do
|
185
|
-
let(:value) { 'cat' }
|
186
|
-
it { errors.must_be_nil }
|
187
|
-
end
|
188
|
-
|
189
|
-
context 'when value is not in set' do
|
190
|
-
let(:value) { 'goat' }
|
191
|
-
it { errors.must_equal ["is not in set: [\"cat\", \"dog\", \"bird\", \"rabbit\"]"]}
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
context 'when set is range' do
|
196
|
-
let(:options) {{:inclusion => 10..20}}
|
197
|
-
|
198
|
-
context 'when value is in set' do
|
199
|
-
let(:value) { 11 }
|
200
|
-
it { errors.must_be_nil }
|
201
|
-
end
|
202
|
-
|
203
|
-
context 'when value is not in set' do
|
204
|
-
let(:value) { 5 }
|
205
|
-
it { errors.must_equal ["is not in set: 10..20"] }
|
206
|
-
end
|
207
|
-
end
|
208
|
-
end
|
209
|
-
|
210
|
-
describe 'integer' do
|
211
|
-
let(:options) {{:integer => true}}
|
212
|
-
|
213
|
-
context 'when value is integer' do
|
214
|
-
let(:value) { 123 }
|
215
|
-
it { errors.must_be_nil }
|
216
|
-
end
|
217
|
-
|
218
|
-
context 'when value is float' do
|
219
|
-
let(:value) { 123.4 }
|
220
|
-
it { errors.must_equal ["is not a number"]}
|
221
|
-
end
|
222
|
-
|
223
|
-
context 'when value is string' do
|
224
|
-
let(:value) { 'abc' }
|
225
|
-
it { errors.must_equal ["is not a number"]}
|
226
|
-
end
|
227
|
-
|
228
|
-
context 'when value is nil' do
|
229
|
-
let(:value) { nil }
|
230
|
-
it { errors.must_equal ["is not a number"]}
|
231
|
-
end
|
232
|
-
|
233
|
-
context 'when value is everything else' do
|
234
|
-
let(:value) { ['array'] }
|
235
|
-
it { errors.must_equal ["is not a number"]}
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
describe 'length_range' do
|
240
|
-
context 'when range is array' do
|
241
|
-
let(:options) {{:length_range => [5, 8, 15]}}
|
242
|
-
|
243
|
-
context 'when value length is in array' do
|
244
|
-
let(:value) { 'abcde' }
|
245
|
-
it { errors.must_be_nil }
|
246
|
-
end
|
247
|
-
|
248
|
-
context 'when value length is not in array' do
|
249
|
-
let(:value) { 'abc' }
|
250
|
-
it { errors.must_equal ["is too short or too long"] }
|
251
|
-
end
|
252
|
-
|
253
|
-
context 'when value length is nil' do
|
254
|
-
let(:value) { nil }
|
255
|
-
it { errors.must_equal ["is too short or too long"] }
|
256
|
-
end
|
257
|
-
end
|
258
|
-
|
259
|
-
context 'when range is range' do
|
260
|
-
let(:options) {{:length_range => 5..10}}
|
261
|
-
|
262
|
-
context 'when value length is in range' do
|
263
|
-
let(:value) { 'abcdef' }
|
264
|
-
it { errors.must_be_nil }
|
265
|
-
end
|
266
|
-
|
267
|
-
context 'when value length is not in range' do
|
268
|
-
let(:value) { 'abc' }
|
269
|
-
it { errors.must_equal ["is too short or too long"] }
|
270
|
-
end
|
271
|
-
|
272
|
-
context 'when value length is nil' do
|
273
|
-
let(:value) { nil }
|
274
|
-
it { errors.must_equal ["is too short or too long"] }
|
275
|
-
end
|
276
|
-
end
|
277
|
-
end
|
278
|
-
|
279
|
-
describe 'max_length' do
|
280
|
-
let(:options) {{:max_length => 10}}
|
281
|
-
|
282
|
-
context 'when value length is less than max' do
|
283
|
-
let(:value) { 'abc' }
|
284
|
-
it { errors.must_be_nil }
|
285
|
-
end
|
286
|
-
|
287
|
-
context 'when value is too long' do
|
288
|
-
let(:value) { 'abcdefghijklmnop' }
|
289
|
-
it { errors.must_equal ["is longer than 10 characters"] }
|
290
|
-
end
|
291
|
-
|
292
|
-
context 'when value is nil' do
|
293
|
-
let(:value) { nil }
|
294
|
-
it { errors.must_equal ["is longer than 10 characters"] }
|
295
|
-
end
|
296
|
-
end
|
297
|
-
|
298
|
-
describe 'min_length' do
|
299
|
-
let(:options) {{:min_length => 5}}
|
300
|
-
|
301
|
-
context 'when value length is greater than min' do
|
302
|
-
let(:value) { 'abcdefg' }
|
303
|
-
it { errors.must_be_nil }
|
304
|
-
end
|
305
|
-
|
306
|
-
context 'when value is too short' do
|
307
|
-
let(:value) { 'abcd' }
|
308
|
-
it { errors.must_equal ["is shorter than 5 characters"] }
|
309
|
-
end
|
310
|
-
|
311
|
-
context 'when value is nil' do
|
312
|
-
let(:value) { nil }
|
313
|
-
it { errors.must_equal ["is shorter than 5 characters"] }
|
314
|
-
end
|
315
|
-
end
|
316
|
-
|
317
|
-
describe 'not_null' do
|
318
|
-
let(:options) {{:not_null => 5}}
|
319
|
-
|
320
|
-
context 'when value is not null' do
|
321
|
-
let(:value) { 123 }
|
322
|
-
it { errors.must_be_nil }
|
323
|
-
end
|
324
|
-
|
325
|
-
context 'when value is nil' do
|
326
|
-
let(:value) { nil }
|
327
|
-
it { errors.must_equal ["is not present"]}
|
328
|
-
end
|
329
|
-
end
|
330
|
-
|
331
|
-
describe 'numeric' do
|
332
|
-
let(:options) {{:numeric => true}}
|
333
|
-
|
334
|
-
context 'when value is integer' do
|
335
|
-
let(:value) { 123 }
|
336
|
-
it { errors.must_be_nil }
|
337
|
-
end
|
338
|
-
|
339
|
-
context 'when value is float' do
|
340
|
-
let(:value) { 123.4 }
|
341
|
-
it { errors.must_be_nil }
|
342
|
-
end
|
343
|
-
|
344
|
-
context 'when value is string' do
|
345
|
-
let(:value) { 'abc' }
|
346
|
-
it { errors.must_equal ["is not a number"]}
|
347
|
-
end
|
348
|
-
|
349
|
-
context 'when value is nil' do
|
350
|
-
let(:value) { nil }
|
351
|
-
it { errors.must_equal ["is not a number"]}
|
352
|
-
end
|
353
|
-
|
354
|
-
context 'when value is everything else' do
|
355
|
-
let(:value) { ['array'] }
|
356
|
-
it { errors.must_equal ["is not a number"]}
|
357
|
-
end
|
358
|
-
end
|
359
|
-
|
360
|
-
describe 'presence' do
|
361
|
-
let(:options) {{:presence => true}}
|
362
|
-
|
363
|
-
context 'when value is not null' do
|
364
|
-
let(:value) { 123 }
|
365
|
-
it { errors.must_be_nil }
|
366
|
-
end
|
367
|
-
|
368
|
-
context 'when value is nil' do
|
369
|
-
let(:value) { nil }
|
370
|
-
it { errors.must_equal ["is not present"]}
|
371
|
-
end
|
372
|
-
|
373
|
-
context 'when value is empty string' do
|
374
|
-
let(:value) { '' }
|
375
|
-
it { errors.must_equal ["is not present"]}
|
376
|
-
end
|
377
|
-
|
378
|
-
context 'when value is string of whitespace' do
|
379
|
-
let(:value) { ' ' }
|
380
|
-
it { errors.must_equal ["is not present"]}
|
381
|
-
end
|
382
|
-
|
383
|
-
context 'when value is empty array' do
|
384
|
-
let(:value) { [] }
|
385
|
-
it { errors.must_equal ["is not present"]}
|
386
|
-
end
|
387
|
-
|
388
|
-
context 'when value is empty hash' do
|
389
|
-
let(:value) {{}}
|
390
|
-
it { errors.must_equal ["is not present"]}
|
391
|
-
end
|
392
|
-
end
|
393
|
-
end
|
394
|
-
|
395
|
-
describe 'conditions' do
|
396
|
-
describe 'with_options' do
|
397
|
-
let(:conditions) {{:if => true}}
|
398
|
-
let(:validator_class) do
|
399
|
-
klass = Class.new{
|
400
|
-
include Veto.validator
|
401
|
-
|
402
|
-
def create_errors
|
403
|
-
errors.add(:base, "error")
|
404
|
-
end
|
405
|
-
}
|
406
|
-
klass.with_options conditions do
|
407
|
-
validate :create_errors
|
408
|
-
end
|
409
|
-
klass
|
410
|
-
end
|
411
|
-
|
412
|
-
context 'when conditions pass' do
|
413
|
-
let(:conditions) {{:if => true}}
|
414
|
-
|
415
|
-
it 'performs validations within block' do
|
416
|
-
validator.valid?.must_equal false
|
417
|
-
validator.errors.full_messages.must_equal ['base error']
|
418
|
-
end
|
419
|
-
end
|
420
|
-
|
421
|
-
context 'when conditions fail' do
|
422
|
-
let(:conditions) {{:if => false}}
|
423
|
-
|
424
|
-
it 'does not perform validations within block' do
|
425
|
-
validator.valid?
|
426
|
-
validator.errors.must_be_empty
|
427
|
-
end
|
428
|
-
end
|
429
|
-
end
|
430
|
-
|
431
|
-
describe 'validates' do
|
432
|
-
let(:entity){ stub(:title => nil, :name => 'John') }
|
433
|
-
let(:options) {{}}
|
434
|
-
let(:validator_class) do
|
435
|
-
klass = Class.new{
|
436
|
-
include Veto.validator
|
437
|
-
}
|
438
|
-
|
439
|
-
klass.validates :title, options
|
440
|
-
klass
|
441
|
-
end
|
442
|
-
|
443
|
-
context 'when outer conditions pass' do
|
444
|
-
context 'when inner conditions pass' do
|
445
|
-
let(:options) {{:not_null => {:with => true, :if => true}, :exact_length => {:with => 4, :if => true}, :if => true}}
|
446
|
-
|
447
|
-
it 'performs validation' do
|
448
|
-
validator.valid?.must_equal false
|
449
|
-
validator.errors.full_messages.must_equal ["title is not present", "title is not 4 characters"]
|
450
|
-
end
|
451
|
-
end
|
452
|
-
|
453
|
-
context 'when inner conditions fail' do
|
454
|
-
let(:options) {{:not_null => {:with => true, :if => false}, :exact_length => {:with => 4, :if => true}, :if => true}}
|
455
|
-
|
456
|
-
it 'skips individual validator' do
|
457
|
-
validator.valid?.must_equal false
|
458
|
-
validator.errors.full_messages.must_equal ["title is not 4 characters"]
|
459
|
-
end
|
460
|
-
end
|
461
|
-
end
|
462
|
-
|
463
|
-
context 'when outer conditions fail' do
|
464
|
-
let(:options) {{:not_null => {:with => true, :if => true}, :exact_length => {:with => 4, :if => true}, :if => false}}
|
465
|
-
|
466
|
-
it 'skips validates block' do
|
467
|
-
validator.valid?.must_equal true
|
468
|
-
end
|
469
|
-
end
|
470
|
-
end
|
471
|
-
end
|
472
|
-
|
473
|
-
describe 'custom attribute validator class' do
|
474
|
-
let(:entity){ stub(:email_address => 'blah') }
|
475
|
-
let(:validator_class) do
|
476
|
-
klass = Class.new{
|
477
|
-
include Veto.validator
|
478
|
-
|
479
|
-
class EmailValidator < ::Veto::AttributeValidator
|
480
|
-
def validate entity, attribute, value, errors, options={}
|
481
|
-
unless value.to_s =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
|
482
|
-
errors.add(attribute, "is not a valid email address")
|
483
|
-
end
|
484
|
-
end
|
485
|
-
end
|
486
|
-
|
487
|
-
validates :email_address, :email => true
|
488
|
-
}
|
489
|
-
end
|
490
|
-
|
491
|
-
it 'uses custom validator' do
|
492
|
-
validator.valid?.must_equal false
|
493
|
-
validator.errors.full_messages.must_equal ["email_address is not a valid email address"]
|
494
|
-
end
|
495
|
-
end
|
496
|
-
|
497
|
-
describe 'validator descendents/inheritence' do
|
498
|
-
let(:validator_class) do
|
499
|
-
Class.new{
|
500
|
-
include Veto.validator
|
501
|
-
|
502
|
-
validate :create_superclass_error
|
503
|
-
|
504
|
-
def create_superclass_error
|
505
|
-
errors.add(:base, "superclass error")
|
506
|
-
end
|
507
|
-
}
|
508
|
-
end
|
509
|
-
|
510
|
-
context 'when validator is subclassed' do
|
511
|
-
let(:validator_subclass) do
|
512
|
-
Class.new(validator_class){
|
513
|
-
|
514
|
-
validate :create_subclass_error
|
515
|
-
|
516
|
-
def create_subclass_error
|
517
|
-
errors.add(:base, "subclass error")
|
518
|
-
end
|
519
|
-
}
|
520
|
-
end
|
521
|
-
|
522
|
-
it 'inherits superclass validation rules' do
|
523
|
-
validator = validator_subclass.new(entity)
|
524
|
-
|
525
|
-
validator.valid?
|
526
|
-
validator.errors.must_equal({:base=>["superclass error", "subclass error"]})
|
527
|
-
end
|
528
|
-
end
|
529
|
-
end
|
530
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'veto/validators/exact_length_validator'
|
3
|
-
require 'veto/errors'
|
4
|
-
|
5
|
-
describe Veto::ExactLengthValidator do
|
6
|
-
let(:errors){ Veto::Errors.new }
|
7
|
-
let(:entity) { Object.new }
|
8
|
-
let(:attribute) { :title }
|
9
|
-
let(:value) { 'blahblah' }
|
10
|
-
let(:options) {{:with => 10}}
|
11
|
-
let(:validator){ Veto::ExactLengthValidator.new(stub, stub) }
|
12
|
-
let(:result){ validator.validate(entity, attribute, value, errors, options) }
|
13
|
-
|
14
|
-
describe '#validate' do
|
15
|
-
before { result }
|
16
|
-
|
17
|
-
context 'when value exact length' do
|
18
|
-
let(:value) { 'abcdefghij' }
|
19
|
-
it { errors[:title].must_be_nil }
|
20
|
-
end
|
21
|
-
|
22
|
-
context 'when value is too short' do
|
23
|
-
let(:value) { 'short' }
|
24
|
-
it { errors[:title].must_equal ["is not 10 characters"] }
|
25
|
-
end
|
26
|
-
|
27
|
-
context 'when value is too long' do
|
28
|
-
let(:value) { 'this title is wayyyy to long' }
|
29
|
-
it { errors[:title].must_equal ["is not 10 characters"] }
|
30
|
-
end
|
31
|
-
|
32
|
-
context 'when value is nil' do
|
33
|
-
let(:value) { nil }
|
34
|
-
it { errors[:title].must_equal ["is not 10 characters"] }
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'veto/validators/format_validator'
|
3
|
-
require 'veto/errors'
|
4
|
-
|
5
|
-
describe Veto::FormatValidator do
|
6
|
-
let(:errors){ Veto::Errors.new }
|
7
|
-
let(:entity) { Object.new }
|
8
|
-
let(:attribute) { :title }
|
9
|
-
let(:value) { 'blahblah' }
|
10
|
-
let(:options) {{:with => /^\d+$/}}
|
11
|
-
let(:validator){ Veto::FormatValidator.new(stub, stub) }
|
12
|
-
let(:result){ validator.validate(entity, attribute, value, errors, options) }
|
13
|
-
|
14
|
-
describe '#validate' do
|
15
|
-
before { result }
|
16
|
-
|
17
|
-
context 'when value matches pattern' do
|
18
|
-
let(:value) { 123 }
|
19
|
-
it { errors[:title].must_be_nil }
|
20
|
-
end
|
21
|
-
|
22
|
-
context 'when value does not match' do
|
23
|
-
let(:value) { 'abc' }
|
24
|
-
it { errors[:title].must_equal ["is not valid"] }
|
25
|
-
end
|
26
|
-
|
27
|
-
context 'when value is nil' do
|
28
|
-
let(:value) { nil }
|
29
|
-
it { errors[:title].must_equal ["is not valid"] }
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'veto/validators/inclusion_validator'
|
3
|
-
require 'veto/errors'
|
4
|
-
|
5
|
-
describe Veto::InclusionValidator do
|
6
|
-
let(:errors){ Veto::Errors.new }
|
7
|
-
let(:entity) { Object.new }
|
8
|
-
let(:attribute) { :title }
|
9
|
-
let(:value) { 'blahblah' }
|
10
|
-
let(:options) {{:in => %w(cat dog bird rabbit)}}
|
11
|
-
let(:validator){ Veto::InclusionValidator.new(stub, stub) }
|
12
|
-
let(:result){ validator.validate(entity, attribute, value, errors, options) }
|
13
|
-
|
14
|
-
describe '#validate' do
|
15
|
-
before { result }
|
16
|
-
|
17
|
-
context 'when set is array' do
|
18
|
-
let(:options) {{:in => %w(cat dog bird rabbit)}}
|
19
|
-
|
20
|
-
context 'when value is in set' do
|
21
|
-
let(:value) { 'cat' }
|
22
|
-
it { errors[:title].must_be_nil }
|
23
|
-
end
|
24
|
-
|
25
|
-
context 'when value is not in set' do
|
26
|
-
let(:value) { 'goat' }
|
27
|
-
it { errors[:title].must_equal ["is not in set: [\"cat\", \"dog\", \"bird\", \"rabbit\"]"]}
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
context 'when set is range' do
|
32
|
-
let(:options) {{:in => (10..20)}}
|
33
|
-
|
34
|
-
context 'when value is in set' do
|
35
|
-
let(:value) { 11 }
|
36
|
-
it { errors[:title].must_be_nil }
|
37
|
-
end
|
38
|
-
|
39
|
-
context 'when value is not in set' do
|
40
|
-
let(:value) { 5 }
|
41
|
-
it { errors[:title].must_equal ["is not in set: 10..20"] }
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
@@ -1,42 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'veto/validators/integer_validator'
|
3
|
-
require 'veto/errors'
|
4
|
-
|
5
|
-
describe Veto::IntegerValidator do
|
6
|
-
let(:errors){ Veto::Errors.new }
|
7
|
-
let(:entity) { Object.new }
|
8
|
-
let(:attribute) { :title }
|
9
|
-
let(:value) { 'blahblah' }
|
10
|
-
let(:options) {{:with => {}}}
|
11
|
-
let(:validator){ Veto::IntegerValidator.new(stub, stub) }
|
12
|
-
let(:result){ validator.validate(entity, attribute, value, errors, options) }
|
13
|
-
|
14
|
-
describe '#validate' do
|
15
|
-
before { result }
|
16
|
-
|
17
|
-
context 'when value is integer' do
|
18
|
-
let(:value) { 123 }
|
19
|
-
it { errors[:title].must_be_nil }
|
20
|
-
end
|
21
|
-
|
22
|
-
context 'when value is float' do
|
23
|
-
let(:value) { 123.4 }
|
24
|
-
it { errors[:title].must_equal ["is not a number"]}
|
25
|
-
end
|
26
|
-
|
27
|
-
context 'when value is string' do
|
28
|
-
let(:value) { 'abc' }
|
29
|
-
it { errors[:title].must_equal ["is not a number"]}
|
30
|
-
end
|
31
|
-
|
32
|
-
context 'when value is nil' do
|
33
|
-
let(:value) { nil }
|
34
|
-
it { errors[:title].must_equal ["is not a number"]}
|
35
|
-
end
|
36
|
-
|
37
|
-
context 'when value is everything else' do
|
38
|
-
let(:value) { ['array'] }
|
39
|
-
it { errors[:title].must_equal ["is not a number"]}
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|