Class: MaxValidationRule
Instance Method Summary
collapse
#get_field_value, #is_required?
Instance Method Details
#error_message(field, attributes) ⇒ Object
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/validation_profiler/rules/rules.rb', line 175
def error_message(field, attributes)
if attributes[:message] == nil
max = attributes[:value]
"#{field} must not have a value greater than #{max}"
else
attributes[:message]
end
end
|
#validate(obj, field, attributes) ⇒ Object
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
# File 'lib/validation_profiler/rules/rules.rb', line 186
def validate(obj, field, attributes)
max = attributes[:value]
if max == nil
raise InvalidRuleAttributes.new(MinValidationRule, field)
end
field_value = get_field_value(obj, field)
if !is_required?(field_value, attributes)
return true
end
if field_value.is_a?(DateTime) || field_value.is_a?(Numeric)
field_value <= max
else
raise InvalidFieldType.new(MaxValidationRule, field)
end
end
|