Class KiwiValidationGroups

java.lang.Object
org.kiwiproject.validation.group.KiwiValidationGroups

public final 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:
  1. 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)
  2. 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
  3. a group that should always be included (this is the Default validation group)
To use this utility, annotate your classes with Jakarta Beans Validation annotations. If you only want an annotation to be included for "new" objects, set the 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 Type
    Method
    Description
    static 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 given Validator instance.
    static <T> Set<jakarta.validation.ConstraintViolation<T>>
    Validate an existing (persistent) object using a default Validator instance.
    static <T> Set<jakarta.validation.ConstraintViolation<T>>
    validateNewObject(jakarta.validation.Validator validator, T object)
    Validate a new (transient) object using the given Validator instance.
    static <T> Set<jakarta.validation.ConstraintViolation<T>>
    Validate a new (transient) object using a default Validator instance.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • newObjectGroups

      public static Class<?>[] newObjectGroups()
      Use this when validating a new (transient) object. The groups include NewObject and Default.
      Returns:
      a new array containing the new object group classes
      See Also:
    • existingObjectGroups

      public static Class<?>[] existingObjectGroups()
      Use this when validating an existing (persistent) object. The groups include ExistingObject and Default.
      Returns:
      a new array containing the existing object group classes
      See Also:
    • validateNewObject

      public static <T> Set<jakarta.validation.ConstraintViolation<T>> validateNewObject(T object)
      Validate a new (transient) object using a default Validator instance.
      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 given Validator instance.
      Type Parameters:
      T - the type of object being validated
      Parameters:
      validator - the Validator instance to use
      object - the object to validate
      Returns:
      validation results
    • validateExistingObject

      public static <T> Set<jakarta.validation.ConstraintViolation<T>> validateExistingObject(T object)
      Validate an existing (persistent) object using a default Validator instance.
      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 given Validator instance.
      Type Parameters:
      T - the type of object being validated
      Parameters:
      validator - the Validator instance to use
      object - the object to validate
      Returns:
      validation results