Class: Incline::EmailValidator

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

Overview

Validates a string to ensure it contains a valid email address.

validates :email_address, 'incline/email' => true

Constant Summary collapse

VALID_EMAIL_REGEX =

This regular expression should validate 99.9% of common email addresses.

There are some weird rules that it doesn’t account for, but they should be rare.

/\A[\w+\-.]+@#{INTERNAL_DOM_REGEX}\z/i
VALID_DOMAIN_REGEX =

This regular expression should validate any domain.

/\A#{INTERNAL_DOM_REGEX}\z/i

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.valid?(email) ⇒ Boolean

Validates that an email address is valid based on format.

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/incline/validators/email_validator.rb', line 37

def self.valid?(email)
  return false if email.blank?
  !!(email =~ VALID_EMAIL_REGEX)
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object

Validates attributes to determine if they contain valid email addresses.

Does not perform an in depth check, but does verify that the format is valid.



29
30
31
32
33
# File 'lib/incline/validators/email_validator.rb', line 29

def validate_each(record, attribute, value)
  unless value.blank?
    record.errors[attribute] << (options[:message] || 'is not a valid email address') unless value =~ VALID_EMAIL_REGEX
  end
end