tweak email validation (no functional change)

pull/8154/head
Niklas Fiekas 2021-02-19 17:40:25 +01:00
parent 280cfbb6ff
commit 297af4374f
3 changed files with 21 additions and 11 deletions

View File

@ -84,18 +84,11 @@ object EmailAddress {
private val gmailLikeNormalizedDomains =
Set("gmail.com", "googlemail.com", "protonmail.com", "protonmail.ch", "pm.me")
private def hasDotAt(str: String) = str contains ".@" // mailgun will reject it
private def hasConsecutiveDots(str: String) = str contains ".." // mailgun will reject it
private def startsWithDot(str: String) = str startsWith "." // mailgun will reject it
def matches(str: String): Boolean =
regex.find(str) &&
!hasDotAt(str) &&
!hasConsecutiveDots(str) &&
!startsWithDot(str)
def isValid(str: String) =
regex.matches(str) && !str.contains("..") && !str.contains(".@") && !str.startsWith(".")
def from(str: String): Option[EmailAddress] =
matches(str) option EmailAddress(str)
isValid(str) option EmailAddress(str)
private def isNoReply(str: String) = str.startsWith("noreply.") && str.endsWith("@lichess.org")
}

View File

@ -16,4 +16,21 @@ class EmailTest extends Specification {
}
}
"from" should {
"accept valid addresses" in {
EmailAddress.from("Hello.World+suffix1+suffix2@gmail.com") must beSome
EmailAddress.from("kebab-case@example.com") must beSome
EmailAddress.from("snake_case@example.com") must beSome
EmailAddress.from("_leading.underscore@example.com") must beSome
EmailAddress.from("trailing.dash-@example.com") must beSome
}
"reject invalid addresses" in {
EmailAddress.from(".leading.dot@example.com") must beNone
EmailAddress.from("trailing.dot.@example.com") must beNone
EmailAddress.from("underscore.in@domain_name.com") must beNone
EmailAddress.from("consecutive..dots@example.com") must beNone
EmailAddress.from("invalid<character@example.com") must beNone
}
}
}

View File

@ -190,7 +190,7 @@ final class Signup(
} {
if (
err.errors.exists(_.messages.contains("error.email_acceptable")) &&
err("email").value.exists(EmailAddress.matches)
err("email").value.exists(EmailAddress.isValid)
)
authLog(username, email, s"Signup with unacceptable email")
}