Class: MaxValidationRule
Instance Method Summary
collapse
#get_field_value, #is_required?
Instance Method Details
#error_message(field, attributes) ⇒ Object
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/validation_profiler/rules/rules.rb', line 184
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
|
# File 'lib/validation_profiler/rules/rules.rb', line 195
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
|