-
Notifications
You must be signed in to change notification settings - Fork 5
/
validator.rb
199 lines (155 loc) · 4.64 KB
/
validator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# frozen_string_literal: true
module MiniDefender
class Validator
attr_reader :rules, :data, :errors
def initialize(rules, data)
@rules = rules
@data = data.flatten_keys(keep_roots: true)
@errors = nil
@validated = {}
@coerced = {}
@expander = RulesExpander.new
@factory = RulesFactory.new
@array_patterns = @expander.array_patterns(rules)
end
def validate
unless @errors.nil?
return
end
@errors = {}
data_rules = @rules.to_h do |k, set|
[k, @factory.init_set(set)]
end
# Set default values for missing data key compared to rules
data_rules.each do |k, set|
if !@data.key?(k) || missing_value?(@data[k])
set.filter{ |r| r.defaults?(self) }.each do |r|
@data.merge!({k => r.default_value(self)}.flatten_keys(keep_roots: true))
end
end
end
data_rules = @expander.expand(data_rules, @data)
data_rules.each do |k, rule_set|
@errors[k] = []
value_included = true
required = rule_set.any? { |r| r.implicit?(self) }
if !@data.key?(k) || missing_value?(@data[k])
rule_set.filter{ |r| r.defaults?(self) }.each do |r|
@data.merge!({k => r.default_value(self)}.flatten_keys(keep_roots: true))
end
end
unless @data.key?(k)
@errors[k] << 'This field is missing.' if required
next
end
value = coerced = @data[k]
force_coerce = false
rule_set.each do |rule|
next unless rule.active?(self)
value_included &= !rule.excluded?(self)
if rule.passes?(k, coerced, self)
coerced = rule.coerce(coerced)
force_coerce = rule.force_coerce?
else
@errors[k] << rule.error_message(k, coerced, self)
end
end
if force_coerce
value = coerced
end
if @errors[k].empty? && value_included
@validated[k] = value
@coerced[k] = coerced
end
end
@errors.reject! { |_, v| v.empty? }
end
def validate!
validate if @errors.nil?
unless @errors.empty?
raise ValidationError.new('Data validation failed', @errors)
end
end
def passes?
validate if @errors.nil?
@errors.empty?
end
def fails?
!passes?
end
def validated
validate! if @errors.nil?
@validated_compacted ||= compact_expanded(@validated)
end
def coerced
validate! if @errors.nil?
@coerced_compacted = compact_expanded(@coerced)
end
# @return [Hash]
def neighbors(key, level = 1)
pattern = leveled_search_pattern(key, level)
@data.filter { |k, _| k.match?(pattern) }
end
# @return [Array]
def neighbor_values(key, level = 1)
neighbors(key, level).values
end
private
def leveled_search_pattern(key, level = 1)
search_key = key.split('.').map{ |part| Regexp.quote(part) }.reverse
# Change array indexes to digits pattern according to the desired level
while level > 0
target_index = search_key.index { |p| p.match?(/\A\d+\z/) }
break if target_index.nil?
search_key[target_index] = '\d+'
level -= 1
end
Regexp.compile("\\A#{search_key.reverse.join('\.')}\\z")
end
def compact_expanded(hash)
expanded = {}
hash.each do |k, v|
keys = k.split('.')
node = expanded
while keys.length > 1
next_key = keys.shift
next_node = (node[next_key] ||= {})
if next_node.is_a?(Array)
node[next_key] = next_node = next_node.each_with_index.to_h do |value, index|
[index.to_s, value]
end
end
node = next_node
end
node[keys.shift] = v
end
compact_keys(expanded)
end
def compact_keys(hash, current_key = '')
current_key_suffixed = if current_key == ''
''
else
"#{current_key}."
end
result = hash.to_h do |k, v|
[k, v.is_a?(Hash) ? compact_keys(v, current_key_suffixed + k) : v]
end
if result.empty?
return result
end
unless @array_patterns.any? { |p| p.match?("#{current_key_suffixed}#{result.keys.first}") }
return result
end
if result.all? { |k, _v| k.match?(/\A\d+\z/) }
return result.values
end
result
end
def missing_value?(value)
value.nil? ||
value.is_a?(String) && value.strip.empty? ||
value.is_a?(Hash) && value.empty? ||
value.is_a?(Array) && value.empty?
end
end
end