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
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
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.