validation_profiler 0.1.11 → 0.2.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: dc456f7b7675e026379968f8301308f31e31b289
4
- data.tar.gz: bbf98c30843ec7420d4237f0747804251a0a3d80
3
+ metadata.gz: 27176d2061e3c1cc5b60ad63f95b37f0f94b3997
4
+ data.tar.gz: c086eb080473ac0f0cb95d7e3ef7c18b2dbfd0cb
5
5
  SHA512:
6
- metadata.gz: c19ceb8e25b76e3b5ed19a6df1434349ec27ecaeaf5059729f248c2669cad85dd3d08750d85b40ae2a4b449a7697f195e66beec23e7b209af3694adff32592df
7
- data.tar.gz: c5a059a2296246f25ca0505752168429be3fcb7f5f845ddc54f5a9dbb3956da641b3549f9d19355f39a6be38cabf00f96b6df6adfafa18c90f9f15aeafa8dbdb
6
+ metadata.gz: 5da629eb13fa60d7cc371f65550781b956d0c2b6be6bfa721899c86b455ba2e1ad83eadad8071a8511788e633d9355a9f06a57c4500624c564e86a5597a13c9e
7
+ data.tar.gz: a38bcf9271b406d33a8759380a305c336decf4ef8161163ceafd0078801779f684a7f9c51b7e29873cfa4662b77429682f013fb8b4d64933b25c0d26ecaeed47
@@ -84,25 +84,30 @@ end
84
84
 
85
85
  class LengthValidationRule < ValidationRule
86
86
 
87
- def error_message(field, attributes)
87
+ def error_message(field, attributes, parent = nil)
88
+ field_name = field.to_s
89
+ if parent != nil
90
+ field_name = "#{parent.to_s}.#{field.to_s}"
91
+ end
92
+
88
93
  #check if a custom error message has been specified in the attributes
89
94
  if attributes[:message] == nil
90
95
  #no custom error message has been specified so create the default message.
91
96
  min = attributes[:min]
92
97
  max = attributes[:max]
93
98
  if min && max
94
- "#{field} must have a min length of #{min} and a max length of #{max}"
99
+ "#{field_name} must have a min length of #{min} and a max length of #{max}"
95
100
  elsif min
96
- "#{field} must have a min length of #{min}"
101
+ "#{field_name} must have a min length of #{min}"
97
102
  else
98
- "#{field} must have a max length of #{max}"
103
+ "#{field_name} must have a max length of #{max}"
99
104
  end
100
105
  else
101
106
  attributes[:message]
102
107
  end
103
108
  end
104
109
 
105
- def validate(obj, field, attributes)
110
+ def validate(obj, field, attributes, parent = nil)
106
111
 
107
112
  min = attributes[:min]
108
113
  max = attributes[:max]
@@ -142,18 +147,24 @@ end
142
147
 
143
148
  class MinValidationRule < ValidationRule
144
149
 
145
- def error_message(field, attributes)
150
+ def error_message(field, attributes, parent = nil)
151
+
152
+ field_name = field.to_s
153
+ if parent != nil
154
+ field_name = "#{parent.to_s}.#{field.to_s}"
155
+ end
156
+
146
157
  #check if a custom error message has been specified in the attributes
147
158
  if attributes[:message] == nil
148
159
  #no custom error message has been specified so create the default message.
149
160
  min = attributes[:value]
150
- "#{field} must have a minimum value of #{min}"
161
+ "#{field_name} must have a minimum value of #{min}"
151
162
  else
152
163
  attributes[:message]
153
164
  end
154
165
  end
155
166
 
156
- def validate(obj, field, attributes)
167
+ def validate(obj, field, attributes, parent = nil)
157
168
 
158
169
  min = attributes[:value]
159
170
 
@@ -169,6 +180,10 @@ class MinValidationRule < ValidationRule
169
180
  return true
170
181
  end
171
182
 
183
+ if field_value == nil
184
+ return false
185
+ end
186
+
172
187
  if field_value.is_a?(DateTime) || field_value.is_a?(Numeric)
173
188
  field_value >= min
174
189
  else
