Sha256: 9f9d51b553de334c22952563f7cf1d3e59ca444bd154d3714ed6beb260d81647

Contents?: true

Size: 684 Bytes

Versions: 3

Compression:

Stored size: 684 Bytes

Contents

class EmailValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    unless valid?(value, options)
      record.errors[attribute] << (options[:message] || I18n.t('errors.messages.email'))
    end
  end

  private

  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

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

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
flash_validators-1.1.0 lib/flash_validators/validators/email_validator.rb
flash_validators-1.0.0 lib/flash_validators/validators/email_validator.rb
flash_validators-0.0.1 lib/flash_validators/validators/email_validator.rb