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
# 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
    Rails.logger.error "Nondisposable validation error: #{e.message}"
    record.errors.add(attribute, "is an invalid email address, cannot check if it's disposable")
  end
end