Sha256: f604a6a263230788c809543f2f849b045cb8e01ecc5a457ccb074f6e68ce463d

Contents?: true

Size: 765 Bytes

Versions: 1

Compression:

Stored size: 765 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[: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

1 entries across 1 versions & 1 rubygems

Version Path
valid_email2-1.0.0 lib/valid_email2/email_validator.rb