Class KiwiValidations


  • public class KiwiValidations
    extends Object
    Static utilities related to Jakarta Bean Validation (formerly Java Bean Validation). Relies on the Bean Validation API.

    Dependency requirements:

    The jakarta.validation:jakarta.validation-api dependency and some implementation such as Hibernate Validator (org.hibernate.validator:hibernate-validator must be available at runtime.

    • Constructor Summary

      Constructors 
      Constructor Description
      KiwiValidations()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addError​(javax.validation.ConstraintValidatorContext context, String template)
      Adds an error to the ConstraintValidatorContext using the specified template, thereby overriding the constraint's default message.
      static void addError​(javax.validation.ConstraintValidatorContext context, String template, String propertyName)
      Adds an error to the ConstraintValidatorContext using the specified template and property name, thereby overriding the constraint's default message.
      static Object getPropertyValue​(Object bean, String fieldName)
      Finds the value of the specified property by direct field access.
      static javax.validation.Validator getValidator()
      Return the re-usable (singleton) Validator instance.
      static javax.validation.Validator newValidator()
      Creates a new, default Validator instance using the default validator factory provided by the underlying bean validation implementation, for example Hibernate Valdiator.
      static void setValidator​(javax.validation.Validator newValidator)
      Reset the singleton Validator instance.
      static <T> Set<javax.validation.ConstraintViolation<T>> validate​(T object)
      Validate the given object using the singleton validator instance against the Default group.
      static <T> Set<javax.validation.ConstraintViolation<T>> validate​(T object, Class<?>... groupClasses)
      Validate the given object using the singleton validator instance against the specified validation groups.
    • Constructor Detail

      • KiwiValidations

        public KiwiValidations()
    • Method Detail

      • newValidator

        public static javax.validation.Validator newValidator()
        Creates a new, default Validator instance using the default validator factory provided by the underlying bean validation implementation, for example Hibernate Valdiator.
        Returns:
        a new Validator instance
      • getValidator

        public static javax.validation.Validator getValidator()
        Return the re-usable (singleton) Validator instance.
        Returns:
        singleton Validator instance
      • setValidator

        public static void setValidator​(javax.validation.Validator newValidator)
        Reset the singleton Validator instance.

        This is intended primarily for use by unit tests, to permit resetting the Validator. Use with caution and remember: with great power, come great responsibility.

        Parameters:
        newValidator - the new Validator to use as the singleton instance
        Implementation Note:
        This method is intentionally not synchronized. Since it is expected only to be used once at application startup, and during unit tests, we can skip adding synchronization to this and all other methods that use the singleton Validator instance. In other words, because we assume this will not be called in real production code, except perhaps one time at application startup, we don't think it's worth adding locking or synchronization on every method that uses the singleton Validator instance.
      • validate

        public static <T> Set<javax.validation.ConstraintViolation<T>> validate​(T object)
        Validate the given object using the singleton validator instance against the Default group.
        Type Parameters:
        T - the object type
        Parameters:
        object - the object to validate
        Returns:
        validation results
      • validate

        public static <T> Set<javax.validation.ConstraintViolation<T>> validate​(T object,
                                                                                Class<?>... groupClasses)
        Validate the given object using the singleton validator instance against the specified validation groups.
        Type Parameters:
        T - the object type
        Parameters:
        object - the object to validate
        groupClasses - zero or more validation group classes
        Returns:
        validation results
      • addError

        public static void addError​(javax.validation.ConstraintValidatorContext context,
                                    String template)
        Adds an error to the ConstraintValidatorContext using the specified template, thereby overriding the constraint's default message.
        Parameters:
        context - the validator context
        template - the template to use
        See Also:
        ConstraintValidatorContext.buildConstraintViolationWithTemplate(String)
      • addError

        public static void addError​(javax.validation.ConstraintValidatorContext context,
                                    String template,
                                    String propertyName)
        Adds an error to the ConstraintValidatorContext using the specified template and property name, thereby overriding the constraint's default message.

        This is intended to be used when a validation annotation applies at the type level, as opposed to a field or method. The propertyName can be used to specify the property node that the violation should be attached to. For example, if a validator validates multiple fields and the annotation is applied to the type, then this can be used to specify which field a constraint violation applies to.

        Parameters:
        context - the validator context
        template - the template to use
        propertyName - the property name to attach constrain violations to
      • getPropertyValue

        public static Object getPropertyValue​(Object bean,
                                              String fieldName)
        Finds the value of the specified property by direct field access.

        This is provided for validators to obtain the value of a specific field, and will only be useful when for validation annotations that are comparing multiple fields. For example, an annotation that validates a range of values between two fields in an object.

        Parameters:
        bean - the object
        fieldName - the property/field name
        Returns:
        the value of the property in the object, or null if any problem occurs
        Implementation Note:
        This uses KiwiReflection.findField(Object, String) to obtain the value.