@@ -65,11 +65,12 @@ public final class EmailValidator {
65
65
*
66
66
* <pre>
67
67
* 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 "),
69
69
* email -> email.localPart.contains("hello"), new FailureReason("MUST_CONTAIN_HELLO"));
70
70
* </pre>
71
71
*
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}
73
74
* @return the new {@code EmailValidator} instance
74
75
*/
75
76
public EmailValidator withRules (Map <Predicate <Email >, FailureReason > rules ) {
@@ -126,19 +127,40 @@ public EmailValidator withRule(Predicate<Email> rule) {
126
127
* <pre>
127
128
* validator.withRule(
128
129
* email -> email.domain().startsWith("test"),
129
- * new FailureReason("DOES_NOT_START_WITH_TEST "));
130
+ * new FailureReason("MISSING_TEST_PREFIX "));
130
131
* </pre>
131
132
*
132
133
* @param rule the requirement for a valid email address. This must be a {@link Predicate} that
133
134
* accepts an {@link Email} object.
134
135
* @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.
136
137
* @return the new {@code EmailValidator} instance
137
138
*/
138
139
public EmailValidator withRule (Predicate <Email > rule , FailureReason failureReason ) {
139
140
return withRules (Collections .singletonMap (rule , failureReason ));
140
141
}
141
142
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
+
142
164
/**
143
165
* <p>Create a new {@code EmailValidator} with all rules from the current instance and the
144
166
* {@link ValidationRules#disallowIpDomain(Email)} rule.
0 commit comments