@@ -181,18 +196,24 @@ end
181
196
 
182
197
  class MaxValidationRule < ValidationRule
183
198
 
184
- def error_message(field, attributes)
199
+ def error_message(field, attributes, parent = nil)
200
+
201
+ field_name = field.to_s
202
+ if parent != nil
203
+ field_name = "#{parent.to_s}.#{field.to_s}"
204
+ end
205
+
185
206
  #check if a custom error message has been specified in the attributes
186
207
  if attributes[:message] == nil
187
208
  #no custom error message has been specified so create the default message.
188
209
  max = attributes[:value]
189
- "#{field} must not have a value greater than #{max}"
210
+ "#{field_name} must not have a value greater than #{max}"
190
211
  else
191
212
  attributes[:message]
192
213
  end
193
214
  end
194
215
 
195
- def validate(obj, field, attributes)
216
+ def validate(obj, field, attributes, parent = nil)
196
217
 
197
218
  max = attributes[:value]
198
219
 
@@ -208,6 +229,10 @@ class MaxValidationRule < ValidationRule
208
229
  return true
209
230
  end
210
231
 
232
+ if field_value == nil
233
+ return false
234
+ end
235
+
211
236
  if field_value.is_a?(DateTime) || field_value.is_a?(Numeric)
212
237
  field_value <= max
213
238
  else
@@ -220,18 +245,24 @@ end
220
245
 
221
246
  class RequiredValidationRule < ValidationRule
222
247
 
223
- def error_message(field, attributes = {})
248
+ def error_message(field, attributes = {}, parent = nil)
249
+
250
+ field_name = field.to_s
251
+ if parent != nil
252
+ field_name = "#{parent.to_s}.#{field.to_s}"
253
+ end
254
+
224
255
  #check if a custom error message has been specified in the attributes
225
256
  if attributes[:message] == nil
226
257
  #no custom error message has been specified so create the default message.
227
- "#{field} is required"
258
+ "#{field_name} is required"
228
259
  else
229
260
  attributes[:message]
230
261
  end
231
262
  end
232
263
 
233
264
 
234
- def validate(obj, field, attributes = {})
265
+ def validate(obj, field, attributes = {}, parent = nil)
235
266
 
236
267
  #attempt to get the field value from the object
237
268
  field_value = get_field_value(obj, field)
@@ -254,18 +285,24 @@ class EmailValidationRule < ValidationRule
254
285
 
255
286
  REGEX = /^[^@]+@[^@]+\.[^@]+$/
256
287
 
257
- def error_message(field, attributes = {})
288
+ def error_message(field, attributes = {}, parent = nil)
289
+
290
+ field_name = field.to_s
291
+ if parent != nil
292
+ field_name = "#{parent.to_s}.#{field.to_s}"
293
+ end
294
+
258
295
  #check if a custom error message has been specified in the attributes
259
296
  if attributes[:message] == nil
260
297
  #no custom error message has been specified so create the default message.
261
- "#{field} is not a valid email address"
298
+ "#{field_name} is not a valid email address"
262
299
  else
263
300
  attributes[:message]
264
301
  end
265
302
  end
266
303
 
267
304
 
268
- def validate(obj, field, attributes = {})
305
+ def validate(obj, field, attributes = {}, parent = nil)
269
306
 
270
307
  #attempt to get the field value from the object
271
308
  field_value = get_field_value(obj, field)
@@ -287,18 +324,24 @@ end
287
324
 
288
325
  class RegexValidationRule < ValidationRule
289
326
 
290
- def error_message(field, attributes = {})
327
+ def error_message(field, attributes = {}, parent = nil)
328
+
329
+ field_name = field.to_s
330
+ if parent != nil
331
+ field_name = "#{parent.to_s}.#{field.to_s}"
332
+ end
333
+
291
334
  #check if a custom error message has been specified in the attributes
292
335
  if attributes[:message] == nil
293
336
  #no custom error message has been specified so create the default message.
294
- "#{field} is not a valid"
337
+ "#{field_name} is not a valid"
295
338
  else
296
339
  attributes[:message]
297
340
  end
298
341
  end
