Sha256: 18acedaf5fd6a8873b99b2cc9f0c45a1a46669e21364ef55135aebc370192785

Contents?: true

Size: 648 Bytes

Versions: 11

Compression:

Stored size: 648 Bytes

Contents

class EmailValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    unless valid?(value, options)
      record.errors[attribute] << (options[:message] || I18n.t('flash_validators.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

11 entries across 11 versions & 1 rubygems

Version Path
flash_validators-3.0.12 lib/flash_validators/validators/email_validator.rb
flash_validators-3.0.11 lib/flash_validators/validators/email_validator.rb
flash_validators-3.0.10 lib/flash_validators/validators/email_validator.rb
flash_validators-3.0.9 lib/flash_validators/validators/email_validator.rb
flash_validators-3.0.8 lib/flash_validators/validators/email_validator.rb
flash_validators-3.0.7 lib/flash_validators/validators/email_validator.rb
flash_validators-3.0.6 lib/flash_validators/validators/email_validator.rb
flash_validators-3.0.5 lib/flash_validators/validators/email_validator.rb
flash_validators-3.0.4 lib/flash_validators/validators/email_validator.rb
flash_validators-3.0.3 lib/flash_validators/validators/email_validator.rb
flash_validators-3.0.2 lib/flash_validators/validators/email_validator.rb