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
typescript
import { NestFactory } from '@nestjs/core'; import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { ValidationPipe } from '@nestjs/common'; import { ApiProperty } from '@nestjs/swagger';
Wiring validation and Swagger docs in NestJS
validation
openapi
decorators
Intermediate
8 steps
java
public interface GitHubClient { @GetExchange("/users/{username}") GitHubUser getUser(@PathVariable String username);
Declarative HTTP clients in Spring
http-client
declarative-api
proxy
Intermediate
8 steps
ruby
class TemplateInterpolator PLACEHOLDER = /\{\{\s*([\w.]+)\s*\}\}/ def initialize(strict: false)
Interpolating templates with dotted keys in Ruby
regex
string-interpolation
hash-traversal
Intermediate
6 steps
java
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
Building a trie for autocomplete in Java
trie
prefix-tree
recursion
Intermediate
8 steps
java
@RestController @RequestMapping("/api/products") public class ProductSearchController {
Binding collection query params in Spring
rest-api
query-parameters
dependency-injection
Intermediate
6 steps
typescript
import { parsePhoneNumberFromString, CountryCode } from 'libphonenumber-js'; export interface NormalizedPhone { e164: string;
Normalizing phone numbers to E.164 in TypeScript
validation
normalization
error-handling
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.