299
342
 
300
343
 
301
- def validate(obj, field, attributes)
344
+ def validate(obj, field, attributes, parent = nil)
302
345
 
303
346
  #attempt to get the field value from the object
304
347
  field_value = get_field_value(obj, field)
@@ -325,21 +368,26 @@ end
325
368
 
326
369
  class MatchValidationRule < ValidationRule
327
370
 
328
- def error_message(field, attributes)
371
+ def error_message(field, attributes, parent = nil)
372
+
373
+ field_name = field.to_s
374
+ if parent != nil
375
+ field_name = "#{parent.to_s}.#{field.to_s}"
376
+ end
329
377
 
330
378
  match_field = attributes[:field]
331
379
 
332
380
  #check if a custom error message has been specified in the attributes
333
381
  if attributes[:message] == nil
334
382
  #no custom error message has been specified so create the default message.
335
- "#{field} does not match #{match_field}"
383
+ "#{field_name} does not match #{match_field}"
336
384
  else
337
385
  attributes[:message]
338
386
  end
339
387
  end
340
388
 
341
389
 
342
- def validate(obj, field, attributes)
390
+ def validate(obj, field, attributes, parent = nil)
343
391
 
344
392
  #attempt to get the field value from the object
345
393
  field_value = get_field_value(obj, field)
@@ -363,21 +411,24 @@ end
363
411
 
364
412
  class ConditionValidationRule < ValidationRule
365
413
 
366
- def error_message(field, attributes = {})
414
+ def error_message(field, attributes = {}, parent = nil)
367
415
 
368
- match_field = attributes[:field]
416
+ field_name = field.to_s
417
+ if parent != nil
418
+ field_name = "#{parent.to_s}.#{field.to_s}"
419
+ end
369
420
 
370
421
  #check if a custom error message has been specified in the attributes
371
422
  if attributes[:message] == nil
372
423
  #no custom error message has been specified so create the default message.
373
- "#{field} is not valid"
424
+ "#{field_name} is not valid"
374
425
  else
375
426
  attributes[:message]
376
427
  end
377
428
  end
378
429
 
379
430
 
380
- def validate(obj, field, attributes)
431
+ def validate(obj, field, attributes, parent = nil)
381
432
 
382
433
  #attempt to get the field value from the object
383
434
  value = get_field_value(obj, field)
@@ -450,18 +501,24 @@ end
450
501
 
451
502
  class NotAllowedValidationRule < ValidationRule
452
503
 
453
- def error_message(field, attributes = {})
504
+ def error_message(field, attributes = {}, parent = nil)
505
+
506
+ field_name = field.to_s
507
+ if parent != nil
508
+ field_name = "#{parent.to_s}.#{field.to_s}"
509
+ end
510
+
454
511
  #check if a custom error message has been specified in the attributes
455
512
  if attributes[:message] == nil
456
513
  #no custom error message has been specified so create the default message.
457
- "#{field} is not allowed."
514
+ "#{field_name} is not allowed."
458
515
  else
459
516
  attributes[:message]
460
517
  end
461
518
  end
462
519
 
463
520
 
464
- def validate(obj, field, attributes = {})
521
+ def validate(obj, field, attributes = {}, parent = nil)
465
522
 
466
523
  #attempt to get the field value from the object
467
524
  field_value = get_field_value(obj, field)
@@ -478,18 +535,24 @@ end
478
535
 
479
536
  class ListValidationRule < ValidationRule
480
537
 
481
- def error_message(field, attributes = {})
538
+ def error_message(field, attributes = {}, parent = nil)
539
+
540
+ field_name = field.to_s
541
+ if parent != nil
542
+ field_name = "#{parent.to_s}.#{field.to_s}"
543
+ end
544
+
482
545
  #check if a custom error message has been specified in the attributes
483
546
  if attributes[:message] == nil
484
547
  #no custom error message has been specified so create the default message.
485
- "#{field} is not an accepted value."
548
+ "#{field_name} is not an accepted value."
486
549
  else
487
550
  attributes[:message]
488
551
  end
489
552
  end
490
553
 
491
554
 
