class EmailValidator

Public Instance Methods

validate_each(record, attribute, value) click to toggle source
# File lib/email_check/email_validator.rb, line 7
def validate_each(record, attribute, value)

  unless value.present?
    add_error(record, attribute)
    return
  end

  address = EmailCheck::EmailAddress.new(value)

  unless address && address.format_valid?
    add_error(record, attribute)
    return
  end

  if options[:block_special_usernames] && address.blocked_username?
    add_error(record, attribute)
    return
  end

  return if address.whitelisted_domain?

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

  if options[:not_blacklisted] && address.blacklisted_domain?
    add_error(record, attribute)
    return
  end

  if options[:not_free] && address.free_email_provider?
    add_error(record, attribute)
    return
  end

  # TODO: Add a callback to bypass this if the domain is already known
  if options[:check_mx] && address.domain_has_mx? == false
    add_error(record, attribute)
    return
  end
end

Private Instance Methods

add_error(record, attribute) click to toggle source
# File lib/email_check/email_validator.rb, line 51
def add_error(record, attribute)
  record.errors.add(attribute, options[:message] || :invalid)
end