Class ValidationUtils
The ValidationUtils class supports two primary responsibilities:
- Object validation: Validates message payloads using the
Validatorconfigured on the currentFluxzeroinstance, falling back todefaultValidator, and optionally applying validation groups specified viaValidateWithannotations. - Authorization enforcement: Performs role-based access checks based on annotations such as
RequiresAnyRole,ForbidsAnyRole, andNoUserRequireddeclared on classes, methods, or packages.
Validation
Validation is typically executed automatically by the ValidatingInterceptor before invoking handler methods.
Programmatic convenience methods such as assertValid(Object, Class[]) use the validator configured on the
current Fluxzero instance when one is bound to the current execution. Outside a Fluxzero context, the default
validator implementation is used. That default is loaded via ServiceLoader (e.g.
DefaultValidator).
Methods like assertValid(Object, Class[]) and checkValidity(Object, Validator, Class[])
support recursive validation of collections and custom validation groups.
Authorization
The class also performs role-based security checks. Handler methods or message payloads can declare required roles,
which are evaluated against the current User. If authorization fails, the utility throws either
UnauthenticatedException or UnauthorizedException.
Examples
ValidationUtils.assertValid(payload); // Validate a single object
boolean isValid = ValidationUtils.isValid(payload, MyGroup.class); // Validate with a group
User user = ...;
ValidationUtils.assertAuthorized(MyCommand.class, user); // Authorization check
- See Also:
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanassertAuthorized(Class<?> payloadType, User user) Verifies whether the given user is authorized to issue the given payload, based on roles declared via annotations on the payload's class or package.static booleanassertAuthorized(Class<?> target, Executable method, User user) Checks if the given user is authorized to invoke the given method on the given target.protected static booleanassertAuthorized(String action, User user, ValidationUtils.RequiredRole[] requiredRoles) static voidassertValid(Object object, Validator validator, Class<?>... groups) Asserts that the object is valid using the givenValidatorand validation groups.static voidassertValid(Object object, Class<?>... groups) Asserts that the given object is valid, using the current FluxzeroValidator.static voidassertValidParameters(Object target, Executable executable, Object[] arguments) Asserts that executable arguments satisfy Jakarta Validation constraints declared on method or constructor parameters.static voidassertValidReturnValue(Object target, Executable executable, Object returnValue) Asserts that an executable return value satisfies constraints declared on the method or constructor return value.static Optional<ValidationException> checkValidity(Object object, Validator validator, Class<?>... groups) Checks whether the provided object is valid using the givenValidatorand validation groups.static Optional<ValidationException> checkValidity(Object object, Class<?>... groups) Checks whether the provided object is valid, using the current FluxzeroValidatorand validation groups.static Set<ConstraintViolation<?>> getConstraintViolations(Collection<?> objects, Validator validator, Class<?>... groups) Validates each element in the collection using the provided validator and returns structured constraint violations.static Set<ConstraintViolation<?>> getConstraintViolations(Collection<?> objects, Class<?>... groups) Validates each element in the collection using the current Fluxzero validator and returns structured constraint violations.static <T> Set<ConstraintViolation<T>> getConstraintViolations(T object, Validator validator, Class<?>... groups) Validates the object using the provided validator and returns structured constraint violations with property paths.static <T> Set<ConstraintViolation<T>> getConstraintViolations(T object, Class<?>... groups) Validates the object using the current Fluxzero validator and returns structured constraint violations with property paths.protected static ValidationUtils.RequiredRole[]getRequiredRoles(Collection<? extends Annotation> annotations) static booleanignoreSilently(Class<?> payloadType, User user) Determines whether a particular operation on a payload type should be ignored without raising an exception.static booleanignoreSilently(Class<?> target, Executable method, User user) Determines whether a specific method invocation on a target class by a given user should be ignored without raising an exception, based on the user's authorization.static booleanReturnstrueif the object is valid, using the givenValidatorand validation groups.static booleanReturnstrueif the given object is valid using the current FluxzeroValidatorand validation groups.
-
Field Details
-
defaultValidator
Returns the defaultValidatorused for message validation.This is resolved via Java's
ServiceLoadermechanism. If no customValidatoris found, a default Jakarta Validation implementation is used.
-
-
Constructor Details
-
ValidationUtils
public ValidationUtils()
-
-
Method Details
-
checkValidity
Checks whether the provided object is valid, using the current FluxzeroValidatorand validation groups.- Parameters:
object- the object to validategroups- optional validation groups- Returns:
- an
Optionalcontaining aValidationExceptionif validation fails, or empty if valid
-
getConstraintViolations
Validates the object using the current Fluxzero validator and returns structured constraint violations with property paths.This method is intended for tests, diagnostics, and integrations that need structured validation output instead of the formatted messages stored in
ValidationException.- Type Parameters:
T- object type- Parameters:
object- the object to validategroups- optional validation groups- Returns:
- structured constraint violations
-
getConstraintViolations
public static Set<ConstraintViolation<?>> getConstraintViolations(Collection<?> objects, Class<?>... groups) Validates each element in the collection using the current Fluxzero validator and returns structured constraint violations.- Parameters:
objects- the objects to validategroups- optional validation groups- Returns:
- structured constraint violations for the collection elements
-
isValid
-
assertValid
Asserts that the given object is valid, using the current FluxzeroValidator.Throws a
ValidationExceptionif the object fails validation.- Parameters:
object- the object to validategroups- optional validation groups- Throws:
ValidationException- if validation fails
-
checkValidity
public static Optional<ValidationException> checkValidity(Object object, Validator validator, Class<?>... groups) Checks whether the provided object is valid using the givenValidatorand validation groups.- Parameters:
object- the object to validatevalidator- the validator to usegroups- optional validation groups- Returns:
- an
Optionalcontaining aValidationExceptionif invalid, or empty if valid
-
getConstraintViolations
public static <T> Set<ConstraintViolation<T>> getConstraintViolations(T object, Validator validator, Class<?>... groups) Validates the object using the provided validator and returns structured constraint violations with property paths.- Type Parameters:
T- object type- Parameters:
object- the object to validatevalidator- the validator to usegroups- optional validation groups- Returns:
- structured constraint violations
- Throws:
UnsupportedOperationException- if the supplied validator does not expose raw violations
-
getConstraintViolations
public static Set<ConstraintViolation<?>> getConstraintViolations(Collection<?> objects, Validator validator, Class<?>... groups) Validates each element in the collection using the provided validator and returns structured constraint violations.- Parameters:
objects- the objects to validatevalidator- the validator to usegroups- optional validation groups- Returns:
- structured constraint violations for the collection elements
- Throws:
UnsupportedOperationException- if the supplied validator does not expose raw violations
-
isValid
Returnstrueif the object is valid, using the givenValidatorand validation groups.- Parameters:
object- the object to validatevalidator- the validator to usegroups- optional validation groups- Returns:
trueif valid,falseotherwise
-
assertValid
Asserts that the object is valid using the givenValidatorand validation groups.Throws a
ValidationExceptionif validation fails.- Parameters:
object- the object to validatevalidator- the validator to usegroups- optional validation groups- Throws:
ValidationException- if validation fails
-
assertValidParameters
public static void assertValidParameters(@Nullable Object target, Executable executable, Object[] arguments) Asserts that executable arguments satisfy Jakarta Validation constraints declared on method or constructor parameters.- Parameters:
target- the target object for method invocation (ignored for constructors and static methods)executable- the executable that will be invokedarguments- resolved argument values in declaration order
-
assertValidReturnValue
public static void assertValidReturnValue(@Nullable Object target, Executable executable, @Nullable Object returnValue) Asserts that an executable return value satisfies constraints declared on the method or constructor return value.- Parameters:
target- the target object for method invocation (ignored for constructors and static methods)executable- the executable that produced the valuereturnValue- returned value
-
assertAuthorized
public static boolean assertAuthorized(Class<?> payloadType, @Nullable User user) throws UnauthenticatedException, UnauthorizedException Verifies whether the given user is authorized to issue the given payload, based on roles declared via annotations on the payload's class or package.Returns
trueif the user is authorized.If the user is not authorized, either
falseis returned or an exception is thrown. Which happens depends on the detected annotation.- Parameters:
payloadType- the class of the payloaduser- the authenticated user (may be null)- Returns:
trueif authorized,falseif authorization should fail quietly- Throws:
UnauthenticatedException- if authentication is required but the user isnullUnauthorizedException- if the user lacks required roles
-
ignoreSilently
Determines whether a particular operation on a payload type should be ignored without raising an exception.Note: If the user is not authorized and an error occurs during the authorization check, the error is caught silently, and the method invocation is allowed to proceed.
- Parameters:
payloadType- the class of the payload to be evaluateduser- the user whose authorization is being evaluated; may be null for unauthenticated access- Returns:
trueif the operation should be ignored silently,falseotherwise
-
assertAuthorized
Checks if the given user is authorized to invoke the given method on the given target.Returns
trueif the user is authorized.If the user is not authorized, either
falseis returned or an exception is thrown. Which happens depends on the detected annotation.Role requirements may be defined via annotations on the method, class, or package.
- Parameters:
target- the class declaring the methodmethod- the method to checkuser- the user to check- Returns:
trueif authorized,falseif authorization should fail quietly- Throws:
UnauthenticatedException- if authentication is required but the user isnullUnauthorizedException- if the user lacks required roles
-
ignoreSilently
Determines whether a specific method invocation on a target class by a given user should be ignored without raising an exception, based on the user's authorization.Note: If the user is not authorized and an error occurs during the authorization check, the error is caught silently, and the method invocation is allowed to proceed.
- Parameters:
target- the class declaring the method to be checkedmethod- the executable method to be checkeduser- the user whose authorization is being evaluated- Returns:
trueif the method invocation should be ignored without raising an exception,falseotherwise
-
assertAuthorized
protected static boolean assertAuthorized(String action, @Nullable User user, ValidationUtils.RequiredRole[] requiredRoles) -
getRequiredRoles
protected static ValidationUtils.RequiredRole[] getRequiredRoles(Collection<? extends Annotation> annotations)
-