forked from mastodon/mastodon
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve email address validation (mastodon#29838)
- Loading branch information
1 parent
53fc1bf
commit 5910fe3
Showing
5 changed files
with
32 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
# NOTE: I initially wrote this as `EmailValidator` but it ended up clashing | ||
# with an indirect dependency of ours, `validate_email`, which, turns out, | ||
# has the same approach as we do, but with an extra check disallowing | ||
# single-label domains. Decided to not switch to `validate_email` because | ||
# we do want to allow at least `localhost`. | ||
|
||
class EmailAddressValidator < ActiveModel::EachValidator | ||
def validate_each(record, attribute, value) | ||
value = value.strip | ||
|
||
address = Mail::Address.new(value) | ||
record.errors.add(attribute, :invalid) if address.address != value | ||
rescue Mail::Field::FieldError | ||
record.errors.add(attribute, :invalid) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters