Sha256: 6a10f77c9367cf429a16a8a669a0c6908f0e5a266bc8fe43bb0978816d2cc31d

Contents?: true

Size: 697 Bytes

Versions: 5

Compression:

Stored size: 697 Bytes

Contents

class EmailValidator < ActiveModel::EachValidator

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

  private

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

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

  def valid_length?(value)
    value.present?
  end

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

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
active_validation-2.2.2 lib/active_validation/validators/email_validator.rb
active_validation-2.2.1 lib/active_validation/validators/email_validator.rb
active_validation-2.2.0 lib/active_validation/validators/email_validator.rb
active_validation-2.0.1 lib/active_validation/validators/email_validator.rb
active_validation-2.0.0 lib/active_validation/validators/email_validator.rb