492
- def validate(obj, field, attributes = {})
555
+ def validate(obj, field, attributes = {}, parent = nil)
493
556
 
494
557
  #attempt to get the field value from the object
495
558
  field_value = get_field_value(obj, field)
@@ -498,6 +561,10 @@ class ListValidationRule < ValidationRule
498
561
  return true
499
562
  end
500
563
 
564
+ if field_value == nil
565
+ return false
566
+ end
567
+
501
568
  list = attributes[:list]
502
569
  if list == nil || !list.is_a?(Array)
503
570
  raise InvalidRuleAttributes.new(ListValidationRule, field)
@@ -511,4 +578,55 @@ class ListValidationRule < ValidationRule
511
578
 
512
579
  end
513
580
 
581
+ end
582
+
583
+ class ChildValidationRule < ValidationRule
584
+
585
+ def initialize
586
+ @validation_manager = ValidationManager.new
587
+ end
588
+
589
+ def error_message(field, attributes = {}, parent = nil)
590
+
591
+ field_name = field.to_s
592
+ if parent != nil
593
+ field_name = "#{parent.to_s}.#{field.to_s}"
594
+ end
595
+
596
+ #check if a custom error message has been specified in the attributes
597
+ if attributes[:message] == nil
598
+ #no custom error message has been specified so create the default message.
599
+ "#{field_name} is required."
600
+ else
601
+ attributes[:message]
602
+ end
603
+ end
604
+
605
+ def validate(obj, field, attributes = {}, parent = nil)
606
+
607
+ #attempt to get the field value from the object
608
+ field_value = get_field_value(obj, field)
609
+
610
+ if !is_required?(field_value, attributes)
611
+ return true
612
+ end
613
+
614
+ profile = attributes[:profile]
615
+ if profile == nil
616
+ raise InvalidRuleAttributes.new(ChildValidationRule, field)
617
+ end
618
+
619
+ if field_value == nil
620
+ return false
621
+ end
622
+
623
+ parent_field = field.to_s
624
+ if parent != nil
625
+ parent_field = "#{parent.to_s}.#{field.to_s}"
626
+ end
627
+
628
+ return @validation_manager.validate(field_value, profile, parent_field)
629
+
630
+ end
631
+
514
632
  end
@@ -64,6 +64,7 @@ class ValidationRuleManager
64
64
  @rules.push({ key: :condition, instance: ConditionValidationRule.new })
65
65
  @rules.push({ key: :not_allowed, instance: NotAllowedValidationRule.new })
66
66
  @rules.push({ key: :list, instance: ListValidationRule.new })
67
+ @rules.push({ key: :child, instance: ChildValidationRule.new })
67
68
 
68
69
  end
69
70
 
@@ -1,3 +1,3 @@
1
1
  module ValidationProfiler
2
- VERSION = "0.1.11"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -36,7 +36,7 @@ class ValidationManager
36
36
  # @param profile [ClassName] The class name of the validation profile to validate against
37
37
  #
38
38
  # @return [ValidationManagerResult] The result of the validation
39
- def validate(obj, profile)
39
+ def validate(obj, profile, parent = nil)
40
40
 
41
41
  result = ValidationManagerResult.new
42
42
 
@@ -49,11 +49,13 @@ class ValidationManager
49
49
  end
50
50
 
51
51
  rule = ValidationRuleManager.instance.get_rule(r[:name])
52
- outcome = rule.validate(obj, r[:field], r[:attributes])
52
+ outcome = rule.validate(obj, r[:field], r[:attributes], parent)
53
53
 
54
- if !outcome
54
+ if outcome.is_a?(ValidationManagerResult)
55
+ result = outcome
56
+ elsif !outcome
55
57
  result.outcome = false
56
- result.errors.push({ field: r[:field], message: rule.error_message(r[:field], r[:attributes]) })
58
+ result.errors.push({ field: r[:field], message: rule.error_message(r[:field], r[:attributes], parent) })
57
59
  end
58
60
 
59
61
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validation_profiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - vaughanbrittonsage
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-06-14 00:00:00.000000000 Z
11
+ date: 2016-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler