Class: EmailValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/email_validator.rb

Constant Summary collapse

Pattern =
Regexp.new('\A' + email_address_regexp + '\Z', Regexp::EXTENDED | Regexp::IGNORECASE, 'n').freeze
Scanner =
Regexp.new(       email_address_regexp,        Regexp::EXTENDED | Regexp::IGNORECASE, 'n').freeze
Separator =

for multiple e-mail addresses

/[;,\s]\s*/.freeze
Defaults =
{
  :message          => I18n.t(:invalid_email_address,    :scope => [:activerecord, :errors, :messages], :default => 'does not appear to be a valid e-mail address'),
  :multiple_message => I18n.t(:invalid_multiple_email,   :scope => [:activerecord, :errors, :messages], :default => 'appears to contain an invalid e-mail address'),
  :mx_message       => I18n.t(:unroutable_email_address, :scope => [:activerecord, :errors, :messages], :default => 'is not routable'),
  :check_mx         => false,
  :with             => Pattern,
  :local_length     => 64,
  :domain_length    => 255
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.errors_on(email, options) ⇒ Object



68
69
70
71
# File 'lib/email_validator.rb', line 68

def errors_on(email, options)
  options = Defaults.merge(options)
  options[:multiple] ? validate_many(email, options) : validate_one(email, options)
end

.extract(string) ⇒ Object



57
58
59
60
61
62
# File 'lib/email_validator.rb', line 57

def extract(string)
  if string.respond_to?(:encode)
    string = string.encode('ascii', :undef => :replace)
  end
  string.scan(Scanner)
end

.valid?(email, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/email_validator.rb', line 64

def valid?(email, options = {})
  errors_on(email, options).nil?
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object

Validates whether the specified value is a valid email address, and uses record.errors.add() to add the error if the provided value is not valid.

Configuration options:

  • message - A custom error message

    (default: "does not appear to be a valid e-mail address")
    
  • check_mx - Check for MX records

    (default: false)
    
  • mx_message - A custom error message when an MX record validation fails

    (default: "is not routable.")
    
  • with - The regex to use for validating the format of the email address

    (default: +Pattern+)</tt>
    
  • multiple - Allow multiple e-mail addresses, separated by Separator

    (default: false)
    
  • multiple_message - A custom error message shown when there are 2 or more addresses

    to validate and one or more is invalid
    (default: "appears to contain an invalid e-mail address)
    
  • local_length - Maximum number of characters allowed in the local part

    (default: 64)
    
  • domain_length - Maximum number of characters allowed in the domain part

    (default: 255)
    


50
51
52
53
54
# File 'lib/email_validator.rb', line 50

def validate_each(record, attribute, value)
  return if value.blank? # Use :presence => true
  error = self.class.errors_on(value, self.options)
  record.errors.add(attribute, :invalid, :message => error, :value => value) if error
end