Class: RegexValidationRule

Inherits:
ValidationRule show all
Defined in:
lib/validation_profiler/rules/rules.rb

Instance Method Summary collapse

Methods inherited from ValidationRule

#get_field_value, #is_required?

Instance Method Details

#error_message(field, attributes = {}) ⇒ Object



278
279
280
281
282
283
284
285
286
# File 'lib/validation_profiler/rules/rules.rb', line 278

def error_message(field, attributes = {})
  #check if a custom error message has been specified in the attributes
  if attributes[:message] == nil
    #no custom error message has been specified so create the default message.
    "#{field} is not a valid"
  else
    attributes[:message]
  end
end

#validate(obj, field, attributes) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/validation_profiler/rules/rules.rb', line 289

def validate(obj, field, attributes)

  #attempt to get the field value from the object
  field_value = get_field_value(obj, field)

  regex = attributes[:regex]
  if regex == nil
    raise InvalidRuleAttributes.new(RegexValidationRule, field)
  end

  if !is_required?(field_value, attributes)
    return true
  end

  #validate the value against the regex
  if field_value =~ regex
    return true
  end

  return false

end