Sha256: f10dcc6b0579a4a4b1f1cc70f60663e684a8b3543e5b6a971cf214c3394dc25b

Contents?: true

Size: 718 Bytes

Versions: 4

Compression:

Stored size: 718 Bytes

Contents

class EmailValidator < ActiveModel::EachValidator

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

  private

  def valid_domain?(value, options)
    options.empty? || options.any? { |d| value.downcase.end_with?(".#{d.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.fetch(:domain, nil))])
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_validation-2.6.0 lib/active_validation/validators/email_validator.rb
active_validation-2.5.0 lib/active_validation/validators/email_validator.rb
active_validation-2.4.0 lib/active_validation/validators/email_validator.rb
active_validation-2.3.0 lib/active_validation/validators/email_validator.rb