Skip to content

Commit 8370ef6

Browse files
committed
Add a withRule(Predicate, String) method to EmailValidator
1 parent c78d217 commit 8370ef6

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

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

+26-4
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,12 @@ public final class EmailValidator {
6565
*
6666
* <pre>
6767
* validator.withRules(Map.of(
68-
* email -> email.domain().startsWith("test"), new FailureReason("MUST_START_WITH_TEST"),
68+
* email -> email.domain().startsWith("test"), new FailureReason("MISSING_TEST_PREFIX"),
6969
* email -> email.localPart.contains("hello"), new FailureReason("MUST_CONTAIN_HELLO"));
7070
* </pre>
7171
*
72-
* @param rules a collection of requirements that make a valid email address
72+
* @param rules a map of requirements that make a valid email address and each requirement's
73+
* {@link FailureReason}
7374
* @return the new {@code EmailValidator} instance
7475
*/
7576
public EmailValidator withRules(Map<Predicate<Email>, FailureReason> rules) {
@@ -126,19 +127,40 @@ public EmailValidator withRule(Predicate<Email> rule) {
126127
* <pre>
127128
* validator.withRule(
128129
* email -> email.domain().startsWith("test"),
129-
* new FailureReason("DOES_NOT_START_WITH_TEST"));
130+
* new FailureReason("MISSING_TEST_PREFIX"));
130131
* </pre>
131132
*
132133
* @param rule the requirement for a valid email address. This must be a {@link Predicate} that
133134
* accepts an {@link Email} object.
134135
* @param failureReason the {@link FailureReason} to return in the {@link EmailValidationResult}
135-
* if an email fails to pass this rule.
136+
* if an email address fails to pass this rule.
136137
* @return the new {@code EmailValidator} instance
137138
*/
138139
public EmailValidator withRule(Predicate<Email> rule, FailureReason failureReason) {
139140
return withRules(Collections.singletonMap(rule, failureReason));
140141
}
141142

143+
/**
144+
* Create a new {@code EmailValidator} with all rules from the current instance and an
145+
* additional provided custom validation rule.
146+
*
147+
* <p>Example usage:
148+
*
149+
* <pre>
150+
* validator.withRule(
151+
* email -> email.domain().startsWith("test"), "MISSING_TEST_PREFIX");
152+
* </pre>
153+
*
154+
* @param rule the requirement for a valid email address. This must be a {@link Predicate} that
155+
* accepts an {@link Email} object.
156+
* @param failureReason the reason to return in the {@link EmailValidationResult} as the
157+
* {@link FailureReason }if an email address fails to pass this rule.
158+
* @return the new {@code EmailValidator} instance
159+
*/
160+
public EmailValidator withRule(Predicate<Email> rule, String failureReason) {
161+
return withRules(Collections.singletonMap(rule, new FailureReason(failureReason)));
162+
}
163+
142164
/**
143165
* <p>Create a new {@code EmailValidator} with all rules from the current instance and the
144166
* {@link ValidationRules#disallowIpDomain(Email)} rule.

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

+11
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,17 @@ void invalidatesCorrectlyWithCustomFailureReason(String email) {
267267
failureReason);
268268
}
269269

270+
@ParameterizedTest(name = "{0}")
271+
@ValueSource(strings = {"[email protected]", "[email protected]"})
272+
void invalidatesCorrectlyWithCustomStringFailureReason(String email) {
273+
FailureReason failureReason = new FailureReason("MY_REASON");
274+
275+
runInvalidTest(
276+
JMail.validator().withRule(e -> e.domain().startsWith("test"), "MY_REASON"),
277+
email,
278+
failureReason);
279+
}
280+
270281
@ParameterizedTest(name = "{0}")
271282
@ValueSource(strings = {"[email protected]", "[email protected]"})
272283
void validatesCorrectlyWithCollection(String email) {

0 commit comments

Comments
 (0)