Sha256: c3f30d45e19863dd4993d179d20f6a94bc6cb68a7eae38c9447a3b6703936c27

Contents?: true

Size: 631 Bytes

Versions: 2

Compression:

Stored size: 631 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 valid_format?(value)
    value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
  end

  def valid_domain?(value, options)
    value_downcased = value.downcase
    options.empty? || options.any? { |domain| value_downcased.end_with?(".#{domain.downcase}") }
  end

  def valid?(email, options)
    valid_format?(email) &&
    valid_domain?(email, [*(options[:domain])])
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flash_validators-3.0.1 lib/flash_validators/validators/email_validator.rb
flash_validators-3.0.0 lib/flash_validators/validators/email_validator.rb