Class 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:
    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 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.

    • Constructor Detail

      • KiwiValidationGroups

        public KiwiValidationGroups()
    • Method Detail

      • 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:
        NewObject, Default
      • 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:
        ExistingObject, Default
      • validateNewObject

        public static <T> Set<javax.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<javax.validation.ConstraintViolation<T>> validateNewObject​(javax.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<javax.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<javax.validation.ConstraintViolation<T>> validateExistingObject​(javax.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