Update address validator to conform to RFC 5321

This commit is contained in:
M Madison Phillips 2018-08-09 11:12:20 -05:00 committed by cketti
parent 95f6079a31
commit 262e0840ba
2 changed files with 11 additions and 1 deletions

View file

@ -16,15 +16,18 @@ class EmailAddressValidator : Validator {
companion object {
//https://www.rfc-editor.org/rfc/rfc2396.txt (3.2.2)
//https://www.rfc-editor.org/rfc/rfc5321.txt (4.1.2)
private const val ALPHA = "[a-zA-Z]"
private const val ALPHANUM = "[a-zA-Z0-9]"
private const val ATEXT = "[0-9a-zA-Z!#$%&'*+\\-/=?^_`{|}~]"
private const val QCONTENT = "([\\p{Graph}\\p{Blank}&&[^\"\\\\]]|\\\\[\\p{Graph}\\p{Blank}])"
private const val TOP_LABEL = "(($ALPHA($ALPHANUM|\\-|_)*$ALPHANUM)|$ALPHA)"
private const val DOMAIN_LABEL = "(($ALPHANUM($ALPHANUM|\\-|_)*$ALPHANUM)|$ALPHANUM)"
private const val HOST_NAME = "((($DOMAIN_LABEL\\.)+$TOP_LABEL)|$DOMAIN_LABEL)"
private val EMAIL_ADDRESS_PATTERN = Pattern.compile(
"[a-zA-Z0-9\\+\\.\\_\\%\\-]{1,256}" +
"^($ATEXT+(\\.$ATEXT+)*|\"$QCONTENT+\")" +
"\\@$HOST_NAME"
)
}

View file

@ -20,10 +20,17 @@ class EmailAddressValidatorTest {
Assert.assertTrue(validator.isValidAddressOnly("example@1.com"))
Assert.assertTrue(validator.isValidAddressOnly("admin@mailserver1"))
Assert.assertTrue(validator.isValidAddressOnly("user@localserver"))
Assert.assertTrue(validator.isValidAddressOnly("\"very.(),:;<>[]\\\".VERY.\\\"very@\\\\ \\\"very\\\".unusual\"@strange.example.com"))
Assert.assertTrue(validator.isValidAddressOnly("\"()<>[]:,;@\\\\\\\"!#$%&'-/=?^_`{}| ~.a\"@example.org"))
Assert.assertTrue(validator.isValidAddressOnly("\" \"@example.org"))
Assert.assertTrue(validator.isValidAddressOnly("x@example.com"))
Assert.assertFalse(validator.isValidAddressOnly("Abc.example.com"))
Assert.assertFalse(validator.isValidAddressOnly("\"not\"right@example.com"))
Assert.assertFalse(validator.isValidAddressOnly("john.doe@example..com"))
Assert.assertFalse(validator.isValidAddressOnly("example@c.2"))
Assert.assertFalse(validator.isValidAddressOnly("this\\ still\\\"not\\\\allowed@example.com"))
Assert.assertFalse(validator.isValidAddressOnly("john..doe@example.com"))
Assert.assertFalse(validator.isValidAddressOnly("invalidperiod.@example.com"))
}
}