Skip to content

Commit d847e61

Browse files
committed
Rename allowGmailDots to allowNonstandardDots
1 parent 4f3f061 commit d847e61

File tree

5 files changed

+38
-34
lines changed

5 files changed

+38
-34
lines changed

CHANGELOG.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,19 @@ the `Email` object (similar to the `normalize()` method).
132132
```
133133

134134

135-
### Support for Leading and Trailing Dots in the Local-Part (GMail Allowed)
135+
### Support for Leading and Trailing Dots in the Local-Part
136136

137-
While technically disallowed under published RFCs, GMail considers email addresses that have
138-
local-parts that start with or end with a dot `.` character as valid. For example, GMail
139-
considers `[email protected]` valid, even though it is not actually valid according to RFC.
137+
While technically disallowed under published RFCs, some email providers (ex: GMail)
138+
consider email addresses that have local-parts that start with or end with a dot `.` character
139+
as valid. For example, GMail considers `[email protected]` valid, even though it is not
140+
actually valid according to RFC.
140141

141142
JMail now gives you the option to consider these addresses valid as well. You must use an
142-
`EmailValidator` with the `allowGmailDots` rule added to it to allow these addresses to pass validation.
143+
`EmailValidator` with the `allowNonstandardDots` rule added to it to allow these addresses to pass validation.
143144

144145
```java
145146
EmailValidator validator = JMail.strictValidator()
146-
.allowGmailDots();
147+
.allowNonstandardDots();
147148

