Class: ActiveModel::Validations::NondisposableValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/nondisposable/email_validator.rb

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/nondisposable/email_validator.rb', line 6

def validate_each(record, attribute, value)
  return if value.blank?

  begin
    domain = value.to_s.split('@').last&.downcase
    return if domain.nil? # Invalid email format

    if Nondisposable::DisposableDomain.disposable?(domain)
      record.errors.add(attribute, options[:message] || Nondisposable.configuration.error_message)
    end
  rescue StandardError => e
    if Nondisposable.configuration.on_check_failure == :reject
      Rails.logger.error "[nondisposable] Nondisposable validation error: #{e.message} — rejecting #{attribute} (on_check_failure = :reject)"
      record.errors.add(attribute, "is an invalid email address, cannot check if it's disposable")
    else
      Rails.logger.error "[nondisposable] Nondisposable validation error: #{e.message} — ALLOWING #{attribute} through without a disposable check (on_check_failure = :allow). Set config.on_check_failure = :reject to fail closed instead."
    end
  end
end