Class: ValidEmail2::EmailValidator

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

Instance Method Summary collapse

Instance Method Details

#default_optionsObject



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

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

#error(record, attribute) ⇒ Object



48
49
50
# File 'lib/valid_email2/email_validator.rb', line 48

def error(record, attribute)
  record.errors.add(attribute, options[:message] || :invalid)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/valid_email2/email_validator.rb', line 11

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

  address = ValidEmail2::Address.new(value)

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

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

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

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

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

  if options[:disposable_with_whitelist]
    error(record, attribute) && return if address.disposable? && !address.whitelisted?
  end

  if options[:blacklist]
    error(record, attribute) && return if address.blacklisted?
  end

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