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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Collecting errors into a list lets you report every problem at once instead of forcing the user through repeated round trips.
- 2Pre-compiling regex patterns as static finals reuses them across calls rather than recompiling on each validation.
- 3Guarding presence checks before format checks avoids redundant or confusing messages for empty fields.
Related explainers
go
package main import ( "errors"
Parsing and validating CLI flags in Go
cli-parsing
validation
error-handling
Intermediate
8 steps
javascript
'use server' import { revalidatePath } from 'next/cache' import { redirect } from 'next/navigation'
How a Next.js Server Action updates a post
server-actions
authorization
validation
Intermediate
7 steps
java
public class ThumbnailProcessor { private static final int MAX_CONCURRENCY = 4;
Bounded parallel thumbnail rendering in Java
concurrency
thread-pool
futures
Intermediate
7 steps
java
public class SortedListMerger { public static int[] merge(int[] a, int[] b) { int[] result = new int[a.length + b.length];
Merging two sorted arrays in Java
two-pointers
merging
arrays
Beginner
6 steps
php
<?php namespace App\Support;
Merging query params onto a URL in PHP
url-parsing
query-strings
immutability
Intermediate
8 steps
java
import java.util.ArrayDeque; import java.util.Deque; public final class RollingAverage {
A rolling average over a sliding window
sliding-window
running-sum
deque
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/accumulating-validation-errors-in-java-explained-java-131a/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.