Sha256: e95442d9932ee323da1c4f495763e526064ab83776e501ea8a6f5ff705e46ca8

Contents?: true

Size: 900 Bytes

Versions: 13

Compression:

Stored size: 900 Bytes

Contents

# frozen_string_literal: true
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

13 entries across 13 versions & 1 rubygems

Version Path
email_assessor-0.7.1 lib/email_assessor/email_validator.rb
email_assessor-0.7.0 lib/email_assessor/email_validator.rb
email_assessor-0.6.0 lib/email_assessor/email_validator.rb
email_assessor-0.5.0 lib/email_assessor/email_validator.rb
email_assessor-0.4.13 lib/email_assessor/email_validator.rb
email_assessor-0.4.12 lib/email_assessor/email_validator.rb
email_assessor-0.4.11 lib/email_assessor/email_validator.rb
email_assessor-0.4.10 lib/email_assessor/email_validator.rb
email_assessor-0.4.9 lib/email_assessor/email_validator.rb
email_assessor-0.4.8 lib/email_assessor/email_validator.rb
email_assessor-0.4.7 lib/email_assessor/email_validator.rb
email_assessor-0.4.6 lib/email_assessor/email_validator.rb
email_assessor-0.4.5 lib/email_assessor/email_validator.rb