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



286
287
288
289
290
291
292
293
294
# File 'lib/validation_profiler/rules/rules.rb', line 286

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



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/validation_profiler/rules/rules.rb', line 297

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