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.
- 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,23 @@
|
|
1
|
+
require_relative 'rules/validation_rule'
|
2
|
+
|
3
|
+
###############################
|
4
|
+
###Add Validation Rules Here###
|
5
|
+
###############################
|
6
|
+
|
7
|
+
require_relative 'rules/child_validation_rule'
|
8
|
+
require_relative 'rules/condition_validation_rule'
|
9
|
+
require_relative 'rules/email_validation_rule'
|
10
|
+
require_relative 'rules/length_validation_rule'
|
11
|
+
require_relative 'rules/list_validation_rule'
|
12
|
+
require_relative 'rules/match_validation_rule'
|
13
|
+
require_relative 'rules/max_validation_rule'
|
14
|
+
require_relative 'rules/min_validation_rule'
|
15
|
+
require_relative 'rules/not_allowed_validation_rule'
|
16
|
+
require_relative 'rules/regex_validation_rule'
|
17
|
+
require_relative 'rules/required_validation_rule'
|
18
|
+
|
19
|
+
|
20
|
+
################################
|
21
|
+
|
22
|
+
require_relative 'rules/validation_rule_manager'
|
23
|
+
|
data/lib/validation_profiler.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'validation_profiler/version'
|
2
|
-
require 'validation_profiler/
|
3
|
-
require 'validation_profiler/rules
|
2
|
+
require 'validation_profiler/exceptions'
|
3
|
+
require 'validation_profiler/rules'
|
4
4
|
|
5
5
|
class Class
|
6
6
|
|
@@ -28,56 +28,58 @@ class Class
|
|
28
28
|
|
29
29
|
end
|
30
30
|
|
31
|
-
|
31
|
+
module ValidationProfiler
|
32
|
+
class Manager
|
32
33
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
34
|
+
# Called to validate an object against a validation profile.
|
35
|
+
#
|
36
|
+
# @param obj [Object] The object to validate
|
37
|
+
# @param profile [ClassName] The class name of the validation profile to validate against
|
38
|
+
#
|
39
|
+
# @return [ValidationManagerResult] The result of the validation
|
40
|
+
def validate(obj, profile, parent = nil)
|
40
41
|
|
41
|
-
|
42
|
+
result = ValidationProfiler::ManagerResult.new
|
42
43
|
|
43
|
-
|
44
|
+
validation_rules = profile.class_variable_get(:@@validation_rules)
|
44
45
|
|
45
|
-
|
46
|
+
validation_rules.each do |r|
|
46
47
|
|
47
|
-
|
48
|
-
|
49
|
-
|
48
|
+
if ValidationProfiler::Rules::Manager.instance == nil
|
49
|
+
ValidationProfiler::Rules::Manager.new
|
50
|
+
end
|
50
51
|
|
51
|
-
|
52
|
-
|
52
|
+
rule = ValidationProfiler::Rules::Manager.instance.get_rule(r[:name])
|
53
|
+
outcome = rule.validate(obj, r[:field], r[:attributes], parent)
|
54
|
+
|
55
|
+
if outcome.is_a?(ValidationProfiler::ManagerResult)
|
56
|
+
result = outcome
|
57
|
+
elsif !outcome
|
58
|
+
result.outcome = false
|
59
|
+
result.errors.push({ field: r[:field], message: rule.error_message(r[:field], r[:attributes], parent) })
|
60
|
+
end
|
53
61
|
|
54
|
-
if outcome.is_a?(ValidationManagerResult)
|
55
|
-
result = outcome
|
56
|
-
elsif !outcome
|
57
|
-
result.outcome = false
|
58
|
-
result.errors.push({ field: r[:field], message: rule.error_message(r[:field], r[:attributes], parent) })
|
59
62
|
end
|
60
63
|
|
61
|
-
|
64
|
+
result
|
62
65
|
|
63
|
-
|
66
|
+
end
|
64
67
|
|
65
68
|
end
|
66
69
|
|
67
|
-
|
68
|
-
|
69
|
-
# This is used to specify validation results from the #validate method of the ValidationManager
|
70
|
-
class ValidationManagerResult
|
70
|
+
# This is used to specify validation results from the #validate method of the Validation Manager
|
71
|
+
class ManagerResult
|
71
72
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
73
|
+
# @!attribute outcome
|
74
|
+
# @return [Boolean] The outcome of the validation.
|
75
|
+
attr_accessor :outcome
|
76
|
+
# @!attribute errors
|
77
|
+
# @return [Array[{:field, :error_message}]] An array of field errors that occurred during validation.
|
78
|
+
attr_accessor :errors
|
78
79
|
|
79
|
-
|
80
|
-
|
81
|
-
|
80
|
+
def initialize
|
81
|
+
@errors = []
|
82
|
+
@outcome = true
|
83
|
+
end
|
82
84
|
end
|
83
85
|
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: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- vaughanbrittonsage
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -77,7 +77,26 @@ files:
|
|
77
77
|
- bin/console
|
78
78
|
- bin/setup
|
79
79
|
- lib/validation_profiler.rb
|
80
|
-
- lib/validation_profiler/
|
80
|
+
- lib/validation_profiler/exceptions.rb
|
81
|
+
- lib/validation_profiler/exceptions/field_not_found.rb
|
82
|
+
- lib/validation_profiler/exceptions/invalid_field_type.rb
|
83
|
+
- lib/validation_profiler/exceptions/invalid_validation_rule_attributes.rb
|
84
|
+
- lib/validation_profiler/exceptions/invalid_validation_rule_type.rb
|
85
|
+
- lib/validation_profiler/exceptions/validation_rule_already_exists.rb
|
86
|
+
- lib/validation_profiler/exceptions/validation_rule_not_found.rb
|
87
|
+
- lib/validation_profiler/rules.rb
|
88
|
+
- lib/validation_profiler/rules/child_validation_rule.rb
|
89
|
+
- lib/validation_profiler/rules/condition_validation_rule.rb
|
90
|
+
- lib/validation_profiler/rules/email_validation_rule.rb
|
91
|
+
- lib/validation_profiler/rules/length_validation_rule.rb
|
92
|
+
- lib/validation_profiler/rules/list_validation_rule.rb
|
93
|
+
- lib/validation_profiler/rules/match_validation_rule.rb
|
94
|
+
- lib/validation_profiler/rules/max_validation_rule.rb
|
95
|
+
- lib/validation_profiler/rules/min_validation_rule.rb
|
96
|
+
- lib/validation_profiler/rules/not_allowed_validation_rule.rb
|
97
|
+
- lib/validation_profiler/rules/regex_validation_rule.rb
|
98
|
+
- lib/validation_profiler/rules/required_validation_rule.rb
|
99
|
+
- lib/validation_profiler/rules/validation_rule.rb
|
81
100
|
- lib/validation_profiler/rules/validation_rule_manager.rb
|
82
101
|
- lib/validation_profiler/version.rb
|
83
102
|
homepage: https://github.com/vaughanbrittonsage/validation_profiler
|
@@ -1,632 +0,0 @@
|
|
1
|
-
|
2
|
-
class InvalidRuleAttributes < StandardError
|
3
|
-
|
4
|
-
def initialize(rule, field)
|
5
|
-
@rule = rule
|
6
|
-
@field = field
|
7
|
-
end
|
8
|
-
|
9
|
-
def to_s
|
10
|
-
rule = @rule
|
11
|
-
field = @field
|
12
|
-
"Incorrect attributes specified for Validation rule: #{rule} Field: #{field}."
|
13
|
-
end
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
class FieldNotFound < StandardError
|
18
|
-
|
19
|
-
def initialize(field)
|
20
|
-
@field = field
|
21
|
-
end
|
22
|
-
|
23
|
-
def to_s
|
24
|
-
field = @field
|
25
|
-
"Field: #{field} could not be found."
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
class InvalidFieldType < StandardError
|
31
|
-
|
32
|
-
def initialize(rule, field)
|
33
|
-
@rule = rule
|
34
|
-
@field = field
|
35
|
-
end
|
36
|
-
|
37
|
-
def to_s
|
38
|
-
rule = @rule
|
39
|
-
field = @field
|
40
|
-
"Field: #{field} has an incorrect value type for Validation Rule: #{rule}."
|
41
|
-
end
|
42
|
-
|
43
|
-
end
|
44
|
-
|
45
|
-
class ValidationRule
|
46
|
-
|
47
|
-
def get_field_value(obj, field)
|
48
|
-
#attempt to get the field value from the object
|
49
|
-
field_value = nil
|
50
|
-
#check if the object is a hash
|
51
|
-
if obj.is_a?(Hash)
|
52
|
-
if obj.has_key?(field)
|
53
|
-
#get the field value
|
54
|
-
field_value = obj[field]
|
55
|
-
elsif obj.has_key?(field.to_s)
|
56
|
-
field_value = obj[field.to_s]
|
57
|
-
end
|
58
|
-
else
|
59
|
-
#if the object does not contain the specified field raise an exception
|
60
|
-
if !obj.respond_to?(field)
|
61
|
-
raise FieldNotFound.new(field)
|
62
|
-
end
|
63
|
-
|
64
|
-
#get the field value
|
65
|
-
field_value = obj.send(field)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def is_required?(field_value, attributes = {})
|
70
|
-
required = attributes[:required]
|
71
|
-
if required == nil
|
72
|
-
required = true
|
73
|
-
end
|
74
|
-
|
75
|
-
#check if the field is required
|
76
|
-
if field_value == nil && required == false
|
77
|
-
return false
|
78
|
-
else
|
79
|
-
return true
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
end
|
84
|
-
|
85
|
-
class LengthValidationRule < ValidationRule
|
86
|
-
|
87
|
-
def error_message(field, attributes, parent = nil)
|
88
|
-
field_name = field.to_s
|
89
|
-
if parent != nil
|
90
|
-
field_name = "#{parent.to_s}.#{field.to_s}"
|
91
|
-
end
|
92
|
-
|
93
|
-
#check if a custom error message has been specified in the attributes
|
94
|
-
if attributes[:message] == nil
|
95
|
-
#no custom error message has been specified so create the default message.
|
96
|
-
min = attributes[:min]
|
97
|
-
max = attributes[:max]
|
98
|
-
if min && max
|
99
|
-
"#{field_name} must have a min length of #{min} and a max length of #{max}"
|
100
|
-
elsif min
|
101
|
-
"#{field_name} must have a min length of #{min}"
|
102
|
-
else
|
103
|
-
"#{field_name} must have a max length of #{max}"
|
104
|
-
end
|
105
|
-
else
|
106
|
-
attributes[:message]
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
def validate(obj, field, attributes, parent = nil)
|
111
|
-
|
112
|
-
min = attributes[:min]
|
113
|
-
max = attributes[:max]
|
114
|
-
|
115
|
-
#verify the expected attributes have been specified.
|
116
|
-
if min == nil && max == nil
|
117
|
-
raise InvalidRuleAttributes.new(LengthValidationRule, field)
|
118
|
-
end
|
119
|
-
|
120
|
-
#attempt to get the field value from the object
|
121
|
-
field_value = get_field_value(obj, field)
|
122
|
-
|
123
|
-
#check if this validation check should be skipped
|
124
|
-
if !is_required?(field_value, attributes)
|
125
|
-
return true
|
126
|
-
end
|
127
|
-
|
128
|
-
if field_value.is_a?(Array)
|
129
|
-
length = field_value.length
|
130
|
-
else
|
131
|
-
length = field_value.to_s.length
|
132
|
-
end
|
133
|
-
|
134
|
-
#validate the field value
|
135
|
-
if min && max
|
136
|
-
return length >= min && length <= max
|
137
|
-
elsif min
|
138
|
-
return length >= min
|
139
|
-
elsif max
|
140
|
-
return length <= max
|
141
|
-
end
|
142
|
-
|
143
|
-
return false
|
144
|
-
end
|
145
|
-
|
146
|
-
end
|
147
|
-
|
148
|
-
class MinValidationRule < ValidationRule
|
149
|
-
|
150
|
-
def error_message(field, attributes, parent = nil)
|
151
|
-
|
152
|
-
field_name = field.to_s
|
153
|
-
if parent != nil
|
154
|
-
field_name = "#{parent.to_s}.#{field.to_s}"
|
155
|
-
end
|
156
|
-
|
157
|
-
#check if a custom error message has been specified in the attributes
|
158
|
-
if attributes[:message] == nil
|
159
|
-
#no custom error message has been specified so create the default message.
|
160
|
-
min = attributes[:value]
|
161
|
-
"#{field_name} must have a minimum value of #{min}"
|
162
|
-
else
|
163
|
-
attributes[:message]
|
164
|
-
end
|
165
|
-
end
|
166
|
-
|
167
|
-
def validate(obj, field, attributes, parent = nil)
|
168
|
-
|
169
|
-
min = attributes[:value]
|
170
|
-
|
171
|
-
#verify the expected attributes have been specified.
|
172
|
-
if min == nil
|
173
|
-
raise InvalidRuleAttributes.new(MinValidationRule, field)
|
174
|
-
end
|
175
|
-
|
176
|
-
#attempt to get the field value from the object
|
177
|
-
field_value = get_field_value(obj, field)
|
178
|
-
|
179
|
-
if !is_required?(field_value, attributes)
|
180
|
-
return true
|
181
|
-
end
|
182
|
-
|
183
|
-
if field_value == nil
|
184
|
-
return false
|
185
|
-
end
|
186
|
-
|
187
|
-
if field_value.is_a?(DateTime) || field_value.is_a?(Numeric)
|
188
|
-
field_value >= min
|
189
|
-
else
|
190
|
-
raise InvalidFieldType.new(MinValidationRule, field)
|
191
|
-
end
|
192
|
-
|
193
|
-
end
|
194
|
-
|
195
|
-
end
|
196
|
-
|
197
|
-
class MaxValidationRule < ValidationRule
|
198
|
-
|
199
|
-
def error_message(field, attributes, parent = nil)
|
200
|
-
|
201
|
-
field_name = field.to_s
|
202
|
-
if parent != nil
|
203
|
-
field_name = "#{parent.to_s}.#{field.to_s}"
|
204
|
-
end
|
205
|
-
|
206
|
-
#check if a custom error message has been specified in the attributes
|
207
|
-
if attributes[:message] == nil
|
208
|
-
#no custom error message has been specified so create the default message.
|
209
|
-
max = attributes[:value]
|
210
|
-
"#{field_name} must not have a value greater than #{max}"
|
211
|
-
else
|
212
|
-
attributes[:message]
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
def validate(obj, field, attributes, parent = nil)
|
217
|
-
|
218
|
-
max = attributes[:value]
|
219
|
-
|
220
|
-
#verify the expected attributes have been specified.
|
221
|
-
if max == nil
|
222
|
-
raise InvalidRuleAttributes.new(MinValidationRule, field)
|
223
|
-
end
|
224
|
-
|
225
|
-
#attempt to get the field value from the object
|
226
|
-
field_value = get_field_value(obj, field)
|
227
|
-
|
228
|
-
if !is_required?(field_value, attributes)
|
229
|
-
return true
|
230
|
-
end
|
231
|
-
|
232
|
-
if field_value == nil
|
233
|
-
return false
|
234
|
-
end
|
235
|
-
|
236
|
-
if field_value.is_a?(DateTime) || field_value.is_a?(Numeric)
|
237
|
-
field_value <= max
|
238
|
-
else
|
239
|
-
raise InvalidFieldType.new(MaxValidationRule, field)
|
240
|
-
end
|
241
|
-
|
242
|
-
end
|
243
|
-
|
244
|
-
end
|
245
|
-
|
246
|
-
class RequiredValidationRule < ValidationRule
|
247
|
-
|
248
|
-
def error_message(field, attributes = {}, parent = nil)
|
249
|
-
|
250
|
-
field_name = field.to_s
|
251
|
-
if parent != nil
|
252
|
-
field_name = "#{parent.to_s}.#{field.to_s}"
|
253
|
-
end
|
254
|
-
|
255
|
-
#check if a custom error message has been specified in the attributes
|
256
|
-
if attributes[:message] == nil
|
257
|
-
#no custom error message has been specified so create the default message.
|
258
|
-
"#{field_name} is required"
|
259
|
-
else
|
260
|
-
attributes[:message]
|
261
|
-
end
|
262
|
-
end
|
263
|
-
|
264
|
-
|
265
|
-
def validate(obj, field, attributes = {}, parent = nil)
|
266
|
-
|
267
|
-
#attempt to get the field value from the object
|
268
|
-
field_value = get_field_value(obj, field)
|
269
|
-
|
270
|
-
if field_value == nil
|
271
|
-
return false
|
272
|
-
end
|
273
|
-
|
274
|
-
if field_value.is_a?(Array) || field_value.is_a?(String)
|
275
|
-
return !field_value.empty?
|
276
|
-
end
|
277
|
-
|
278
|
-
return true
|
279
|
-
|
280
|
-
end
|
281
|
-
|
282
|
-
end
|
283
|
-
|
284
|
-
class EmailValidationRule < ValidationRule
|
285
|
-
|
286
|
-
REGEX = /^[^@]+@[^@]+\.[^@]+$/
|
287
|
-
|
288
|
-
def error_message(field, attributes = {}, parent = nil)
|
289
|
-
|
290
|
-
field_name = field.to_s
|
291
|
-
if parent != nil
|
292
|
-
field_name = "#{parent.to_s}.#{field.to_s}"
|
293
|
-
end
|
294
|
-
|
295
|
-
#check if a custom error message has been specified in the attributes
|
296
|
-
if attributes[:message] == nil
|
297
|
-
#no custom error message has been specified so create the default message.
|
298
|
-
"#{field_name} is not a valid email address"
|
299
|
-
else
|
300
|
-
attributes[:message]
|
301
|
-
end
|
302
|
-
end
|
303
|
-
|
304
|
-
|
305
|
-
def validate(obj, field, attributes = {}, parent = nil)
|
306
|
-
|
307
|
-
#attempt to get the field value from the object
|
308
|
-
field_value = get_field_value(obj, field)
|
309
|
-
|
310
|
-
if !is_required?(field_value, attributes)
|
311
|
-
return true
|
312
|
-
end
|
313
|
-
|
314
|
-
#validate the value against the regex
|
315
|
-
if field_value =~ REGEX
|
316
|
-
return true
|
317
|
-
end
|
318
|
-
|
319
|
-
return false
|
320
|
-
|
321
|
-
end
|
322
|
-
|
323
|
-
end
|
324
|
-
|
325
|
-
class RegexValidationRule < ValidationRule
|
326
|
-
|
327
|
-
def error_message(field, attributes = {}, parent = nil)
|
328
|
-
|
329
|
-
field_name = field.to_s
|
330
|
-
if parent != nil
|
331
|
-
field_name = "#{parent.to_s}.#{field.to_s}"
|
332
|
-
end
|
333
|
-
|
334
|
-
#check if a custom error message has been specified in the attributes
|
335
|
-
if attributes[:message] == nil
|
336
|
-
#no custom error message has been specified so create the default message.
|
337
|
-
"#{field_name} is not a valid"
|
338
|
-
else
|
339
|
-
attributes[:message]
|
340
|
-
end
|
341
|
-
end
|
342
|
-
|
343
|
-
|
344
|
-
def validate(obj, field, attributes, parent = nil)
|
345
|
-
|
346
|
-
#attempt to get the field value from the object
|
347
|
-
field_value = get_field_value(obj, field)
|
348
|
-
|
349
|
-
regex = attributes[:regex]
|
350
|
-
if regex == nil
|
351
|
-
raise InvalidRuleAttributes.new(RegexValidationRule, field)
|
352
|
-
end
|
353
|
-
|
354
|
-
if !is_required?(field_value, attributes)
|
355
|
-
return true
|
356
|
-
end
|
357
|
-
|
358
|
-
#validate the value against the regex
|
359
|
-
if field_value =~ regex
|
360
|
-
return true
|
361
|
-
end
|
362
|
-
|
363
|
-
return false
|
364
|
-
|
365
|
-
end
|
366
|
-
|
367
|
-
end
|
368
|
-
|
369
|
-
class MatchValidationRule < ValidationRule
|
370
|
-
|
371
|
-
def error_message(field, attributes, parent = nil)
|
372
|
-
|
373
|
-
field_name = field.to_s
|
374
|
-
if parent != nil
|
375
|
-
field_name = "#{parent.to_s}.#{field.to_s}"
|
376
|
-
end
|
377
|
-
|
378
|
-
match_field = attributes[:field]
|
379
|
-
|
380
|
-
#check if a custom error message has been specified in the attributes
|
381
|
-
if attributes[:message] == nil
|
382
|
-
#no custom error message has been specified so create the default message.
|
383
|
-
"#{field_name} does not match #{match_field}"
|
384
|
-
else
|
385
|
-
attributes[:message]
|
386
|
-
end
|
387
|
-
end
|
388
|
-
|
389
|
-
|
390
|
-
def validate(obj, field, attributes, parent = nil)
|
391
|
-
|
392
|
-
#attempt to get the field value from the object
|
393
|
-
field_value = get_field_value(obj, field)
|
394
|
-
|
395
|
-
match_field = attributes[:field]
|
396
|
-
if match_field == nil
|
397
|
-
raise InvalidRuleAttributes.new(MatchValidationRule, field)
|
398
|
-
end
|
399
|
-
|
400
|
-
if !is_required?(field_value, attributes)
|
401
|
-
return true
|
402
|
-
end
|
403
|
-
|
404
|
-
match_field_value = get_field_value(obj, match_field)
|
405
|
-
|
406
|
-
return field_value == match_field_value
|
407
|
-
|
408
|
-
end
|
409
|
-
|
410
|
-
end
|
411
|
-
|
412
|
-
class ConditionValidationRule < ValidationRule
|
413
|
-
|
414
|
-
def error_message(field, attributes = {}, parent = nil)
|
415
|
-
|
416
|
-
field_name = field.to_s
|
417
|
-
if parent != nil
|
418
|
-
field_name = "#{parent.to_s}.#{field.to_s}"
|
419
|
-
end
|
420
|
-
|
421
|
-
#check if a custom error message has been specified in the attributes
|
422
|
-
if attributes[:message] == nil
|
423
|
-
#no custom error message has been specified so create the default message.
|
424
|
-
"#{field_name} is not valid"
|
425
|
-
else
|
426
|
-
attributes[:message]
|
427
|
-
end
|
428
|
-
end
|
429
|
-
|
430
|
-
|
431
|
-
def validate(obj, field, attributes, parent = nil)
|
432
|
-
|
433
|
-
#attempt to get the field value from the object
|
434
|
-
value = get_field_value(obj, field)
|
435
|
-
|
436
|
-
condition_field = attributes[:condition_field]
|
437
|
-
if condition_field == nil
|
438
|
-
raise InvalidRuleAttributes.new(ConditionValidationRule, field)
|
439
|
-
end
|
440
|
-
|
441
|
-
condition_value = attributes[:condition_value]
|
442
|
-
if condition_value == nil
|
443
|
-
raise InvalidRuleAttributes.new(ConditionValidationRule, field)
|
444
|
-
end
|
445
|
-
|
446
|
-
condition_expression = attributes[:condition_expression]
|
447
|
-
if condition_expression == nil
|
448
|
-
raise InvalidRuleAttributes.new(ConditionValidationRule, field)
|
449
|
-
end
|
450
|
-
|
451
|
-
field_expression = attributes[:field_expression]
|
452
|
-
if field_expression == nil
|
453
|
-
raise InvalidRuleAttributes.new(ConditionValidationRule, field)
|
454
|
-
end
|
455
|
-
|
456
|
-
field_value = attributes[:field_value]
|
457
|
-
if field_value == nil
|
458
|
-
raise InvalidRuleAttributes.new(ConditionValidationRule, field)
|
459
|
-
end
|
460
|
-
|
461
|
-
if !is_required?(value, attributes)
|
462
|
-
return true
|
463
|
-
end
|
464
|
-
|
465
|
-
condition_field_value = get_field_value(obj, condition_field)
|
466
|
-
|
467
|
-
#check if the condition is valid
|
468
|
-
if confirm_expression?(condition_field_value, condition_expression, condition_value)
|
469
|
-
#check if the field value is correct for the condition
|
470
|
-
if confirm_expression?(value, field_expression, field_value)
|
471
|
-
return true
|
472
|
-
else
|
473
|
-
return false
|
474
|
-
end
|
475
|
-
end
|
476
|
-
|
477
|
-
return true
|
478
|
-
|
479
|
-
end
|
480
|
-
|
481
|
-
def confirm_expression?(value_a, expression, value_b)
|
482
|
-
a = value_a
|
483
|
-
b = value_b
|
484
|
-
|
485
|
-
if value_a.is_a?(String)
|
486
|
-
a = "'#{value_a}'"
|
487
|
-
elsif value_a == nil
|
488
|
-
a = 'nil'
|
489
|
-
end
|
490
|
-
|
491
|
-
if value_b.is_a?(String)
|
492
|
-
b = "'#{value_b}'"
|
493
|
-
elsif value_b == nil
|
494
|
-
b = 'nil'
|
495
|
-
end
|
496
|
-
|
497
|
-
eval("#{a} #{expression} #{b}")
|
498
|
-
end
|
499
|
-
|
500
|
-
end
|
501
|
-
|
502
|
-
class NotAllowedValidationRule < ValidationRule
|
503
|
-
|
504
|
-
def error_message(field, attributes = {}, parent = nil)
|
505
|
-
|
506
|
-
field_name = field.to_s
|
507
|
-
if parent != nil
|
508
|
-
field_name = "#{parent.to_s}.#{field.to_s}"
|
509
|
-
end
|
510
|
-
|
511
|
-
#check if a custom error message has been specified in the attributes
|
512
|
-
if attributes[:message] == nil
|
513
|
-
#no custom error message has been specified so create the default message.
|
514
|
-
"#{field_name} is not allowed."
|
515
|
-
else
|
516
|
-
attributes[:message]
|
517
|
-
end
|
518
|
-
end
|
519
|
-
|
520
|
-
|
521
|
-
def validate(obj, field, attributes = {}, parent = nil)
|
522
|
-
|
523
|
-
#attempt to get the field value from the object
|
524
|
-
field_value = get_field_value(obj, field)
|
525
|
-
|
526
|
-
if field_value != nil
|
527
|
-
return false
|
528
|
-
end
|
529
|
-
|
530
|
-
return true
|
531
|
-
|
532
|
-
end
|
533
|
-
|
534
|
-
end
|
535
|
-
|
536
|
-
class ListValidationRule < ValidationRule
|
537
|
-
|
538
|
-
def error_message(field, attributes = {}, parent = nil)
|
539
|
-
|
540
|
-
field_name = field.to_s
|
541
|
-
if parent != nil
|
542
|
-
field_name = "#{parent.to_s}.#{field.to_s}"
|
543
|
-
end
|
544
|
-
|
545
|
-
#check if a custom error message has been specified in the attributes
|
546
|
-
if attributes[:message] == nil
|
547
|
-
#no custom error message has been specified so create the default message.
|
548
|
-
"#{field_name} is not an accepted value."
|
549
|
-
else
|
550
|
-
attributes[:message]
|
551
|
-
end
|
552
|
-
end
|
553
|
-
|
554
|
-
|
555
|
-
def validate(obj, field, attributes = {}, parent = nil)
|
556
|
-
|
557
|
-
#attempt to get the field value from the object
|
558
|
-
field_value = get_field_value(obj, field)
|
559
|
-
|
560
|
-
if !is_required?(field_value, attributes)
|
561
|
-
return true
|
562
|
-
end
|
563
|
-
|
564
|
-
if field_value == nil
|
565
|
-
return false
|
566
|
-
end
|
567
|
-
|
568
|
-
list = attributes[:list]
|
569
|
-
if list == nil || !list.is_a?(Array)
|
570
|
-
raise InvalidRuleAttributes.new(ListValidationRule, field)
|
571
|
-
end
|
572
|
-
|
573
|
-
if !list.include?(field_value)
|
574
|
-
return false
|
575
|
-
end
|
576
|
-
|
577
|
-
return true
|
578
|
-
|
579
|
-
end
|
580
|
-
|
581
|
-
end
|
582
|
-
|
583
|
-
class ChildValidationRule < ValidationRule
|
584
|
-
|
585
|
-
def initialize
|
586
|
-
@validation_manager = ValidationManager.new
|
587
|
-
end
|
588
|
-
|
589
|
-
def error_message(field, attributes = {}, parent = nil)
|
590
|
-
|
591
|
-
field_name = field.to_s
|
592
|
-
if parent != nil
|
593
|
-
field_name = "#{parent.to_s}.#{field.to_s}"
|
594
|
-
end
|
595
|
-
|
596
|
-
#check if a custom error message has been specified in the attributes
|
597
|
-
if attributes[:message] == nil
|
598
|
-
#no custom error message has been specified so create the default message.
|
599
|
-
"#{field_name} is required."
|
600
|
-
else
|
601
|
-
attributes[:message]
|
602
|
-
end
|
603
|
-
end
|
604
|
-
|
605
|
-
def validate(obj, field, attributes = {}, parent = nil)
|
606
|
-
|
607
|
-
#attempt to get the field value from the object
|
608
|
-
field_value = get_field_value(obj, field)
|
609
|
-
|
610
|
-
if !is_required?(field_value, attributes)
|
611
|
-
return true
|
612
|
-
end
|
613
|
-
|
614
|
-
profile = attributes[:profile]
|
615
|
-
if profile == nil
|
616
|
-
raise InvalidRuleAttributes.new(ChildValidationRule, field)
|
617
|
-
end
|
618
|
-
|
619
|
-
if field_value == nil
|
620
|
-
return false
|
621
|
-
end
|
622
|
-
|
623
|
-
parent_field = field.to_s
|
624
|
-
if parent != nil
|
625
|
-
parent_field = "#{parent.to_s}.#{field.to_s}"
|
626
|
-
end
|
627
|
-
|
628
|
-
return @validation_manager.validate(field_value, profile, parent_field)
|
629
|
-
|
630
|
-
end
|
631
|
-
|
632
|
-
end
|