Sha256: b13e4066d8e28dbb6b495b5084c59737556fa733c210753589db2113a2fb125f

Contents?: true

Size: 848 Bytes

Versions: 4

Compression:

Stored size: 848 Bytes

Contents

# Checks if the value of an attribute is a valid email address.
#
# @example Validate that the user email is a valid email address.
#   class User << ActiveRecord::Base
#     attr_accessor :email, :name
#     validates :email, email: true
#   end
class EmailValidator < BaseValidator
  private

  EMAIL_FORMAT = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i

  def valid?(email, options)
    validate_format(email) && \
      validate_domain(email, *options[:domain])
  end

  def validate_format(email)
    (email =~ EMAIL_FORMAT).present?
  end

  def validate_domain(email, *domains)
    return true if domains.blank?

    email_downcased = email.to_s.downcase

    domains.any? do |domain|
      domain = domain.to_s.downcase
      domain = "@#{domain}" unless domain.start_with?('.')

      email_downcased.end_with?(domain)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
missing_validators-2.3.0 lib/missing_validators/validators/email_validator.rb
missing_validators-2.2.0 lib/missing_validators/validators/email_validator.rb
missing_validators-2.1.0 lib/missing_validators/validators/email_validator.rb
missing_validators-2.0.0 lib/missing_validators/validators/email_validator.rb