Sha256: aa81a760b4cda73716a7fa3193fff41bcc7209cd3fe4a2d64b7d0ca0345d1b56

Contents?: true

Size: 870 Bytes

Versions: 18

Compression:

Stored size: 870 Bytes

Contents

require "email_assessor/address"
require "active_model"
require "active_model/validations"

class EmailValidator < ActiveModel::EachValidator
  def default_options
    { regex: true, disposable: false, mx: false }
  end

  def validate_each(record, attribute, value)
    return unless value.present?
    options = default_options.merge(self.options)

    address = EmailAssessor::Address.new(value)

    error(record, attribute) && return unless address.valid?

    if options[:disposable]
      error(record, attribute) && return if address.disposable?
    end

    if options[:blacklist]
      error(record, attribute) && return if address.blacklisted?
    end

    if options[:mx]
      error(record, attribute) && return unless address.valid_mx?
    end
  end

  def error(record, attribute)
    record.errors.add(attribute, options[:message] || :invalid)
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
email_assessor-0.4.4 lib/email_assessor/email_validator.rb
email_assessor-0.4.3 lib/email_assessor/email_validator.rb
email_assessor-0.4.2 lib/email_assessor/email_validator.rb
email_assessor-0.4.1 lib/email_assessor/email_validator.rb
email_assessor-0.4.0 lib/email_assessor/email_validator.rb
email_assessor-0.3.9 lib/email_assessor/email_validator.rb
email_assessor-0.3.8 lib/email_assessor/email_validator.rb
email_assessor-0.3.7 lib/email_assessor/email_validator.rb
email_assessor-0.3.6 lib/email_assessor/email_validator.rb
email_assessor-0.3.5 lib/email_assessor/email_validator.rb
email_assessor-0.3.4 lib/email_assessor/email_validator.rb
email_assessor-0.3.3 lib/email_assessor/email_validator.rb
email_assessor-0.3.2 lib/email_assessor/email_validator.rb
email_assessor-0.3.1 lib/email_assessor/email_validator.rb
email_assessor-0.3 lib/email_assessor/email_validator.rb
email_assessor-0.2.1 lib/email_assessor/email_validator.rb
email_assessor-0.2 lib/email_assessor/email_validator.rb
email_assessor-0.1 lib/email_assessor/email_validator.rb