Class: EmailFormatValidator

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

Overview

Verify the email is a valid email format based on RFC8222

Constant Summary collapse

EMAIL_ADDRESS_REGEX =
/#{addr_list}/

Instance Method Summary collapse

Instance Method Details

#valid_email?(email, strict = false) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
65
# File 'lib/app/validators/email_format_validator.rb', line 59

def valid_email?(email, strict = false)
  if strict
    !!(email =~ EMAIL_ADDRESS_REGEX)
  else
    !!(email =~ /^[\S&&[^@]]+@[\S&&[^@]]+$/)
  end
end

#validate_each(record, attribute, value) ⇒ Object



54
55
56
57
# File 'lib/app/validators/email_format_validator.rb', line 54

def validate_each(record, attribute, value)
  return if valid_email?(value, options[:strict])
  record.errors.add(attribute, message: options[:message] || 'is invalid')
end