mirror of
https://github.com/discourse/discourse.git
synced 2024-12-24 18:03:54 +08:00
22 lines
565 B
Ruby
22 lines
565 B
Ruby
# frozen_string_literal: true
|
|
|
|
class EmailAddressValidator
|
|
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
|
|
|
|
def decode(email)
|
|
Mail::Address.new(email).decoded
|
|
rescue Mail::Field::ParseError, Mail::Field::IncompleteParseError
|
|
nil
|
|
end
|
|
end
|
|
end
|