Class: EmailValidator

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

Instance Method Summary collapse

Instance Method Details

#default_optionsObject



7
8
9
# File 'lib/email_assessor/email_validator.rb', line 7

def default_options
  { regex: true, disposable: false, mx: false, fastpass: true }
end

#validate_each(record, attribute, value) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/email_assessor/email_validator.rb', line 11

def validate_each(record, attribute, value)
  return unless value.present?

  options = default_options.merge!(self.options)

  address = EmailAssessor::Address.new(value)

  error(record, attribute) && return unless address.valid?

  # Skip all domain blocklist checks for fastpass domains.
  # The goal is to skip needless validation for common "good" domains such as Gmail, Yahoo, and Outlook.
  # The fastpass domain list is configurable via vendor/fastpass_domains.txt
  validate_domain(record, attribute, address, options) unless options[:fastpass] && address.fastpass?

  # Exit early if validate_domain found a validation error
  return if record.errors.key?(attribute)

  if options[:mx]
    error(record, attribute, error_type(:mx, options)) && return unless address.valid_mx?
  end
end