Class: EmailValidator

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

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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