Sha256: cfced8fb4861d3ca3b29a9c9fdd720249fc12b28f1952ca80b220370bef99aa0

Contents?: true

Size: 715 Bytes

Versions: 1

Compression:

Stored size: 715 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? { |dom| value.downcase.end_with?(".#{dom.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)
    options = [*(options[:domain])]

    valid_length?(value) && valid_format?(value) && valid_domain?(value, options)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_validation-4.0.0 lib/active_validation/validators/email_validator.rb