Sha256: c26ece08ac4b60e3570942e8732a728a9190c13f53e835f6a8dab98d585e3752

Contents?: true

Size: 733 Bytes

Versions: 1

Compression:

Stored size: 733 Bytes

Contents

# Allows to check if the value of a specific 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
  def self.validate_format(email)
    !!(email =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i)
  end

  def self.validate_domain(email, domains)
    email_downcased = email.to_s.downcase
    domains.empty? || domains.any? { |domain| email_downcased.end_with?(".#{domain.downcase}") }
  end

  private

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
missing_validators-1.1.0 lib/missing_validators/validators/email_validator.rb