Package org.kiwiproject.validation.group
Class KiwiValidationGroups
java.lang.Object
org.kiwiproject.validation.group.KiwiValidationGroups
An opinionated class that makes working with validation groups easier...if you are OK with the restrictions this
utility imposes. Specifically, it assumes you only need three validation groups:
- a group that should be included only when validating "new" objects (e.g. objects that have been instantiated but not yet persisted to some data store)
- a group that should be included only when validating "existing" objects (e.g. an object was retrieved from an external data source and has been changed with new information
-
a group that should always be included (this is the
Defaultvalidation group)
groups property to NewObject. If instead
you only want an annotation to be included for "existing" objects, set the groups property to
ExistingObject. If you always want a validation annotation to be included during validation, don't set
the groups property. Here's an example:
@Null(groups = NewObject.class) @NotNull(groups = ExistingObject.class) private Long id; @NotBlank private String content;In this example code,
id must be null when the object is "new" but must be non-null when the object "exists".
The content is always required. When performing the logic to insert a "new" object you would pass
newObjectGroups() as the groups in Validator.validate(Object, Class[]). Similarly, you would
pass existingObjectGroups() as the groups in Validator.validate(Object, Class[]) when performing
update logic on "existing" objects. For example, assuming a static import:
// validate a new object var violations = validator.validate(aNewObject, newObjectGroups()); // validate an existing object var violations = validator.validate(anExistingObject, existingObjectGroups());For even more convenience, you can just use the various
validateXxx methods, assuming as mentioned earlier
that you have annotated your classes with Jakarta Beans Validator annotations. A typical example is using
annotations such as NotNull and Null
and setting NewObject and ExistingObject as the value of the groups property, as shown
in the above code example. To validate a new object, you would then simply do the following:
var violations = KiwiValidationGroups.validateNewObject(aNewObject);And to validate an existing object, you would do:
var violations = KiwiValidationGroups.validateExistingObject(anExistingObject);Using this utility ensures objects are validated against all groups, unlike
GroupSequence
whose behavior is to stop validating any subsequent groups once a group fails validation. For example, if the
sequence is { Default.class, NewObject.class } and a constraint fails for the Default group, then
the constraints for NewObject will not be evaluated. In general this is not the behavior we want or
expect.
To reiterate, this utility is opinionated and therfore limited in that it only knows about NewObject and
ExistingObject, and expects them to always be validated along with the Default group. We have
found this to be useful in enough situations to include it in kiwi. But if you need more flexibility, don't
use this and instead just pass specific validation groups manually to the validate method in Validator.
-
Method Summary
Modifier and TypeMethodDescriptionstatic Class<?>[]Use this when validating an existing (persistent) object.static Class<?>[]Use this when validating a new (transient) object.static <T> Set<jakarta.validation.ConstraintViolation<T>>validateExistingObject(jakarta.validation.Validator validator, T object) Validate an existing (persistent) object using the givenValidatorinstance.static <T> Set<jakarta.validation.ConstraintViolation<T>>validateExistingObject(T object) Validate an existing (persistent) object using a defaultValidatorinstance.static <T> Set<jakarta.validation.ConstraintViolation<T>>validateNewObject(jakarta.validation.Validator validator, T object) Validate a new (transient) object using the givenValidatorinstance.static <T> Set<jakarta.validation.ConstraintViolation<T>>validateNewObject(T object) Validate a new (transient) object using a defaultValidatorinstance.
-
Method Details
-
newObjectGroups
Use this when validating a new (transient) object. The groups includeNewObjectandDefault.- Returns:
- a new array containing the new object group classes
- See Also:
-
NewObjectDefault
-
existingObjectGroups
Use this when validating an existing (persistent) object. The groups includeExistingObjectandDefault.- Returns:
- a new array containing the existing object group classes
- See Also:
-
ExistingObjectDefault
-
validateNewObject
Validate a new (transient) object using a defaultValidatorinstance.- Type Parameters:
T- the type of object being validated- Parameters:
object- the object to validate- Returns:
- validation results
-
validateNewObject
public static <T> Set<jakarta.validation.ConstraintViolation<T>> validateNewObject(jakarta.validation.Validator validator, T object) Validate a new (transient) object using the givenValidatorinstance.- Type Parameters:
T- the type of object being validated- Parameters:
validator- theValidatorinstance to useobject- the object to validate- Returns:
- validation results
-
validateExistingObject
Validate an existing (persistent) object using a defaultValidatorinstance.- Type Parameters:
T- the type of object being validated- Parameters:
object- the object to validate- Returns:
- validation results
-
validateExistingObject
public static <T> Set<jakarta.validation.ConstraintViolation<T>> validateExistingObject(jakarta.validation.Validator validator, T object) Validate an existing (persistent) object using the givenValidatorinstance.- Type Parameters:
T- the type of object being validated- Parameters:
validator- theValidatorinstance to useobject- the object to validate- Returns:
- validation results
-