Class: ActiveModel::Validations::EmailValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/validators/validates_email_format_of.rb

Constant Summary collapse

AT_SIGN =
"@".freeze

Instance Method Summary collapse

Instance Method Details

#validate_disposable_email(record, attribute, value, options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/validators/validates_email_format_of.rb', line 42

def validate_disposable_email(record, attribute, value, options)
  hostname = value.to_s.split(AT_SIGN).last.to_s.downcase

  return unless Validators::DisposableHostnames.all.include?(hostname)

  record.errors.add(
    attribute,
    :disposable_email,
    value: value
  )
end

#validate_each(record, attribute, value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/validators/validates_email_format_of.rb', line 6

def validate_each(record, attribute, value)
  allow_disposable = options.fetch(:disposable, false)
  check_tld = options.fetch(:tld, false)

  return if value.blank? && options[:allow_blank]
  return if value.nil? && options[:allow_nil]

  validate_tld(record, attribute, value, options) if check_tld
  validate_email_format(record, attribute, value, options)
  validate_disposable_email(record, attribute, value, options) unless allow_disposable
end

#validate_email_format(record, attribute, value, options) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/validators/validates_email_format_of.rb', line 18

def validate_email_format(record, attribute, value, options)
  return if value.to_s =~ Validators::EMAIL_FORMAT
  return if value.to_s =~ Validators::MICROSOFT_EMAIL_FORMAT

  record.errors.add(
    attribute,
    :invalid_email,
    message: options[:message],
    value: value
  )
end

#validate_tld(record, attribute, value, options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/validators/validates_email_format_of.rb', line 30

def validate_tld(record, attribute, value, options)
  host = value.to_s.split(AT_SIGN).last
  return if Validators::TLD.host_with_valid_tld?(host)

  record.errors.add(
    attribute,
    :invalid_hostname,
    message: options[:message],
    value: value
  )
end