Class GroupedValidator<T>

java.lang.Object
com.oorian.validation.GroupedValidator<T>
Type Parameters:
T - The type of value being validated
All Implemented Interfaces:
Validator<T>

public class GroupedValidator<T> extends Object implements Validator<T>
A wrapper that adds validation group support to any validator.

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 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 wrap
      groups - The validation groups this validator belongs to
  • Method Details

    • validate

      public ValidationResult validate(T value, ValidationContext context)
      Description copied from interface: Validator
      Validates the given value.

      Implementations should return ValidationResult.valid() when the value passes validation, or ValidationResult.invalid(String) with an appropriate message when it fails.

      Validators typically should not fail on null values - use RequiredValidator to enforce non-null requirements. This allows validators to be composed without unexpected failures.

      Specified by:
      validate in interface Validator<T>
      Parameters:
      value - The value to validate (may be null)
      context - The validation context for accessing other field values
      Returns:
      The validation result
    • getMessage

      public String getMessage()
      Description copied from interface: Validator
      Returns the default error message for this validator.
      Specified by:
      getMessage in interface Validator<T>
      Returns:
      The error message
    • withMessage

      public Validator<T> withMessage(String message)
      Description copied from interface: Validator
      Creates 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:
      withMessage in interface Validator<T>
      Parameters:
      message - The custom error message
      Returns:
      A new validator instance with the custom message
    • groups

      public Class<? extends ValidationGroup>[] groups()
      Description copied from interface: Validator
      Returns 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.

      Specified by:
      groups in interface Validator<T>
      Returns:
      Array of validation group classes this validator belongs to
      See Also:
    • forGroups

      @SafeVarargs public final Validator<T> forGroups(Class<? extends ValidationGroup>... newGroups)
      Description copied from interface: Validator
      Creates 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);
       
      Specified by:
      forGroups in interface Validator<T>
      Parameters:
      newGroups - The validation groups this validator should belong to
      Returns:
      A new validator instance with the specified groups
      See Also:
    • getDelegate

      public Validator<T> getDelegate()
      Returns the wrapped validator.
      Returns:
      The delegate validator
    • getGroupSet

      public Set<Class<? extends ValidationGroup>> getGroupSet()
      Returns the validation groups as a set.
      Returns:
      Unmodifiable set of validation groups
    • belongsToAny

      @SafeVarargs public final boolean belongsToAny(Class<? extends ValidationGroup>... targetGroups)
      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