Class GroupedValidator<T>
- Type Parameters:
T- The type of value being validated
- All Implemented Interfaces:
Validator<T>
GroupedValidator wraps an existing validator and associates it with one or more validation groups. This allows the same validator type to be configured differently for different scenarios.
This class is typically created via Validator.forGroups(Class[]) rather
than being instantiated directly:
// Using forGroups() method (preferred)
Validator<String> required = new RequiredValidator<String>()
.forGroups(DefaultGroups.Create.class, UserGroups.Registration.class);
// Direct instantiation (less common)
Validator<String> required = new GroupedValidator<>(
new RequiredValidator<>(),
DefaultGroups.Create.class
);
- Since:
- 2025
- Version:
- 1.0
- Author:
- Marvin P. Warble Jr.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionGroupedValidator(Validator<T> delegate, Class<? extends ValidationGroup>... groups) Creates a GroupedValidator wrapping the specified validator with the given groups. -
Method Summary
Modifier and TypeMethodDescriptionfinal booleanbelongsToAny(Class<? extends ValidationGroup>... targetGroups) Checks if this validator belongs to any of the specified groups.forGroups(Class<? extends ValidationGroup>... newGroups) Creates a new validator with the specified validation groups.Returns the wrapped validator.Set<Class<? extends ValidationGroup>> Returns the validation groups as a set.Returns the default error message for this validator.Class<? extends ValidationGroup>[]groups()Returns the validation groups this validator belongs to.validate(T value, ValidationContext context) Validates the given value.withMessage(String message) Creates a new validator with a custom error message.
-
Constructor Details
-
GroupedValidator
@SafeVarargs public GroupedValidator(Validator<T> delegate, Class<? extends ValidationGroup>... groups) Creates a GroupedValidator wrapping the specified validator with the given groups.- Parameters:
delegate- The validator to wrapgroups- The validation groups this validator belongs to
-
-
Method Details
-
validate
Description copied from interface:ValidatorValidates the given value.Implementations should return
ValidationResult.valid()when the value passes validation, orValidationResult.invalid(String)with an appropriate message when it fails.Validators typically should not fail on null values - use
RequiredValidatorto enforce non-null requirements. This allows validators to be composed without unexpected failures. -
getMessage
Description copied from interface:ValidatorReturns the default error message for this validator.- Specified by:
getMessagein interfaceValidator<T>- Returns:
- The error message
-
withMessage
Description copied from interface:ValidatorCreates a new validator with a custom error message.This method returns a new instance with the custom message, leaving the original validator unchanged.
- Specified by:
withMessagein interfaceValidator<T>- Parameters:
message- The custom error message- Returns:
- A new validator instance with the custom message
-
groups
Description copied from interface:ValidatorReturns the validation groups this validator belongs to.By default, validators belong to
DefaultGroups.Default, meaning they run in all validation scenarios. Override this to restrict a validator to specific groups. -
forGroups
Description copied from interface:ValidatorCreates a new validator with the specified validation groups.This method returns a wrapped validator that belongs to the specified groups. The original validator is unchanged.
Usage:
// Only validate when creating Validator<String> required = new RequiredValidator<String>() .forGroups(DefaultGroups.Create.class); // Validate for both Create and Registration Validator<String> required = new RequiredValidator<String>() .forGroups(DefaultGroups.Create.class, UserGroups.Registration.class); -
getDelegate
Returns the wrapped validator.- Returns:
- The delegate validator
-
getGroupSet
Returns the validation groups as a set.- Returns:
- Unmodifiable set of validation groups
-
belongsToAny
Checks if this validator belongs to any of the specified groups.- Parameters:
targetGroups- The groups to check against- Returns:
- true if this validator belongs to at least one of the target groups
-