Package org.kiwiproject.validation.group
Class KiwiValidationGroups
- java.lang.Object
-
- org.kiwiproject.validation.group.KiwiValidationGroups
-
public class KiwiValidationGroups extends Object
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)
groupsproperty toNewObject. If instead you only want an annotation to be included for "existing" objects, set thegroupsproperty toExistingObject. If you always want a validation annotation to be included during validation, don't set thegroupsproperty. Here's an example:@Null(groups = NewObject.class) @NotNull(groups = ExistingObject.class) private Long id; @NotBlank private String content;
In this example code,idmust be null when the object is "new" but must be non-null when the object "exists". Thecontentis always required. When performing the logic to insert a "new" object you would passnewObjectGroups()as the groups inValidator.validate(Object, Class[]). Similarly, you would passexistingObjectGroups()as the groups inValidator.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 variousvalidateXxxmethods, assuming as mentioned earlier that you have annotated your classes with Jakarta Beans Validator annotations. A typical example is using annotations such asNotNullandNulland settingNewObjectandExistingObjectas the value of thegroupsproperty, 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, unlikeGroupSequencewhose behavior is to stop validating any subsequent groups once a group fails validation. For example, if the the sequence is{ Default.class, NewObject.class }and a constraint fails for theDefaultgroup, then the constraints forNewObjectwill 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
NewObjectandExistingObject, and expects them to always be validated along with theDefaultgroup. 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 inValidator.
-
-
Constructor Summary
Constructors Constructor Description KiwiValidationGroups()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Class<?>[]existingObjectGroups()Use this when validating an existing (persistent) object.static Class<?>[]newObjectGroups()Use this when validating a new (transient) object.static <T> Set<javax.validation.ConstraintViolation<T>>validateExistingObject(javax.validation.Validator validator, T object)Validate an existing (persistent) object using the givenValidatorinstance.static <T> Set<javax.validation.ConstraintViolation<T>>validateExistingObject(T object)Validate an existing (persistent) object using a defaultValidatorinstance.static <T> Set<javax.validation.ConstraintViolation<T>>validateNewObject(javax.validation.Validator validator, T object)Validate a new (transient) object using the givenValidatorinstance.static <T> Set<javax.validation.ConstraintViolation<T>>validateNewObject(T object)Validate a new (transient) object using a defaultValidatorinstance.
-
-
-
Method Detail
-
newObjectGroups
public static Class<?>[] newObjectGroups()
Use this when validating a new (transient) object. The groups includeNewObjectandDefault.- Returns:
- a new array containing the new object group classes
- See Also:
NewObject,Default
-
existingObjectGroups
public static Class<?>[] existingObjectGroups()
Use this when validating an existing (persistent) object. The groups includeExistingObjectandDefault.- Returns:
- a new array containing the existing object group classes
- See Also:
ExistingObject,Default
-
validateNewObject
public static <T> Set<javax.validation.ConstraintViolation<T>> validateNewObject(T object)
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<javax.validation.ConstraintViolation<T>> validateNewObject(javax.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
public static <T> Set<javax.validation.ConstraintViolation<T>> validateExistingObject(T object)
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<javax.validation.ConstraintViolation<T>> validateExistingObject(javax.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
-
-