Class ValidationContext<T>

java.lang.Object
org.dellroad.stuff.validation.ValidationContext<T>
Type Parameters:
T - type of the root object being validated

public class ValidationContext<T> extends Object
Provides additional context for ConstraintValidator implementations.

ValidationContext gives ConstraintValidator implementations access to the root object being validated. This breaks the usual principle of locality for validation (i.e., that validation of a specific bean proceeds unaware of that bean's parents) but it can make custom validators more convenient to implement. Subclasses are encouraged to provide additional application-specific information.

Validation must be performed via validate() for this class to work.

  • Constructor Details

    • ValidationContext

      public ValidationContext(T root, Class<?>... groups)
      Construct a new validation context configured to validate the given root object, using the given validation group(s).
      Parameters:
      root - root object to be validated
      groups - group(s) targeted for validation (if empty, defaults to Default)
      Throws:
      IllegalArgumentException - if either paramter is null
  • Method Details

    • getRoot

      public final T getRoot()
      Get the root object associated with this instance.
      Returns:
      root of validation object graph
    • getGroups

      public final Class<?>[] getGroups()
      Get the validation groups associated with this instance.
      Returns:
      configured validation groups
    • validate

      public Set<jakarta.validation.ConstraintViolation<T>> validate()
      Validate this instance's root object. This is a convenience method, equivalent to:
      validate(Validation.buildDefaultValidatorFactory().getValidator())
      Returns:
      zero or more violations
      Throws:
      IllegalStateException - if this method is invoked re-entrantly
    • validate

      public Set<jakarta.validation.ConstraintViolation<T>> validate(jakarta.validation.Validator validator)
      Validate this instance's root object using the given Validator, making this context available to the current thread during the validation process via getCurrentContext().
      Parameters:
      validator - the validator
      Returns:
      zero or more violations
      Throws:
      IllegalArgumentException - if validator is null
      IllegalStateException - if this method is invoked re-entrantly
    • validate

      public <S> Set<jakarta.validation.ConstraintViolation<S>> validate(jakarta.validation.Validator validator, S target)
      Validate the given target object using the given Validator, making this context available to the current thread during the validation process via getCurrentContext().
      Parameters:
      validator - the validator
      target - object to validate
      Returns:
      zero or more violations
      Throws:
      IllegalArgumentException - if either parameter is null
      IllegalStateException - if this method is invoked re-entrantly
    • getCurrentContext

      public static ValidationContext<?> getCurrentContext()
      Get the ValidationContext associated with the current thread. This method is only valid during invocations of validate().
      Returns:
      current ValidationContext
      Throws:
      IllegalStateException - if validate() is not currently executing
    • getCurrentContext

      public static <V extends ValidationContext<?>> V getCurrentContext(Class<V> type)
      Get the ValidationContext associated with the current thread, cast to the desired type. This method is only valid during invocations of validate().
      Type Parameters:
      V - root validation object type
      Parameters:
      type - required type
      Returns:
      current ValidationContext
      Throws:
      IllegalStateException - if validate() is not currently executing
      ClassCastException - if the current ValidationContext is not of type type
      NullPointerException - if type is null
    • getCurrentRoot

      public static Object getCurrentRoot()
      Convenience method to get the root object being validated by the current thread. This method is only valid during invocations of validate().
      Returns:
      current validation root object
      Throws:
      IllegalStateException - if validate() is not currently executing
    • getCurrentRoot

      public static <T> T getCurrentRoot(Class<T> type)
      Convenience method to get the root object being validated by the current thread, cast to the desired type. This method is only valid during invocations of validate().
      Type Parameters:
      T - root validation object type
      Parameters:
      type - required type
      Returns:
      current validation root object
      Throws:
      IllegalStateException - if validate() is not currently executing
      ClassCastException - if the current validation root is not of type type
      NullPointerException - if type is null
    • getThreadLocalHolder

      public static ThreadLocalHolder<ValidationContext<?>> getThreadLocalHolder()
      Get the ThreadLocalHolder used by this class.
      Returns:
      ThreadLocalHolder for ValidationContext
    • getUniqueDomain

      public Set<Object> getUniqueDomain(String domain)
      Get the uniqueness domain with the given name. Used to validate @Unique constraints.
      Parameters:
      domain - name of the uniqueness domain
      Returns:
      set containing the unique values that have already been seen during this validation check