java 50 lines · 9 steps

Accumulating validation errors in Java

A registration validator collects every problem into one list instead of failing on the first.

Explained by highlit
1public final class RegistrationValidator {
2 
3 private static final Pattern EMAIL = Pattern.compile("^[\\w.+-]+@[\\w-]+\\.[\\w.-]+$");
4 private static final Pattern USERNAME = Pattern.compile("^[a-zA-Z0-9_]{3,20}$");
5 
6 public List<String> validate(RegistrationForm form) {
7 List<String> errors = new ArrayList<>();
8 
9 if (isBlank(form.username())) {
10 errors.add("Username is required.");
11 } else if (!USERNAME.matcher(form.username()).matches()) {
12 errors.add("Username must be 3-20 letters, digits, or underscores.");
13 }
14 
15 if (isBlank(form.email())) {
16 errors.add("Email is required.");
17 } else if (!EMAIL.matcher(form.email()).matches()) {
18 errors.add("Email address is not valid.");
19 }
20 
21 String password = form.password();
22 if (isBlank(password)) {
23 errors.add("Password is required.");
24 } else {
25 if (password.length() < 8) {
26 errors.add("Password must be at least 8 characters.");
27 }
28 if (password.chars().noneMatch(Character::isDigit)) {
29 errors.add("Password must contain at least one digit.");
30 }
31 if (password.chars().noneMatch(Character::isUpperCase)) {
32 errors.add("Password must contain at least one uppercase letter.");
33 }
34 }
35 
36 if (!Objects.equals(password, form.confirmPassword())) {
37 errors.add("Passwords do not match.");
38 }
39 
40 if (!form.acceptedTerms()) {
41 errors.add("You must accept the terms and conditions.");
42 }
43 
44 return errors;
45 }
46 
47 private static boolean isBlank(String value) {
48 return value == null || value.trim().isEmpty();
49 }
50}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1Collecting errors into a list lets you report every problem at once instead of forcing the user through repeated round trips.
  2. 2Pre-compiling regex patterns as static finals reuses them across calls rather than recompiling on each validation.
  3. 3Guarding presence checks before format checks avoids redundant or confusing messages for empty fields.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Accumulating validation errors in Java — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code