2022-02-18 09:12:51 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class EmailAddressValidator
|
2024-08-27 05:20:37 +08:00
|
|
|
class << self
|
|
|
|
def valid_value?(email)
|
|
|
|
email.match?(email_regex) && decode(email)&.match?(email_regex)
|
|
|
|
end
|
|
|
|
|
|
|
|
def email_regex
|
|
|
|
/\A[a-zA-Z0-9!#\$%&'*+\/=?\^_`{|}~\-]+(?:\.[a-zA-Z0-9!#\$%&'\*+\/=?\^_`{|}~\-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-]*[a-zA-Z0-9])?$\z/
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2022-02-18 09:12:51 +08:00
|
|
|
|
2024-08-27 05:20:37 +08:00
|
|
|
def decode(email)
|
|
|
|
Mail::Address.new(email).decoded
|
|
|
|
rescue Mail::Field::ParseError, Mail::Field::IncompleteParseError
|
|
|
|
nil
|
|
|
|
end
|
2022-02-18 09:12:51 +08:00
|
|
|
end
|
|
|
|
end
|