validation_profiler 0.2.0 → 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.
Files changed (26) hide show
  1. checksums.yaml +4 -4
  2. data/lib/validation_profiler/exceptions/field_not_found.rb +16 -0
  3. data/lib/validation_profiler/exceptions/invalid_field_type.rb +18 -0
  4. data/lib/validation_profiler/exceptions/invalid_validation_rule_attributes.rb +18 -0
  5. data/lib/validation_profiler/exceptions/invalid_validation_rule_type.rb +16 -0
  6. data/lib/validation_profiler/exceptions/validation_rule_already_exists.rb +16 -0
  7. data/lib/validation_profiler/exceptions/validation_rule_not_found.rb +16 -0
  8. data/lib/validation_profiler/exceptions.rb +6 -0
  9. data/lib/validation_profiler/rules/child_validation_rule.rb +54 -0
  10. data/lib/validation_profiler/rules/condition_validation_rule.rb +93 -0
  11. data/lib/validation_profiler/rules/email_validation_rule.rb +44 -0
  12. data/lib/validation_profiler/rules/length_validation_rule.rb +66 -0
  13. data/lib/validation_profiler/rules/list_validation_rule.rb +50 -0
  14. data/lib/validation_profiler/rules/match_validation_rule.rb +46 -0
  15. data/lib/validation_profiler/rules/max_validation_rule.rb +52 -0
  16. data/lib/validation_profiler/rules/min_validation_rule.rb +52 -0
  17. data/lib/validation_profiler/rules/not_allowed_validation_rule.rb +37 -0
  18. data/lib/validation_profiler/rules/regex_validation_rule.rb +47 -0
  19. data/lib/validation_profiler/rules/required_validation_rule.rb +41 -0
  20. data/lib/validation_profiler/rules/validation_rule.rb +43 -0
  21. data/lib/validation_profiler/rules/validation_rule_manager.rb +71 -106
  22. data/lib/validation_profiler/rules.rb +23 -0
  23. data/lib/validation_profiler/version.rb +1 -1
  24. data/lib/validation_profiler.rb +40 -38
  25. metadata +22 -3
  26. data/lib/validation_profiler/rules/rules.rb +0 -632
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 27176d2061e3c1cc5b60ad63f95b37f0f94b3997
4
- data.tar.gz: c086eb080473ac0f0cb95d7e3ef7c18b2dbfd0cb
3
+ metadata.gz: b9871f7c011b21c0dab41e770af21eb9eaf17000
4
+ data.tar.gz: 698f44e7f2551c825fa464798ce888f6b411cf17
5
5
  SHA512:
