Sha256: 8e1d16b5a1c6b8bb534848855463ad09cc48a3d8a01262d5a8f1a9e919255e89

Contents?: true

Size: 866 Bytes

Versions: 37

Compression:

Stored size: 866 Bytes

Contents

require "valid_email2/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 = ValidEmail2::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

37 entries across 37 versions & 1 rubygems

Version Path
valid_email2-1.2.2 lib/valid_email2/email_validator.rb
valid_email2-1.2.1 lib/valid_email2/email_validator.rb
valid_email2-1.2.0 lib/valid_email2/email_validator.rb
valid_email2-1.1.13 lib/valid_email2/email_validator.rb
valid_email2-1.1.12 lib/valid_email2/email_validator.rb
valid_email2-1.1.11 lib/valid_email2/email_validator.rb
valid_email2-1.1.10 lib/valid_email2/email_validator.rb
valid_email2-1.1.9 lib/valid_email2/email_validator.rb
valid_email2-1.1.8 lib/valid_email2/email_validator.rb
valid_email2-1.1.7 lib/valid_email2/email_validator.rb
valid_email2-1.1.6 lib/valid_email2/email_validator.rb
valid_email2-1.1.5 lib/valid_email2/email_validator.rb
valid_email2-1.1.4 lib/valid_email2/email_validator.rb
valid_email2-1.1.3 lib/valid_email2/email_validator.rb
valid_email2-1.1.2 lib/valid_email2/email_validator.rb
valid_email2-1.1.1 lib/valid_email2/email_validator.rb
valid_email2-1.1.0 lib/valid_email2/email_validator.rb