Bean Validation (Jakarta Validation)
Bean Validation uses Java annotations and validators to declare constraints on application data. It separates validation rules from much of the surrounding business logic and can report multiple constraint violations together.
Bean Validation lets you place constraints such as `@NotNull`, `@Size`, and `@Email` on fields, methods, parameters, or other supported program elements. A validation provider evaluates those constraints when the application explicitly requests validation or when a framework integrates validation into its request-processing lifecycle.
A typical Java backend defines a DTO with constraint annotations and validates an instance using a `Validator`. The validation result is represented as a set of `ConstraintViolation` objects, each containing information such as the invalid property and a message.
Constraints describe what values are acceptable, while custom validators handle rules that built-in constraints cannot express. For example, an application can create a custom constraint for a domain-specific identifier and implement its checking logic in a validator class.
Bean Validation is different from business authorization or arbitrary application logic. A constraint should express a validity rule about data; rules involving database state, permissions, or complex workflows may belong in application or domain services instead.
Key points
- Use annotations to declare reusable data constraints.
- `Validator` evaluates constraints and returns `ConstraintViolation` results.
- Built-in constraints cover common checks such as nullability, size, and format.
- Custom constraints can encapsulate domain-specific validation logic.
- Validation does not replace business rules or authorization checks.
Interview questions
How do you validate an object programmatically?
Create a `Validator`, typically from a `ValidatorFactory`, and call `validate` on the object. The method returns a set of `ConstraintViolation` objects rather than requiring each field to be checked manually. Each violation provides information that can be used to construct an error response. In framework-based applications, validation can instead be triggered by framework integration.
What is Bean Validation in Java?
Bean Validation is a standard approach for declaring constraints on Java application data. Constraints are commonly expressed with annotations such as `@NotNull`, `@Size`, and `@Email`. A validation provider evaluates those constraints and reports violations. This keeps many straightforward input rules separate from the main application logic.
What is the difference between `@NotNull`, `@NotEmpty`, and `@NotBlank`?
`@NotNull` requires that the value is not `null`. `@NotEmpty` additionally requires that a supported container or character sequence is not empty, but whitespace can still be present for strings. `@NotBlank` is intended for character sequences and requires at least one non-whitespace character. The correct choice depends on the exact input requirement.
When would you create a custom Bean Validation constraint?
A custom constraint is useful when a validation rule is domain-specific or is not adequately represented by the built-in constraints. You define the constraint annotation and associate it with a validator implementation. The validator contains the logic for determining whether the value is valid. This keeps the rule reusable wherever the constraint is applied.
What should Bean Validation not be used for?
Bean Validation is well suited to declarative constraints on data, but it should not become a container for all business logic. Authorization decisions, complex workflows, and rules that depend heavily on changing application state often belong in services or domain logic. Some cross-field rules can be represented with class-level custom constraints, but that does not mean every business rule should be modeled as a constraint. The design should keep validation focused and understandable.