6
- metadata.gz: 5da629eb13fa60d7cc371f65550781b956d0c2b6be6bfa721899c86b455ba2e1ad83eadad8071a8511788e633d9355a9f06a57c4500624c564e86a5597a13c9e
7
- data.tar.gz: a38bcf9271b406d33a8759380a305c336decf4ef8161163ceafd0078801779f684a7f9c51b7e29873cfa4662b77429682f013fb8b4d64933b25c0d26ecaeed47
6
+ metadata.gz: 6c2c68a188987fdaa05b07924a273621d7c7b853f16dadf20cb00cafbeb8abe515620913bd2aeb0ee997fcd7dbafa4b2dc285a70676331807d17d04b9cc4b736
7
+ data.tar.gz: d5d08fe310902cee592e3129810fdee6a3fa984806fa6752600977f080cf87f0ec05adaeb41800327c6c9dd4cb410b3df44a14d10ef6af51a0e8b87fe85afaa2
@@ -0,0 +1,16 @@
1
+ module ValidationProfiler
2
+ module Exceptions
3
+ class FieldNotFound < StandardError
4
+
5
+ def initialize(field)
6
+ @field = field
7
+ end
8
+
9
+ def to_s
10
+ field = @field
11
+ "Field: #{field} could not be found."
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ module ValidationProfiler
2
+ module Exceptions
3
+ class InvalidFieldType < StandardError
4
+
5
+ def initialize(rule, field)
6
+ @rule = rule
7
+ @field = field
8
+ end
9
+
10
+ def to_s
11
+ rule = @rule
12
+ field = @field
13
+ "Field: #{field} has an incorrect value type for Validation Rule: #{rule}."
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module ValidationProfiler
2
+ module Exceptions
3
+ class InvalidValidationRuleAttributes < StandardError
4
+
5
+ def initialize(rule, field)
6
+ @rule = rule
7
+ @field = field
8
+ end
9
+
10
+ def to_s
11
+ rule = @rule
12
+ field = @field
13
+ "Incorrect attributes specified for Validation rule: #{rule} Field: #{field}."
14
+ end
15
+
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ module ValidationProfiler
2
+ module Exceptions
3
+ class InvalidValidationRuleType < StandardError
4
+
5
+ def initialize(rule)
6
+ @rule = rule
7
+ end
8
+
9
+ def to_s
10
+ rule = @rule
11
+ "Validation rule: #{rule} is not a valid type. Rules must inherit from the ValidationRule base class."
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module ValidationProfiler
2
+ module Exceptions
3
+ class ValidationRuleAlreadyExists < StandardError
4
+
5
+ def initialize(key)
6
+ @key = key
7
+ end
8
+
9
+ def to_s
10
+ key = @key
11
+ "A Rule has already been registered for the key: #{key}."
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module ValidationProfiler
2
+ module Exceptions
3
+ class ValidationRuleNotFound < StandardError
4
+
5
+ def initialize(rule)
6
+ @rule = rule
7
+ end
8
+
9
+ def to_s
10
+ rule = @rule
11
+ "Validation rule: #{rule} could not be found."
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,6 @@
1
+ require_relative 'exceptions/field_not_found'
2
+ require_relative 'exceptions/invalid_field_type'
3
+ require_relative 'exceptions/invalid_validation_rule_type'
4
+ require_relative 'exceptions/invalid_validation_rule_attributes'
5
+ require_relative 'exceptions/validation_rule_already_exists'
6
+ require_relative 'exceptions/validation_rule_not_found'
@@ -0,0 +1,54 @@
1
+ module ValidationProfiler
2
+ module Rules
3
+ class ChildValidationRule < ValidationProfiler::Rules::ValidationRule
4
+
5
+ def initialize
6
+ @validation_manager = ValidationProfiler::Manager.new
7
+ end
8
+
9
+ def error_message(field, attributes = {}, parent = nil)
10
+
11
+ field_name = field.to_s
12
+ if parent != nil
13
+ field_name = "#{parent.to_s}.#{field.to_s}"
14
+ end
15
+
16
+ #check if a custom error message has been specified in the attributes
17
+ if attributes[:message] == nil
18
+ #no custom error message has been specified so create the default message.
19
+ "#{field_name} is required."
20
+ else
21
+ attributes[:message]
22
+ end
23
+ end
24
+
25
+ def validate(obj, field, attributes = {}, parent = nil)
26
+
27
+ #attempt to get the field value from the object
28
+ field_value = get_field_value(obj, field)
29
+
30
+ if !is_required?(field_value, attributes)
31
+ return true
32
+ end
33
+
34
+ profile = attributes[:profile]
35
+ if profile == nil
36
+ raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::ChildValidationRule, field)
37
+ end
38
+
39
+ if field_value == nil
40
+ return false
41
+ end
42
+
43
+ parent_field = field.to_s
44
+ if parent != nil
45
+ parent_field = "#{parent.to_s}.#{field.to_s}"
46
+ end
47
+
48
+ return @validation_manager.validate(field_value, profile, parent_field)
49
+
50
+ end
51
+
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,93 @@
1
+ module ValidationProfiler
2
+ module Rules
3
+ class ConditionValidationRule < ValidationProfiler::Rules::ValidationRule
4
+
5
+ def error_message(field, attributes = {}, parent = nil)
6
+
7
+ field_name = field.to_s
8
+ if parent != nil
9
+ field_name = "#{parent.to_s}.#{field.to_s}"
10
+ end
11
+
12
+ #check if a custom error message has been specified in the attributes
13
+ if attributes[:message] == nil
14
+ #no custom error message has been specified so create the default message.
15
+ "#{field_name} is not valid"
16
+ else
17
+ attributes[:message]
18
+ end
19
+ end
20
+
21
+
22
+ def validate(obj, field, attributes, parent = nil)
23
+
24
+ #attempt to get the field value from the object
25
+ value = get_field_value(obj, field)
26
+
27
+ condition_field = attributes[:condition_field]
28
+ if condition_field == nil
29
+ raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::ConditionValidationRule, field)
30
+ end
31
+
32
+ condition_value = attributes[:condition_value]
33
+ if condition_value == nil
34
+ raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::ConditionValidationRule, field)
35
+ end
36
+
37
+ condition_expression = attributes[:condition_expression]
38
+ if condition_expression == nil
39
+ raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::ConditionValidationRule, field)
40
+ end
41
+
42
+ field_expression = attributes[:field_expression]
43
+ if field_expression == nil
44
+ raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::ConditionValidationRule, field)
45
+ end
46
+
47
+ field_value = attributes[:field_value]
48
+ if field_value == nil
49
+ raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::ConditionValidationRule, field)
50
+ end
51
+
52
+ if !is_required?(value, attributes)
53
+ return true
54
+ end
55
+
56
+ condition_field_value = get_field_value(obj, condition_field)
57
+
58
+ #check if the condition is valid
59
+ if confirm_expression?(condition_field_value, condition_expression, condition_value)
60
+ #check if the field value is correct for the condition
61
+ if confirm_expression?(value, field_expression, field_value)
62
+ return true
63
+ else
64
+ return false
65
+ end
66
+ end
67
+
68
+ return true
69
+
70
+ end
71
+
72
+ def confirm_expression?(value_a, expression, value_b)
73
+ a = value_a
74
+ b = value_b
75
+
76
+ if value_a.is_a?(String)
77
+ a = "'#{value_a}'"
78
+ elsif value_a == nil
79
+ a = 'nil'
80
+ end
81
+
82
+ if value_b.is_a?(String)
83
+ b = "'#{value_b}'"
84
+ elsif value_b == nil
85
+ b = 'nil'
86
+ end
87
+
88
+ eval("#{a} #{expression} #{b}")
89
+ end
90
+
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,44 @@
1
+ module ValidationProfiler
2
+ module Rules
3
+ class EmailValidationRule < ValidationProfiler::Rules::ValidationRule
4
+
5
+ REGEX = /^[^@]+@[^@]+\.[^@]+$/
6
+
7
+ def error_message(field, attributes = {}, parent = nil)
8
+
9
+ field_name = field.to_s
10
+ if parent != nil
11
+ field_name = "#{parent.to_s}.#{field.to_s}"
12
+ end
13
+
14
+ #check if a custom error message has been specified in the attributes
15
+ if attributes[:message] == nil
16
+ #no custom error message has been specified so create the default message.
17
+ "#{field_name} is not a valid email address"
18
+ else
19
+ attributes[:message]
20
+ end
21
+ end
22
+
23
+
24
+ def validate(obj, field, attributes = {}, parent = nil)
25
+
26
+ #attempt to get the field value from the object
27
+ field_value = get_field_value(obj, field)
28
+
29
+ if !is_required?(field_value, attributes)
30
+ return true
31
+ end
32
+
33
+ #validate the value against the regex
34
+ if field_value =~ REGEX
35
+ return true
36
+ end
37
+
38
+ return false
39
+
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,66 @@
1
+ module ValidationProfiler
2
+ module Rules
3
+ class LengthValidationRule < ValidationProfiler::Rules::ValidationRule
4
+
5
+ def error_message(field, attributes, parent = nil)
6
+ field_name = field.to_s
7
+ if parent != nil
8
+ field_name = "#{parent.to_s}.#{field.to_s}"
9
+ end
10
+
11
+ #check if a custom error message has been specified in the attributes
12
+ if attributes[:message] == nil
13
+ #no custom error message has been specified so create the default message.
14
+ min = attributes[:min]
15
+ max = attributes[:max]
16
+ if min && max
17
+ "#{field_name} must have a min length of #{min} and a max length of #{max}"
18
+ elsif min
19
+ "#{field_name} must have a min length of #{min}"
20
+ else
21
+ "#{field_name} must have a max length of #{max}"
22
+ end
23
+ else
24
+ attributes[:message]
25
+ end
26
+ end
27
+
28
+ def validate(obj, field, attributes, parent = nil)
29
+
30
+ min = attributes[:min]
31
+ max = attributes[:max]
32
+
33
+ #verify the expected attributes have been specified.
34
+ if min == nil && max == nil
35
+ raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::LengthValidationRule, field)
36
+ end
37
+
38
+ #attempt to get the field value from the object
39
+ field_value = get_field_value(obj, field)
40
+
41
+ #check if this validation check should be skipped
42
+ if !is_required?(field_value, attributes)
43
+ return true
44
+ end
45
+
46
+ if field_value.is_a?(Array)
47
+ length = field_value.length
48
+ else
49
+ length = field_value.to_s.length
50
+ end
51
+
52
+ #validate the field value
53
+ if min && max
54
+ return length >= min && length <= max
55
+ elsif min
56
+ return length >= min
57
+ elsif max
58
+ return length <= max
59
+ end
60
+
61
+ return false
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,50 @@
1
+ module ValidationProfiler
2
+ module Rules
3
+ class ListValidationRule < ValidationProfiler::Rules::ValidationRule
4
+
5
+ def error_message(field, attributes = {}, parent = nil)
6
+
7
+ field_name = field.to_s
8
+ if parent != nil
9
+ field_name = "#{parent.to_s}.#{field.to_s}"
10
+ end
11
+
12
+ #check if a custom error message has been specified in the attributes
13
+ if attributes[:message] == nil
14
+ #no custom error message has been specified so create the default message.
15
+ "#{field_name} is not an accepted value."
16
+ else
17
+ attributes[:message]
18
+ end
19
+ end
20
+
21
+
22
+ def validate(obj, field, attributes = {}, parent = nil)
23
+
24
+ #attempt to get the field value from the object
25
+ field_value = get_field_value(obj, field)
26
+
27
+ if !is_required?(field_value, attributes)
28
+ return true
29
+ end
30
+
31
+ if field_value == nil
32
+ return false
33
+ end
34
+
35
+ list = attributes[:list]
36
+ if list == nil || !list.is_a?(Array)
37
+ raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::ListValidationRule, field)
38
+ end
39
+
40
+ if !list.include?(field_value)
41
+ return false
42
+ end
43
+
44
+ return true
45
+
46
+ end
47
+
48
+ end
49
+ end
50
+ end