148149
validator.isValid("[email protected]"); // returns true
149150
```

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ public final class EmailValidator {
4848
= ValidationRules::requireAscii;
4949

5050
private final Map<Predicate<Email>, FailureReason> validationPredicates;
51-
private final boolean allowGmailDots;
51+
private final boolean allowNonstandardDots;
5252

5353
EmailValidator(Map<Predicate<Email>, FailureReason> validationPredicates,
54-
boolean allowGmailDots) {
54+
boolean allowNonstandardDots) {
5555
this.validationPredicates = Collections.unmodifiableMap(validationPredicates);
56-
this.allowGmailDots = allowGmailDots;
56+
this.allowNonstandardDots = allowNonstandardDots;
5757
}
5858

5959
EmailValidator() {
@@ -80,7 +80,7 @@ public EmailValidator withRules(Map<Predicate<Email>, FailureReason> rules) {
8080
Map<Predicate<Email>, FailureReason> ruleMap = new HashMap<>(validationPredicates);
8181
ruleMap.putAll(rules);
8282

83-
return new EmailValidator(ruleMap, allowGmailDots);
83+
return new EmailValidator(ruleMap, allowNonstandardDots);
8484
}
8585

8686
/**
@@ -170,14 +170,14 @@ public EmailValidator withRule(Predicate<Email> rule, String failureReason) {
170170
* {@code .} character.</p>
171171
*
172172
* <p>While not allowed according to RFC, a leading or trailing dot character in the local-part
173-
* <strong>is allowed</strong> by GMail, hence the naming of this method.</p>
173+
* <strong>is allowed</strong> by some mail providers (such as GMail).</p>
174174
*
175175
* <p>For example, {@code "[email protected]"} would be considered valid if you use the
176176
* {@code EmailValidator} returned by this method.</p>
177177
*
178178
* @return the new {@code EmailValidator} instance
179179
*/
180-
public EmailValidator allowGmailDots() {
180+
public EmailValidator allowNonstandardDots() {
181181
return new EmailValidator(this.validationPredicates, true);
182182
}
183183

@@ -361,7 +361,7 @@ public EmailValidator requireAscii() {
361361
* @return the result of the validation
362362
*/
363363
public boolean isValid(String email) {
364-
return JMail.validate(email, allowGmailDots)
364+
return JMail.validate(email, allowNonstandardDots)
365365
.getEmail()
366366
.filter(e -> !testPredicates(e).isPresent())
367367
.isPresent();
@@ -404,7 +404,7 @@ public void enforceValid(String email) throws InvalidEmailException {
404404
* {@link Email} object if successful, or the {@link FailureReason} if not
405405
*/
406406
public EmailValidationResult validate(String email) {
407-
EmailValidationResult result = JMail.validate(email, allowGmailDots);
407+
EmailValidationResult result = JMail.validate(email, allowNonstandardDots);
408408

409409
// If failed basic validation, just return it
410410
if (!result.getEmail().isPresent()) return result;
@@ -425,7 +425,7 @@ public EmailValidationResult validate(String email) {
425425
* is invalid according to all registered validation rules
426426
*/
427427
public Optional<Email> tryParse(String email) {
428-
return JMail.validate(email, allowGmailDots).getEmail()
428+
return JMail.validate(email, allowNonstandardDots).getEmail()
429429
.filter(e -> !testPredicates(e).isPresent());
430430
}
431431

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

+12-9
Original file line numberDiff line numberDiff line change
@@ -129,24 +129,28 @@ public static EmailValidationResult validate(String email) {
129129
}
130130

131131
/**
132-
* Package-private validate method that exposes an additional option {@code allowGmailDots}.
132+
* Package-private validate method that exposes an additional option {@code allowNonstandardDots}.
133133
*
134134
* @param email the email address to parse and validate
135-
* @param allowGmailDots true if a leading or trailing dot in the local-part should be allowed
135+
* @param allowNonstandardDots true if a leading or trailing dot in the local-part should be
136+
* allowed
136137
* @return a {@link EmailValidationResult} containing success or failure, along with the parsed
137138
* {@link Email} object if successful, or the {@link FailureReason} if not
138139
*/
139-
static EmailValidationResult validate(String email, boolean allowGmailDots) {
140-
return validateInternal(email, allowGmailDots);
140+
static EmailValidationResult validate(String email, boolean allowNonstandardDots) {
141+
return validateInternal(email, allowNonstandardDots);
141142
}
142143

143144
/**
144145
* Internal parsing method.
145146
*
146147
* @param email the email address to parse
148+
* @param allowNonstandardDots true if a leading or trailing dot in the local-part should be
149+
* allowed
147150
* @return a new {@link Email} instance if valid, empty if invalid
148151
*/
149-
private static EmailValidationResult validateInternal(String email, boolean allowGmailDots) {
152+
private static EmailValidationResult validateInternal(String email,
153+
boolean allowNonstandardDots) {
150154
// email cannot be null
151155
if (email == null) return EmailValidationResult.failure(FailureReason.NULL_ADDRESS);
152156

@@ -179,10 +183,9 @@ private static EmailValidationResult validateInternal(String email, boolean allo
179183
// email cannot be more than 320 chars
180184
if (size > 320) return EmailValidationResult.failure(FailureReason.ADDRESS_TOO_LONG);
181185

182-
// email cannot start with '.'
183186
// email cannot start with '.'
184187
// unless we are configured to allow it (GMail doesn't care about a starting dot)
185-
if (email.charAt(0) == '.' && !allowGmailDots) {
188+
if (email.charAt(0) == '.' && !allowNonstandardDots) {
186189
return EmailValidationResult.failure(FailureReason.STARTS_WITH_DOT);
187190
}
188191

@@ -240,7 +243,7 @@ private static EmailValidationResult validateInternal(String email, boolean allo
240243
}
241244

242245
EmailValidationResult innerResult
243-
= validateInternal(email.substring(i + 1, size - 1), allowGmailDots);
246+
= validateInternal(email.substring(i + 1, size - 1), allowNonstandardDots);
244247

245248
// If the address passed validation, return success with the identifier included.
246249
// Otherwise, just return the failed internal result
@@ -530,7 +533,7 @@ private static EmailValidationResult validateInternal(String email, boolean allo
530533
// Check that local-part does not end with '.'
531534
if (localPart.charAt(localPart.length() - 1) == '.') {
532535
// unless we are configured to allow it (GMail doesn't care about a trailing dot)
533-
if (!allowGmailDots) {
536+
if (!allowNonstandardDots) {
534537
return EmailValidationResult.failure(FailureReason.LOCAL_PART_ENDS_WITH_DOT);
535538
}
536539

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -308,38 +308,38 @@ void invalidatesCorrectlyWithMap(String email) {
308308
}
309309

310310
@Nested
311-
class AllowGmailDots {
311+
class AllowNonstandardDots {
312312
@ParameterizedTest(name = "{0}")
313313
@ValueSource(strings = {
314314
315315
void rejectsInvalidDots(String email) {
316-
runInvalidTest(JMail.validator().allowGmailDots(),
316+
runInvalidTest(JMail.validator().allowNonstandardDots(),
317317
email, FailureReason.MULTIPLE_DOT_SEPARATORS);
318318
}
319319

320320
@ParameterizedTest(name = "{0}")
321321
@ValueSource(strings = {
322322
323-
void allowsGmailDots(String email) {
324-
runValidTest(JMail.validator().allowGmailDots(), email);
323+
void allowsNonstandardDots(String email) {
324+
runValidTest(JMail.validator().allowNonstandardDots(), email);
325325
}
326326
}
327327

328328
@Nested
329-
class DisallowIpDomainAllowGmailDotsCombination {
329+
class DisallowIpDomainAllowNonstandardDotsCombination {
330330
@ParameterizedTest(name = "{0}")
331331
@ValueSource(strings = {
332332
"test@[1.2.3.4]", "test@[5.6.7.8]"})
333333
void rejects(String email) {
334-
runInvalidTest(JMail.validator().allowGmailDots().disallowIpDomain(),
334+
runInvalidTest(JMail.validator().allowNonstandardDots().disallowIpDomain(),
335335
email, FailureReason.CONTAINS_IP_DOMAIN);
336336
}
337337

338338
@ParameterizedTest(name = "{0}")
339339
@ValueSource(strings = {
340340
341341
void allows(String email) {
342-
runValidTest(JMail.validator().allowGmailDots().disallowIpDomain(), email);
342+
runValidTest(JMail.validator().allowNonstandardDots().disallowIpDomain(), email);
343343
}
344344
}
345345

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,14 @@ void isInvalidCanValidate() {
211211
}
212212

213213
@Nested
214-
class AllowGmailDots {
214+
class AllowNonstandardDots {
215215
@ParameterizedTest(name = "{0}")
216216
@MethodSource({
217217
"com.sanctionco.jmail.helpers.AdditionalEmailProvider#provideInvalidEmails",
218218
"com.sanctionco.jmail.helpers.AdditionalEmailProvider#provideInvalidWhitespaceEmails",
219219
"com.sanctionco.jmail.helpers.AdditionalEmailProvider#provideInvalidControlEmails"})
220220
@CsvFileSource(resources = "/invalid-addresses.csv", delimiterString = " ;", numLinesToSkip = 1)
221-
void ensureFailuresWhenAllowGmailDotsIsTrue(String email) {
221+
void ensureFailures(String email) {
222222
// This test only works for addresses that will fail
223223
// even when we allow a starting or trailing dot in the local-part
224224
assumeTrue(email.charAt(0) != '.' && !email.contains(".@"));
@@ -228,7 +228,7 @@ void ensureFailuresWhenAllowGmailDotsIsTrue(String email) {
228228
}
229229

230230
@Test
231-
void ensureOnlyDotFailsWhenAllowGmailDotsIsTrue() {
231+
void ensureOnlyDotFails() {
232232
assertThat(JMail.validate("[email protected]", true))
233233
.returns(true, EmailValidationResult::isFailure)
234234
.returns(FailureReason.LOCAL_PART_MISSING, EmailValidationResult::getFailureReason);;

0 commit comments

Comments
 (0)