validation_profiler 1.6.0 → 1.7.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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 043535effac77ff6205ee511cc20214c5dad1de512014ecf1db561891e025791
|
4
|
+
data.tar.gz: 0d8d7081096e6a7375efc624a4a202597a14a6975d21d7d8e7996ba7590df7f2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4c38cb57d62d8c4f324b27daf6d7eda4371474cfa608b863b7b158a2eeefe96cd8f66dbd32da9003560940eb3d5058c3d1967c2986c76edcf68738368d1c63b0
|
7
|
+
data.tar.gz: ba93d8133e0497e89d5e6d0a60f3d93943eb29424d4eaba0ee5a07b7aa36c5c088a6cc4a2d400730b9465611077f36f28de976d57e83ee164d246a5c58e499a5
|
@@ -1,6 +1,5 @@
|
|
1
1
|
module ValidationProfiler
|
2
2
|
class Manager
|
3
|
-
|
4
3
|
# Called to validate an object against a validation profile.
|
5
4
|
#
|
6
5
|
# @param obj [Object] The object to validate
|
@@ -8,33 +7,32 @@ module ValidationProfiler
|
|
8
7
|
#
|
9
8
|
# @return [ValidationManagerResult] The result of the validation
|
10
9
|
def validate(obj, profile, parent = nil)
|
11
|
-
|
12
10
|
result = ValidationProfiler::ManagerResult.new
|
13
11
|
|
14
12
|
validation_rules = profile.class_variable_get(:@@validation_rules)
|
15
13
|
|
16
14
|
validation_rules.each do |r|
|
17
15
|
|
18
|
-
if ValidationProfiler::Rules::Manager.instance
|
16
|
+
if ValidationProfiler::Rules::Manager.instance.nil?
|
19
17
|
ValidationProfiler::Rules::Manager.new
|
20
18
|
end
|
21
19
|
|
22
20
|
rule = ValidationProfiler::Rules::Manager.instance.get_rule(r[:name])
|
23
21
|
outcome = rule.validate(obj, r[:field], r[:attributes], parent)
|
24
22
|
|
25
|
-
if outcome.is_a?(
|
23
|
+
if outcome.is_a?(Array)
|
24
|
+
result.errors = outcome.map(&:errors).flatten
|
25
|
+
result.outcome = result.errors.empty?
|
26
|
+
elsif outcome.is_a?(ValidationProfiler::ManagerResult) && !outcome.outcome
|
26
27
|
result.errors = result.errors + outcome.errors
|
27
28
|
result.outcome = false
|
28
29
|
elsif !outcome
|
29
30
|
result.outcome = false
|
30
|
-
result.errors.push(
|
31
|
+
result.errors.push(field: r[:field], message: rule.error_message(r[:field], r[:attributes], parent))
|
31
32
|
end
|
32
|
-
|
33
33
|
end
|
34
34
|
|
35
35
|
result
|
36
|
-
|
37
36
|
end
|
38
|
-
|
39
37
|
end
|
40
|
-
end
|
38
|
+
end
|
@@ -1,17 +1,14 @@
|
|
1
1
|
module ValidationProfiler
|
2
2
|
module Rules
|
3
|
+
# Validation rule for child attributes
|
3
4
|
class ChildValidationRule < 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 required."
|
16
13
|
else
|
17
14
|
attributes[:message]
|
@@ -19,32 +16,41 @@ module ValidationProfiler
|
|
19
16
|
end
|
20
17
|
|
21
18
|
def validate(obj, field, attributes = {}, parent = nil)
|
22
|
-
|
23
|
-
#attempt to get the field value from the object
|
19
|
+
# attempt to get the field value from the object
|
24
20
|
field_value = get_field_value(obj, field)
|
25
21
|
|
26
|
-
|
27
|
-
|
28
|
-
|
22
|
+
return true unless is_required?(field_value, attributes)
|
23
|
+
return false if field_value.nil?
|
24
|
+
return false if field_value.respond_to?(:empty?) && field_value.empty?
|
29
25
|
|
30
|
-
|
31
|
-
|
32
|
-
raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes.new(ValidationProfiler::Rules::ChildValidationRule, field)
|
33
|
-
end
|
26
|
+
validation_profile = get_validation_profile(attributes, field)
|
27
|
+
parent_field = build_parent_field(parent, field)
|
34
28
|
|
35
|
-
|
36
|
-
|
37
|
-
end
|
29
|
+
validate_field_value(field_value, validation_profile, parent_field)
|
30
|
+
end
|
38
31
|
|
39
|
-
|
40
|
-
if
|
41
|
-
|
32
|
+
def validate_field_value(field_value, profile, parent_field)
|
33
|
+
if field_value.is_a? Array
|
34
|
+
field_value.map { |value| ValidationProfiler::Manager.new.validate(value, profile, parent_field) }
|
35
|
+
else
|
36
|
+
ValidationProfiler::Manager.new.validate(field_value, profile, parent_field)
|
42
37
|
end
|
38
|
+
end
|
43
39
|
|
44
|
-
|
40
|
+
def build_parent_field(parent, field)
|
41
|
+
return field.to_s if parent.nil?
|
45
42
|
|
43
|
+
"#{parent}.#{field}"
|
46
44
|
end
|
47
45
|
|
46
|
+
def get_validation_profile(attributes, field)
|
47
|
+
if attributes[:profile].nil?
|
48
|
+
raise ValidationProfiler::Exceptions::InvalidValidationRuleAttributes
|
49
|
+
.new(ValidationProfiler::Rules::ChildValidationRule, field)
|
50
|
+
end
|
51
|
+
|
52
|
+
attributes[:profile]
|
53
|
+
end
|
48
54
|
end
|
49
55
|
end
|
50
|
-
end
|
56
|
+
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.7.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: 2019-
|
11
|
+
date: 2019-06-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -140,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
140
|
- !ruby/object:Gem::Version
|
141
141
|
version: '0'
|
142
142
|
requirements: []
|
143
|
-
rubygems_version: 3.0.
|
143
|
+
rubygems_version: 3.0.3
|
144
144
|
signing_key:
|
145
145
|
specification_version: 4
|
146
146
|
summary: A Validation framework for creating validation profiles, allowing for the
|