Class: ValidEmail2::EmailValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Includes:
DeprecationHelper
Defined in:
lib/valid_email2/email_validator.rb

Instance Method Summary collapse

Methods included from DeprecationHelper

#deprecate_method, #deprecation_message

Instance Method Details

#default_optionsObject



10
11
12
# File 'lib/valid_email2/email_validator.rb', line 10

def default_options
  { disposable: false, mx: false, strict_mx: false, disallow_subaddressing: false, multiple: false, dns_timeout: 5, dns_nameserver: nil }
end

#error(record, attribute) ⇒ Object



83
84
85
86
87
# File 'lib/valid_email2/email_validator.rb', line 83

def error(record, attribute)
  message = options[:message].respond_to?(:call) ? options[:message].call : options[:message]

  record.errors.add(attribute, message || :invalid)
end

#sanitized_values(input) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/valid_email2/email_validator.rb', line 71

def sanitized_values(input)
  options = default_options.merge(self.options)

  if options[:multiple]
    email_list = input.is_a?(Array) ? input : input.split(',')
  else
    email_list = [input]
  end

  email_list.reject(&:empty?).map(&:strip)
end

#validate_each(record, attribute, value) ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/valid_email2/email_validator.rb', line 14

def validate_each(record, attribute, value)
  return unless value.present?
  options = default_options.merge(self.options)

  addresses = sanitized_values(value).map { |v| ValidEmail2::Address.new(v, options[:dns_timeout], options[:dns_nameserver]) }

  error(record, attribute) && return unless addresses.all?(&:valid?)

  if options[:disallow_dotted]
    error(record, attribute) && return if addresses.any?(&:dotted?)
  end

  if options[:disallow_subaddressing]
    error(record, attribute) && return if addresses.any?(&:subaddressed?)
  end

  if options[:disposable]
    error(record, attribute) && return if addresses.any?(&:disposable?)
  end

  if options[:disposable_domain]
    error(record, attribute) && return if addresses.any?(&:disposable_domain?)
  end

  if options[:disposable_with_whitelist]
    deprecation_message(:disposable_with_whitelist, :disposable_with_allow_list)
  end

  if options[:disposable_with_allow_list] || options[:disposable_with_whitelist]
    error(record, attribute) && return if addresses.any? { |address| address.disposable? && !address.allow_listed? }
  end

  if options[:disposable_domain_with_whitelist]
    deprecation_message(:disposable_domain_with_whitelist, :disposable_domain_with_allow_list)
  end

  if options[:disposable_domain_with_allow_list] || options[:disposable_domain_with_whitelist]
    error(record, attribute) && return if addresses.any? { |address| address.disposable_domain? && !address.allow_listed? }
  end

  if options[:blacklist]
    deprecation_message(:blacklist, :deny_list)
  end
  
  if options[:deny_list] || options[:blacklist]
    error(record, attribute) && return if addresses.any?(&:deny_listed?)
  end

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

  if options[:strict_mx]
    error(record, attribute) && return unless addresses.all?(&:valid_strict_mx?)
  end
end