Skip to content

Commit 8cf766a

Browse files
committed
Emails that end with dash should return correct failure reason
1 parent b29d6d2 commit 8cf766a

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/main/java/com/sanctionco/jmail/JMail.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,16 @@ private static EmailValidationResult validateInternal(String email) {
170170
// email cannot start with '.'
171171
if (email.charAt(0) == '.') return EmailValidationResult.failure(FailureReason.STARTS_WITH_DOT);
172172

173-
// email cannot end with '.' or '-'
174-
if (email.charAt(size - 1) == '.' || email.charAt(size - 1) == '-') {
173+
// email cannot end with '.'
174+
if (email.charAt(size - 1) == '.') {
175175
return EmailValidationResult.failure(FailureReason.ENDS_WITH_DOT);
176176
}
177177

178+
// email cannot end with '-'
179+
if (email.charAt(size - 1) == '-') {
180+
return EmailValidationResult.failure(FailureReason.DOMAIN_PART_ENDS_WITH_DASH);
181+
}
182+
178183
boolean atFound = false; // set to true when the '@' character is found
179184
boolean inQuotes = false; // set to true if we are currently within quotes
180185
boolean previousDot = false; // set to true if the previous character is '.'

src/test/java/com/sanctionco/jmail/FailureReasonTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static Stream<Arguments> provideTestEmails() {
1717
Arguments.of("te[st@test.com", FailureReason.DISALLOWED_UNQUOTED_CHARACTER),
1818
Arguments.of("test@", FailureReason.DOMAIN_MISSING),
1919
Arguments.of("test@test-.com", FailureReason.DOMAIN_PART_ENDS_WITH_DASH),
20+
Arguments.of("email@example.com-", FailureReason.DOMAIN_PART_ENDS_WITH_DASH),
2021
Arguments.of("test@my.-test.com", FailureReason.DOMAIN_PART_STARTS_WITH_DASH),
2122
Arguments.of(
2223
"first.last@x234567890123456789012345678901234567890123456789012345678901234.test.org",

0 commit comments

Comments
 (0)