validation_profiler 1.3.5 → 1.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/validation_profiler/rules/condition_validation_rule.rb +30 -49
- 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: b30c9295a13e83dfcf6acbf158e520f1679ee1cd
|
4
|
+
data.tar.gz: b096548df1dceba53873a7bd3b165d9476c72d68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62c0c1e1f28d8a74fe83ba003924d33f4949e920b1a4d7e1fd7933fe47505c7c695b89c6440d9728ae2b5258660a932f3f1c019ec4ac5699488969096c1c3e32
|
7
|
+
data.tar.gz: a6ba25274094599fc17fe07b4a5a42599a853d53172ef7c51081c031c7da82bbe93f00590c00d7cd835353933e594d3a3e9f198897791f97d4aaba658f6691ef
|
@@ -1,93 +1,74 @@
|
|
1
1
|
module ValidationProfiler
|
2
2
|
module Rules
|
3
|
+
# Defines the ConditionValidationRule class
|
3
4
|
class ConditionValidationRule < ValidationProfiler::Rules::ValidationRule
|
4
|
-
|
5
5
|
def error_message(field, attributes = {}, parent = nil)
|
6
|
-
|
7
6
|
field_name = field.to_s
|
8
|
-
|
9
|
-
field_name = "#{parent.to_s}.#{field.to_s}"
|
10
|
-
end
|
7
|
+
field_name = "#{parent}.#{field}" unless parent.nil?
|
11
8
|
|
12
|
-
#check if a custom error message has been specified in the attributes
|
13
|
-
if attributes[:message]
|
14
|
-
#no custom error message has been specified so create the default message.
|
9
|
+
# check if a custom error message has been specified in the attributes
|
10
|
+
if attributes[:message].nil?
|
11
|
+
# no custom error message has been specified so create the default message.
|
15
12
|
"#{field_name} is not valid"
|
16
13
|
else
|
17
14
|
attributes[:message]
|
18
15
|
end
|
19
16
|
end
|
20
17
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
#attempt to get the field value from the object
|
18
|
+
def validate(obj, field, attributes, _parent = nil)
|
19
|
+
# attempt to get the field value from the object
|
25
20
|
value = get_field_value(obj, field)
|
26
21
|
|
27
22
|
condition_field = attributes[:condition_field]
|
28
|
-
if condition_field
|
29
|
-
raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes
|
23
|
+
if condition_field.nil?
|
24
|
+
raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes
|
25
|
+
.new(ValidationProfiler::Rules::ConditionValidationRule, field)
|
30
26
|
end
|
31
27
|
|
32
28
|
condition_value = attributes[:condition_value]
|
33
|
-
if condition_value
|
34
|
-
raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes
|
29
|
+
if condition_value.nil?
|
30
|
+
raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes
|
31
|
+
.new(ValidationProfiler::Rules::ConditionValidationRule, field)
|
35
32
|
end
|
36
33
|
|
37
34
|
condition_expression = attributes[:condition_expression]
|
38
|
-
if condition_expression
|
39
|
-
raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes
|
35
|
+
if condition_expression.nil?
|
36
|
+
raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes
|
37
|
+
.new(ValidationProfiler::Rules::ConditionValidationRule, field)
|
40
38
|
end
|
41
39
|
|
42
40
|
field_expression = attributes[:field_expression]
|
43
|
-
if field_expression
|
44
|
-
raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes
|
41
|
+
if field_expression.nil?
|
42
|
+
raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes
|
43
|
+
.new(ValidationProfiler::Rules::ConditionValidationRule, field)
|
45
44
|
end
|
46
45
|
|
47
46
|
field_value = attributes[:field_value]
|
48
|
-
if field_value
|
49
|
-
raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes
|
47
|
+
if field_value.nil?
|
48
|
+
raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes
|
49
|
+
.new(ValidationProfiler::Rules::ConditionValidationRule, field)
|
50
50
|
end
|
51
51
|
|
52
|
-
|
53
|
-
return true
|
54
|
-
end
|
52
|
+
return true unless is_required?(value, attributes)
|
55
53
|
|
56
54
|
condition_field_value = get_field_value(obj, condition_field)
|
57
55
|
|
58
|
-
#check if the condition is valid
|
56
|
+
# check if the condition is valid
|
59
57
|
if confirm_expression?(condition_field_value, condition_expression, condition_value)
|
60
|
-
#check if the field value is correct for the condition
|
61
|
-
|
62
|
-
|
63
|
-
else
|
64
|
-
return false
|
65
|
-
end
|
58
|
+
# check if the field value is correct for the condition
|
59
|
+
return false unless confirm_expression?(value, field_expression, field_value)
|
60
|
+
return true
|
66
61
|
end
|
67
62
|
|
68
|
-
|
69
|
-
|
63
|
+
true
|
70
64
|
end
|
71
65
|
|
72
66
|
def confirm_expression?(value_a, expression, value_b)
|
73
67
|
a = value_a
|
74
68
|
b = value_b
|
75
69
|
|
76
|
-
|
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}")
|
70
|
+
a.public_send(expression.to_s, b)
|
89
71
|
end
|
90
|
-
|
91
72
|
end
|
92
73
|
end
|
93
|
-
end
|
74
|
+
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: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sage One
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -126,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
126
126
|
version: '0'
|
127
127
|
requirements: []
|
128
128
|
rubyforge_project:
|
129
|
-
rubygems_version: 2.
|
129
|
+
rubygems_version: 2.6.13
|
130
130
|
signing_key:
|
131
131
|
specification_version: 4
|
132
132
|
summary: A Validation framework for creating validation profiles, allowing for the
|