Package org.dellroad.stuff.validation
Class ValidationContext<T>
java.lang.Object
org.dellroad.stuff.validation.ValidationContext<T>
- Type Parameters:
T
- type of the root object being validated
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 Summary
ConstructorDescriptionValidationContext
(T root, Class<?>... groups) Construct a new validation context configured to validate the given root object, using the given validation group(s). -
Method Summary
Modifier and TypeMethodDescriptionstatic ValidationContext<?>
Get theValidationContext
associated with the current thread.static <V extends ValidationContext<?>>
VgetCurrentContext
(Class<V> type) Get theValidationContext
associated with the current thread, cast to the desired type.static Object
Convenience method to get the root object being validated by the current thread.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.final Class<?>[]
Get the validation groups associated with this instance.final T
getRoot()
Get the root object associated with this instance.static ThreadLocalHolder<ValidationContext<?>>
Get theThreadLocalHolder
used by this class.getUniqueDomain
(String domain) Get the uniqueness domain with the given name.validate()
Validate this instance's root object.validate
(jakarta.validation.Validator validator) Validate this instance's root object using the givenValidator
, making this context available to the current thread during the validation process viagetCurrentContext()
.<S> Set<jakarta.validation.ConstraintViolation<S>>
validate
(jakarta.validation.Validator validator, S target) Validate the given target object using the givenValidator
, making this context available to the current thread during the validation process viagetCurrentContext()
.
-
Constructor Details
-
ValidationContext
Construct a new validation context configured to validate the given root object, using the given validation group(s).- Parameters:
root
- root object to be validatedgroups
- group(s) targeted for validation (if empty, defaults toDefault
)- Throws:
IllegalArgumentException
- if either paramter is null
-
-
Method Details
-
getRoot
Get the root object associated with this instance.- Returns:
- root of validation object graph
-
getGroups
Get the validation groups associated with this instance.- Returns:
- configured validation groups
-
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 givenValidator
, making this context available to the current thread during the validation process viagetCurrentContext()
.- Parameters:
validator
- the validator- Returns:
- zero or more violations
- Throws:
IllegalArgumentException
- ifvalidator
is nullIllegalStateException
- 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 givenValidator
, making this context available to the current thread during the validation process viagetCurrentContext()
.- Parameters:
validator
- the validatortarget
- object to validate- Returns:
- zero or more violations
- Throws:
IllegalArgumentException
- if either parameter is nullIllegalStateException
- if this method is invoked re-entrantly
-
getCurrentContext
Get theValidationContext
associated with the current thread. This method is only valid during invocations ofvalidate()
.- Returns:
- current
ValidationContext
- Throws:
IllegalStateException
- ifvalidate()
is not currently executing
-
getCurrentContext
Get theValidationContext
associated with the current thread, cast to the desired type. This method is only valid during invocations ofvalidate()
.- Type Parameters:
V
- root validation object type- Parameters:
type
- required type- Returns:
- current
ValidationContext
- Throws:
IllegalStateException
- ifvalidate()
is not currently executingClassCastException
- if the currentValidationContext
is not of typetype
NullPointerException
- iftype
is null
-
getCurrentRoot
Convenience method to get the root object being validated by the current thread. This method is only valid during invocations ofvalidate()
.- Returns:
- current validation root object
- Throws:
IllegalStateException
- ifvalidate()
is not currently executing
-
getCurrentRoot
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 ofvalidate()
.- Type Parameters:
T
- root validation object type- Parameters:
type
- required type- Returns:
- current validation root object
- Throws:
IllegalStateException
- ifvalidate()
is not currently executingClassCastException
- if the current validation root is not of typetype
NullPointerException
- iftype
is null
-
getThreadLocalHolder
Get theThreadLocalHolder
used by this class.- Returns:
ThreadLocalHolder
for ValidationContext
-
getUniqueDomain
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
-