Class JakartaValidation
- All Implemented Interfaces:
BodyValidator<Object>,Validator<Object>,AutoCloseable
Validator backed by Jakarta Validation annotations, using Hibernate Validator.
record CreateUser(@NotBlank @Size(max = 80) String name, @Valid List<@NotNull Item> items) {}
static final JakartaValidation VALIDATION = JakartaValidation.create();
var user = Validation.require(VALIDATION, command);
Constraints may be placed on record components, fields and container elements; @Valid
cascades into nested objects and collection elements. Put it on the type argument (
List<@Valid Item>): @Valid List<Item> also cascades, but the provider logs deprecation
warning HV000271, which is left visible on purpose. Groups and group sequences are passed
to create(Class[]); a class-level @GroupSequence redefining the default group is
honored. Executable validation (method parameters and return values) is not offered: this
validator checks values only. Each constraint violation becomes a Violation whose field
is the property path (items[2].sku; map keys and set positions are omitted, class-level
constraints report the object's path or, for the validated value itself, the empty FieldPath.ROOT) and whose code is the constraint annotation's simple name in snake case (
NotBlank becomes not_blank, DecimalMin becomes decimal_min). Provider
messages and invalid values are never read into violations. Message interpolation is disabled
entirely, no Expression Language implementation is used, and META-INF/validation.xml is
ignored, so a template containing ${...} is never evaluated. Violations are sorted by
field and code and capped at Validation.MAX_VIOLATIONS. A null value is reported
as not_null on the whole value.
Instances are thread-safe and meant to be created once at startup and shared; creation
bootstraps the provider and is comparatively expensive. Close the instance when the application
stops to release the provider's caches. If the provider fails while validating, for example
because a constraint is declared on an unsupported type or a constraint implementation throws,
validate(Object) throws an InternalServerErrorException with code
internal_server_error; the provider's exception is attached as its cause for logging and is
never sent to clients.
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Releases the provider's resources; later calls tovalidate(Object)fail.static JakartaValidationBootstraps a validator for the given groups.Validates a value's constraints.
-
Method Details
-
create
Bootstraps a validator for the given groups.- Parameters:
groups- validation groups to check; none means the default group- Returns:
- a validator to share and close when the application stops
- Throws:
NullPointerException- if a group is null
-
validate
Validates a value's constraints.- Specified by:
validatein interfaceBodyValidator<Object>- Specified by:
validatein interfaceValidator<Object>- Parameters:
value- value to validate- Returns:
- immutable violations sorted by field and code, at most
Validation.MAX_VIOLATIONS - Throws:
InternalServerErrorException- if the provider failsIllegalStateException- if this validator is closed
-
close
public void close()Releases the provider's resources; later calls tovalidate(Object)fail. Idempotent.- Specified by:
closein interfaceAutoCloseable
-