Class: EmailTypoValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/email_typo_validator.rb

Constant Summary collapse

COMMON_FORMAT =
{
  '\.\.' => '.',
  '\.con\z' => '.com',
  '\.coj\z' => '.com',
  '\.vom\z' => '.com',
  '\.xom\z' => '.com',
  '\.com[a-z]\z' => '.com',
  '\.con\.' => '.com.',
  '\.orf\z' => '.org',
  '\.orf\.' => '.org.',
  '@hitmail\.' => '@hotmail.',
  '@htomail\.' => '@hotmail.',
  '@hotnail\.' => '@hotmail.',
  '@hotmil\.' => '@hotmail.',
  '@hotmal\.' => '@hotmail.',
  '@hoymail\.' => '@hotmail.',
  '@hormail\.' => '@hotmail.',
  '@hotmsil\.' => '@hotmail.',
  '@gail\.' => '@gmail.',
  '@gnail\.' => '@gmail.',
  '@gmil\.' => '@gmail.',
  '@gmal\.' => '@gmail.',
  '@gmai\.' => '@gmail.',
  '@yahho\.' => '@yahoo.',
  '@yaho\.' => '@yahoo.',
}

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'app/validators/email_typo_validator.rb', line 29

def validate_each(record, attribute, value)
  COMMON_FORMAT.keys.each do |format_key|
    regex = Regexp.new(format_key)
    if match = regex.match(value)
      record.errors.add(attribute, "Invalid, replace #{match[0]} with #{COMMON_FORMAT[format_key]}")
      break
    end
  end
end