validation_profiler 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/validation_profiler/exceptions/field_not_found.rb +16 -0
- data/lib/validation_profiler/exceptions/invalid_field_type.rb +18 -0
- data/lib/validation_profiler/exceptions/invalid_validation_rule_attributes.rb +18 -0
- data/lib/validation_profiler/exceptions/invalid_validation_rule_type.rb +16 -0
- data/lib/validation_profiler/exceptions/validation_rule_already_exists.rb +16 -0
- data/lib/validation_profiler/exceptions/validation_rule_not_found.rb +16 -0
- data/lib/validation_profiler/exceptions.rb +6 -0
- data/lib/validation_profiler/rules/child_validation_rule.rb +54 -0
- data/lib/validation_profiler/rules/condition_validation_rule.rb +93 -0
- data/lib/validation_profiler/rules/email_validation_rule.rb +44 -0
- data/lib/validation_profiler/rules/length_validation_rule.rb +66 -0
- data/lib/validation_profiler/rules/list_validation_rule.rb +50 -0
- data/lib/validation_profiler/rules/match_validation_rule.rb +46 -0
- data/lib/validation_profiler/rules/max_validation_rule.rb +52 -0
- data/lib/validation_profiler/rules/min_validation_rule.rb +52 -0
- data/lib/validation_profiler/rules/not_allowed_validation_rule.rb +37 -0
- data/lib/validation_profiler/rules/regex_validation_rule.rb +47 -0
- data/lib/validation_profiler/rules/required_validation_rule.rb +41 -0
- data/lib/validation_profiler/rules/validation_rule.rb +43 -0
- data/lib/validation_profiler/rules/validation_rule_manager.rb +71 -106
- data/lib/validation_profiler/rules.rb +23 -0
- data/lib/validation_profiler/version.rb +1 -1
- data/lib/validation_profiler.rb +40 -38
- metadata +22 -3
- data/lib/validation_profiler/rules/rules.rb +0 -632
@@ -0,0 +1,46 @@
|
|
1
|
+
module ValidationProfiler
|
2
|
+
module Rules
|
3
|
+
class MatchValidationRule < 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
|
+
match_field = attributes[:field]
|
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} does not match #{match_field}"
|
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
|
+
match_field = attributes[:field]
|
30
|
+
if match_field == nil
|
31
|
+
raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::MatchValidationRule, field)
|
32
|
+
end
|
33
|
+
|
34
|
+
if !is_required?(field_value, attributes)
|
35
|
+
return true
|
36
|
+
end
|
37
|
+
|
38
|
+
match_field_value = get_field_value(obj, match_field)
|
39
|
+
|
40
|
+
return field_value == match_field_value
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module ValidationProfiler
|
2
|
+
module Rules
|
3
|
+
class MaxValidationRule < 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
|
+
max = attributes[:value]
|
16
|
+
"#{field_name} must not have a value greater than #{max}"
|
17
|
+
else
|
18
|
+
attributes[:message]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def validate(obj, field, attributes, parent = nil)
|
23
|
+
|
24
|
+
max = attributes[:value]
|
25
|
+
|
26
|
+
#verify the expected attributes have been specified.
|
27
|
+
if max == nil
|
28
|
+
raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::MaxValidationRule, field)
|
29
|
+
end
|
30
|
+
|
31
|
+
#attempt to get the field value from the object
|
32
|
+
field_value = get_field_value(obj, field)
|
33
|
+
|
34
|
+
if !is_required?(field_value, attributes)
|
35
|
+
return true
|
36
|
+
end
|
37
|
+
|
38
|
+
if field_value == nil
|
39
|
+
return false
|
40
|
+
end
|
41
|
+
|
42
|
+
if field_value.is_a?(DateTime) || field_value.is_a?(Numeric)
|
43
|
+
field_value <= max
|
44
|
+
else
|
45
|
+
raise ValidationProfiler::Exceptions::InvalidFieldType.new(ValidationProfiler::Rules::MaxValidationRule, field)
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module ValidationProfiler
|
2
|
+
module Rules
|
3
|
+
class MinValidationRule < 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
|
+
min = attributes[:value]
|
16
|
+
"#{field_name} must have a minimum value of #{min}"
|
17
|
+
else
|
18
|
+
attributes[:message]
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def validate(obj, field, attributes, parent = nil)
|
23
|
+
|
24
|
+
min = attributes[:value]
|
25
|
+
|
26
|
+
#verify the expected attributes have been specified.
|
27
|
+
if min == nil
|
28
|
+
raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::MinValidationRule, field)
|
29
|
+
end
|
30
|
+
|
31
|
+
#attempt to get the field value from the object
|
32
|
+
field_value = get_field_value(obj, field)
|
33
|
+
|
34
|
+
if !is_required?(field_value, attributes)
|
35
|
+
return true
|
36
|
+
end
|
37
|
+
|
38
|
+
if field_value == nil
|
39
|
+
return false
|
40
|
+
end
|
41
|
+
|
42
|
+
if field_value.is_a?(DateTime) || field_value.is_a?(Numeric)
|
43
|
+
field_value >= min
|
44
|
+
else
|
45
|
+
raise ValidationProfiler::Exceptions::InvalidFieldType.new(ValidationProfiler::Rules::MinValidationRule, field)
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module ValidationProfiler
|
2
|
+
module Rules
|
3
|
+
class NotAllowedValidationRule < 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 allowed."
|
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 field_value != nil
|
28
|
+
return false
|
29
|
+
end
|
30
|
+
|
31
|
+
return true
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module ValidationProfiler
|
2
|
+
module Rules
|
3
|
+
class RegexValidationRule < 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 a 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
|
+
field_value = get_field_value(obj, field)
|
26
|
+
|
27
|
+
regex = attributes[:regex]
|
28
|
+
if regex == nil
|
29
|
+
raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::RegexValidationRule, field)
|
30
|
+
end
|
31
|
+
|
32
|
+
if !is_required?(field_value, attributes)
|
33
|
+
return true
|
34
|
+
end
|
35
|
+
|
36
|
+
#validate the value against the regex
|
37
|
+
if field_value =~ regex
|
38
|
+
return true
|
39
|
+
end
|
40
|
+
|
41
|
+
return false
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module ValidationProfiler
|
2
|
+
module Rules
|
3
|
+
class RequiredValidationRule < 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 required"
|
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 field_value == nil
|
28
|
+
return false
|
29
|
+
end
|
30
|
+
|
31
|
+
if field_value.is_a?(Array) || field_value.is_a?(String)
|
32
|
+
return !field_value.empty?
|
33
|
+
end
|
34
|
+
|
35
|
+
return true
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module ValidationProfiler
|
2
|
+
module Rules
|
3
|
+
class ValidationRule
|
4
|
+
|
5
|
+
def get_field_value(obj, field)
|
6
|
+
#attempt to get the field value from the object
|
7
|
+
field_value = nil
|
8
|
+
#check if the object is a hash
|
9
|
+
if obj.is_a?(Hash)
|
10
|
+
if obj.has_key?(field)
|
11
|
+
#get the field value
|
12
|
+
field_value = obj[field]
|
13
|
+
elsif obj.has_key?(field.to_s)
|
14
|
+
field_value = obj[field.to_s]
|
15
|
+
end
|
16
|
+
else
|
17
|
+
#if the object does not contain the specified field raise an exception
|
18
|
+
if !obj.respond_to?(field)
|
19
|
+
raise ValidationProfiler::Exceptions::FieldNotFound.new(field)
|
20
|
+
end
|
21
|
+
|
22
|
+
#get the field value
|
23
|
+
field_value = obj.send(field)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def is_required?(field_value, attributes = {})
|
28
|
+
required = attributes[:required]
|
29
|
+
if required == nil
|
30
|
+
required = true
|
31
|
+
end
|
32
|
+
|
33
|
+
#check if the field is required
|
34
|
+
if field_value == nil && required == false
|
35
|
+
return false
|
36
|
+
else
|
37
|
+
return true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -1,110 +1,75 @@
|
|
1
|
-
require_relative '../../../lib/validation_profiler/rules/
|
1
|
+
require_relative '../../../lib/validation_profiler/rules/child_validation_rule'
|
2
|
+
|
3
|
+
module ValidationProfiler
|
4
|
+
module Rules
|
5
|
+
# This is the manager class that holds all registered validation rules.
|
6
|
+
class Manager
|
7
|
+
|
8
|
+
class << self
|
9
|
+
attr_accessor :instance
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@rules = []
|
14
|
+
ValidationProfiler::Rules::Manager.instance = self
|
15
|
+
load_rules
|
16
|
+
end
|
17
|
+
|
18
|
+
# This method is called to get a validation rule by it's registered key.
|
19
|
+
#
|
20
|
+
# @params key [Symbol] This is the key of the validation rule you want to request.
|
21
|
+
#
|
22
|
+
# @return [ValidationRule] This is the requested ValidationRule instance.
|
23
|
+
def get_rule(key)
|
24
|
+
|
25
|
+
results = @rules.select { |r| r[:key] == key }
|
26
|
+
if !results.empty?
|
27
|
+
results[0][:instance]
|
28
|
+
else
|
29
|
+
raise ValidationProfiler::Exceptions::ValidationRuleNotFound.new(key)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# This method is called to add a validation rule to the manager for use.
|
34
|
+
#
|
35
|
+
# @param key [Symbol] This is the key to register the validation rule for.
|
36
|
+
# @param rule [ClassName] This is the class name of the validation rule to register.
|
37
|
+
def add_rule(key, rule)
|
38
|
+
|
39
|
+
instance = rule.new
|
40
|
+
|
41
|
+
#verify the rule instance inherits ValidationRule
|
42
|
+
if instance == nil || !instance.is_a?(ValidationRule)
|
43
|
+
raise ValidationProfiler::Exceptions::InvalidValidationRuleType.new(instance.class)
|
44
|
+
end
|
45
|
+
|
46
|
+
#verify the rule name has not already been registered
|
47
|
+
if !@rules.select { |r| r[:key] == key }.empty?
|
48
|
+
raise ValidationProfiler::Exceptions::ValidationRuleAlreadyExists.new(key)
|
49
|
+
end
|
50
|
+
|
51
|
+
@rules.push({ key: key, instance: instance})
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def load_rules
|
58
|
+
|
59
|
+
@rules.push({ key: :required, instance: ValidationProfiler::Rules::RequiredValidationRule.new })
|
60
|
+
@rules.push({ key: :length, instance: ValidationProfiler::Rules::LengthValidationRule.new })
|
61
|
+
@rules.push({ key: :min, instance: ValidationProfiler::Rules::MinValidationRule.new })
|
62
|
+
@rules.push({ key: :max, instance: ValidationProfiler::Rules::MaxValidationRule.new })
|
63
|
+
@rules.push({ key: :email, instance: ValidationProfiler::Rules::EmailValidationRule.new })
|
64
|
+
@rules.push({ key: :regex, instance: ValidationProfiler::Rules::RegexValidationRule.new })
|
65
|
+
@rules.push({ key: :match, instance: ValidationProfiler::Rules::MatchValidationRule.new })
|
66
|
+
@rules.push({ key: :condition, instance: ValidationProfiler::Rules::ConditionValidationRule.new })
|
67
|
+
@rules.push({ key: :not_allowed, instance: ValidationProfiler::Rules::NotAllowedValidationRule.new })
|
68
|
+
@rules.push({ key: :list, instance: ValidationProfiler::Rules::ListValidationRule.new })
|
69
|
+
@rules.push({ key: :child, instance: ValidationProfiler::Rules::ChildValidationRule.new })
|
70
|
+
|
71
|
+
end
|
2
72
|
|
3
|
-
# This is the manager class that holds all registered validation rules.
|
4
|
-
class ValidationRuleManager
|
5
|
-
|
6
|
-
class << self
|
7
|
-
attr_accessor :instance
|
8
|
-
end
|
9
|
-
|
10
|
-
def initialize
|
11
|
-
@rules = []
|
12
|
-
ValidationRuleManager.instance = self
|
13
|
-
load_rules
|
14
|
-
end
|
15
|
-
|
16
|
-
# This method is called to get a validation rule by it's registered key.
|
17
|
-
#
|
18
|
-
# @params key [Symbol] This is the key of the validation rule you want to request.
|
19
|
-
#
|
20
|
-
# @return [ValidationRule] This is the requested ValidationRule instance.
|
21
|
-
def get_rule(key)
|
22
|
-
|
23
|
-
results = @rules.select { |r| r[:key] == key }
|
24
|
-
if !results.empty?
|
25
|
-
results[0][:instance]
|
26
|
-
else
|
27
|
-
raise ValidationRuleNotFound.new(key)
|
28
73
|
end
|
29
74
|
end
|
30
|
-
|
31
|
-
# This method is called to add a validation rule to the manager for use.
|
32
|
-
#
|
33
|
-
# @param key [Symbol] This is the key to register the validation rule for.
|
34
|
-
# @param rule [ClassName] This is the class name of the validation rule to register.
|
35
|
-
def add_rule(key, rule)
|
36
|
-
|
37
|
-
instance = rule.new
|
38
|
-
|
39
|
-
#verify the rule instance inherits ValidationRule
|
40
|
-
if instance == nil || !instance.is_a?(ValidationRule)
|
41
|
-
raise InvalidRuleType.new(instance.class)
|
42
|
-
end
|
43
|
-
|
44
|
-
#verify the rule name has not already been registered
|
45
|
-
if !@rules.select { |r| r[:key] == key }.empty?
|
46
|
-
raise RuleAlreadyExists.new(key)
|
47
|
-
end
|
48
|
-
|
49
|
-
@rules.push({ key: key, instance: instance})
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
private
|
54
|
-
|
55
|
-
def load_rules
|
56
|
-
|
57
|
-
@rules.push({ key: :required, instance: RequiredValidationRule.new })
|
58
|
-
@rules.push({ key: :length, instance: LengthValidationRule.new })
|
59
|
-
@rules.push({ key: :min, instance: MinValidationRule.new })
|
60
|
-
@rules.push({ key: :max, instance: MaxValidationRule.new })
|
61
|
-
@rules.push({ key: :email, instance: EmailValidationRule.new })
|
62
|
-
@rules.push({ key: :regex, instance: RegexValidationRule.new })
|
63
|
-
@rules.push({ key: :match, instance: MatchValidationRule.new })
|
64
|
-
@rules.push({ key: :condition, instance: ConditionValidationRule.new })
|
65
|
-
@rules.push({ key: :not_allowed, instance: NotAllowedValidationRule.new })
|
66
|
-
@rules.push({ key: :list, instance: ListValidationRule.new })
|
67
|
-
@rules.push({ key: :child, instance: ChildValidationRule.new })
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
end
|
72
|
-
|
73
|
-
class ValidationRuleNotFound < StandardError
|
74
|
-
|
75
|
-
def initialize(rule)
|
76
|
-
@rule = rule
|
77
|
-
end
|
78
|
-
|
79
|
-
def to_s
|
80
|
-
rule = @rule
|
81
|
-
"Validation rule: #{rule} could not be found."
|
82
|
-
end
|
83
|
-
|
84
|
-
end
|
85
|
-
|
86
|
-
class InvalidRuleType < StandardError
|
87
|
-
|
88
|
-
def initialize(rule)
|
89
|
-
@rule = rule
|
90
|
-
end
|
91
|
-
|
92
|
-
def to_s
|
93
|
-
rule = @rule
|
94
|
-
"Validation rule: #{rule} is not a valid type. Rules must inherit from the ValidationRule base class."
|
95
|
-
end
|
96
|
-
|
97
|
-
end
|
98
|
-
|
99
|
-
class RuleAlreadyExists < StandardError
|
100
|
-
|
101
|
-
def initialize(key)
|
102
|
-
@key = key
|
103
|
-
end
|
104
|
-
|
105
|
-
def to_s
|
106
|
-
key = @key
|
107
|
-
"A Rule has already been registered for the key: #{key}."
|
108
|
-
end
|
109
|
-
|
110
75
|
end
|