Data Validation in Java: Overview of Libraries and Approaches

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Data Validation in Java: A Complete Guide to Libraries and Tools



Data validation is one of the key aspects of developing reliable Java applications. Without it, it is impossible to guarantee the correctness of input data, protect against incorrect user input, or errors in integration with external systems. In this article, we will review the main libraries and approaches to data validation in Java, starting from the standard Bean Validation to modern solutions.



Why is validation needed?

Validation allows you to:

  • Filter out incorrect data at an early stage
  • Reduce the number of errors in business logic
  • Increase application security (protection against SQL injections, XSS)
  • Improve user experience (clear error messages)


1. Jakarta Bean Validation (formerly JSR 380)

This is the Java standard for validating objects using annotations. The main implementation is Hibernate Validator. Supported starting from Jakarta EE 9 and Java SE 8+.



Main annotations

  • @NotNull — value cannot be null
  • @NotEmpty — string, collection, or array cannot be empty
  • @Size(min, max) — limits string length or collection size
  • @Pattern(regexp) — validation against a regular expression
  • @Email — valid email address
  • @Min / @Max — numeric boundaries


Usage example

import jakarta.validation.constraints.*;

public class User { @NotNull(message = "Name cannot be null") @Size(min = 2, max = 50, message = "Name must be between 2 and 50 characters") private String name;

@Email(message = "Invalid email format") @NotEmpty(message = "Email is required") private String email;

@Min(value = 18, message = "Age must be at least 18") @Max(value = 120, message = "Age cannot exceed 120") private int age;

// getters and setters}


Running validation

import jakarta.validation.*;import java.util.Set;

public class ValidationExample { public static void main(String[] args) { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator();

User user = new User(); user.setName("A"); // name too short user.setEmail("invalid-email"); user.setAge(15); // less than 18

Set<ConstraintViolation<User>> violations = validator.validate(user); for (ConstraintViolation<User> violation : violations) { System.out.println(violation.getMessage()); } }}


2. Hibernate Validator — Extended Implementation

Hibernate Validator is the reference implementation of Bean Validation. It adds additional features:

  • Custom validation annotations
  • Validation groups (for different scenarios)
  • Support for @ScriptAssert for complex checks


Example of a custom annotation

import jakarta.validation.*;import java.lang.annotation.*;

@Target({ElementType.FIELD})@Retention(RetentionPolicy.RUNTIME)@Constraint(validatedBy = PasswordValidator.class)public @interface ValidPassword { String message() default "Password must contain letters and digits"; Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {};}


public class PasswordValidator implements ConstraintValidator<ValidPassword, String> {    @Override    public boolean isValid(String value, ConstraintValidatorContext context) {        if (value == null) return false;        return value.matc                    

Recommendations