Class KiwiValidations

java.lang.Object
org.kiwiproject.validation.KiwiValidations

public final 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.

  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    addError(jakarta.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(jakarta.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 <T> void
    checkArgumentNoViolations(Set<jakarta.validation.ConstraintViolation<T>> violations)
    Ensures the set of constraint violations is empty, throwing an IllegalArgumentException otherwise.
    static <T> void
    checkArgumentNoViolations(Set<jakarta.validation.ConstraintViolation<T>> violations, String errorMessage)
    Ensures the set of constraint violations is empty, throwing an IllegalArgumentException otherwise.
    static <T> void
    checkArgumentNoViolations(Set<jakarta.validation.ConstraintViolation<T>> violations, String errorMessageTemplate, Object... errorMessageArgs)
    Ensures the set of constraint violations is empty, throwing an IllegalArgumentException otherwise.
    static <T> void
    checkArgumentNoViolations(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<Set<jakarta.validation.ConstraintViolation<T>>,String> errorMessageCreator)
    Ensures the set of constraint violations is empty, throwing an IllegalArgumentException otherwise.
    static <T> void
    Validate the given object using the singleton validator instance against the Default group.
    static <T> void
    checkArgumentValid(T object, Class<?>... groupClasses)
    Validate the given object using the singleton validator instance against the specified validation groups.
    static <T> void
    checkArgumentValid(T object, String errorMessage)
    Validate the given object using the singleton validator instance against the Default group.
    static <T> void
    checkArgumentValid(T object, String errorMessage, Class<?>... groupClasses)
    Validate the given object using the singleton validator instance against the specified validation groups.
    static <T> void
    checkArgumentValid(T object, String errorMessageTemplate, Object... errorMessageArgs)
    Validate the given object using the singleton validator instance against the Default group.
    static <T> void
    checkArgumentValid(T object, String errorMessageTemplate, List<Object> errorMessageArgs, Class<?>... groupClasses)
    Validate the given object using the singleton validator instance against the specified validation groups.
    static <T> void
    checkArgumentValid(T object, Function<Set<jakarta.validation.ConstraintViolation<T>>,String> errorMessageCreator)
    Validate the given object using the singleton validator instance against the Default group.
    static <T> void
    checkArgumentValid(T object, Function<Set<jakarta.validation.ConstraintViolation<T>>,String> errorMessageCreator, Class<?>... groupClasses)
    Validate the given object using the singleton validator instance against the specified validation groups.
    static @Nullable Object
    getPropertyValue(Object bean, String fieldName)
    Finds the value of the specified property by direct field access.
    static jakarta.validation.Validator
    Return the re-usable (singleton) Validator instance.
    static jakarta.validation.Validator
    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(jakarta.validation.Validator newValidator)
    Reset the singleton Validator instance.
    static <T> void
    throwConstraintViolationExceptionIfNotEmpty(Set<jakarta.validation.ConstraintViolation<T>> violations)
    If the set of constraint violations is not empty, throw a ConstraintViolationException.
    static <T> Set<jakarta.validation.ConstraintViolation<T>>
    validate(T object)
    Validate the given object using the singleton validator instance against the Default group.
    static <T> Set<jakarta.validation.ConstraintViolation<T>>
    validate(T object, Class<?>... groupClasses)
    Validate the given object using the singleton validator instance against the specified validation groups.
    static <T> void
    validateThrowing(T object)
    Validate the given object using the singleton validator instance against the Default group, and throw a ConstraintViolationException if validation fails.
    static <T> void
    validateThrowing(T object, Class<?>... groupClasses)
    Validate the given object using the singleton validator instance against the specified validation groups, and throw a ConstraintViolationException if validation fails.

    Methods inherited from class java.lang.Object

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

    • newValidator

      public static jakarta.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 jakarta.validation.Validator getValidator()
      Return the re-usable (singleton) Validator instance.
      Returns:
      singleton Validator instance
    • setValidator

      public static void setValidator(jakarta.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<jakarta.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<jakarta.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
    • validateThrowing

      public static <T> void validateThrowing(T object)
      Validate the given object using the singleton validator instance against the Default group, and throw a ConstraintViolationException if validation fails.
      Type Parameters:
      T - the object type
      Parameters:
      object - the object to validate
    • validateThrowing

      public static <T> void validateThrowing(T object, Class<?>... groupClasses)
      Validate the given object using the singleton validator instance against the specified validation groups, and throw a ConstraintViolationException if validation fails.
      Type Parameters:
      T - the object type
      Parameters:
      object - the object to validate
      groupClasses - zero or more validation group classes
    • throwConstraintViolationExceptionIfNotEmpty

      public static <T> void throwConstraintViolationExceptionIfNotEmpty(Set<jakarta.validation.ConstraintViolation<T>> violations)
      If the set of constraint violations is not empty, throw a ConstraintViolationException.
      Type Parameters:
      T - the object type
      Parameters:
      violations - the constraint violations
    • checkArgumentValid

      public static <T> void checkArgumentValid(T object)
      Validate the given object using the singleton validator instance against the Default group. If the argument is not valid, throws an IllegalArgumentException. The exception message is supplied by checkArgumentNoViolations(Set).
      Type Parameters:
      T - the object type
      Parameters:
      object - the object to validate
      See Also:
    • checkArgumentValid

      public static <T> void checkArgumentValid(T object, String errorMessage)
      Validate the given object using the singleton validator instance against the Default group. If the argument is not valid, throws an IllegalArgumentException.
      Type Parameters:
      T - the object type
      Parameters:
      object - the object to validate
      errorMessage - the error message for the exception
    • checkArgumentValid

      public static <T> void checkArgumentValid(T object, String errorMessageTemplate, Object... errorMessageArgs)
      Validate the given object using the singleton validator instance against the Default group. If the argument is not valid, throws an IllegalArgumentException.
      Type Parameters:
      T - the object type
      Parameters:
      object - the object to validate
      errorMessageTemplate - a template for the exception message should the check fail, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to Strings using String.valueOf(Object).
    • checkArgumentValid

      public static <T> void checkArgumentValid(T object, Function<Set<jakarta.validation.ConstraintViolation<T>>,String> errorMessageCreator)
      Validate the given object using the singleton validator instance against the Default group. If the argument is not valid, throws an IllegalArgumentException.
      Type Parameters:
      T - the object type
      Parameters:
      object - the object to validate
      errorMessageCreator - a Function that transforms constraint violations into an error message for the exception
    • checkArgumentValid

      public static <T> void checkArgumentValid(T object, Class<?>... groupClasses)
      Validate the given object using the singleton validator instance against the specified validation groups. If the argument is not valid, throws an IllegalArgumentException. The exception message is supplied by checkArgumentNoViolations(Set).
      Type Parameters:
      T - the object type
      Parameters:
      object - the object to validate
      groupClasses - zero or more validation group classes
      See Also:
    • checkArgumentValid

      public static <T> void checkArgumentValid(T object, String errorMessage, Class<?>... groupClasses)
      Validate the given object using the singleton validator instance against the specified validation groups. If the argument is not valid, throws an IllegalArgumentException.
      Type Parameters:
      T - the object type
      Parameters:
      object - the object to validate
      errorMessage - the error message for the exception
      groupClasses - zero or more validation group classes
    • checkArgumentValid

      public static <T> void checkArgumentValid(T object, String errorMessageTemplate, List<Object> errorMessageArgs, Class<?>... groupClasses)
      Validate the given object using the singleton validator instance against the specified validation groups. If the argument is not valid, throws an IllegalArgumentException.
      Type Parameters:
      T - the object type
      Parameters:
      object - the object to validate
      errorMessageTemplate - a template for the exception message should the check fail, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to Strings using String.valueOf(Object).
      groupClasses - zero or more validation group classes
    • checkArgumentValid

      public static <T> void checkArgumentValid(T object, Function<Set<jakarta.validation.ConstraintViolation<T>>,String> errorMessageCreator, Class<?>... groupClasses)
      Validate the given object using the singleton validator instance against the specified validation groups. If the argument is not valid, throws an IllegalArgumentException.
      Type Parameters:
      T - the object type
      Parameters:
      object - the object to validate
      errorMessageCreator - a Function that transforms constraint violations into an error message for the exception
      groupClasses - zero or more validation group classes
    • checkArgumentNoViolations

      public static <T> void checkArgumentNoViolations(Set<jakarta.validation.ConstraintViolation<T>> violations)
      Ensures the set of constraint violations is empty, throwing an IllegalArgumentException otherwise. The exception message is supplied by KiwiConstraintViolations.simpleCombinedErrorMessageOrNull(Set).
      Type Parameters:
      T - the object type
      Parameters:
      violations - the set of constraint violations to check
    • checkArgumentNoViolations

      public static <T> void checkArgumentNoViolations(Set<jakarta.validation.ConstraintViolation<T>> violations, String errorMessage)
      Ensures the set of constraint violations is empty, throwing an IllegalArgumentException otherwise.
      Type Parameters:
      T - the object type
      Parameters:
      violations - the set of constraint violations to check
      errorMessage - the error message for the exception
    • checkArgumentNoViolations

      public static <T> void checkArgumentNoViolations(Set<jakarta.validation.ConstraintViolation<T>> violations, String errorMessageTemplate, Object... errorMessageArgs)
      Ensures the set of constraint violations is empty, throwing an IllegalArgumentException otherwise.
      Type Parameters:
      T - the object type
      Parameters:
      violations - the set of constraint violations to check
      errorMessageTemplate - a template for the exception message should the check fail, according to how KiwiStrings.format(String, Object...) handles placeholders
      errorMessageArgs - the arguments to be substituted into the message template. Arguments are converted to Strings using String.valueOf(Object).
    • checkArgumentNoViolations

      public static <T> void checkArgumentNoViolations(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<Set<jakarta.validation.ConstraintViolation<T>>,String> errorMessageCreator)
      Ensures the set of constraint violations is empty, throwing an IllegalArgumentException otherwise.
      Type Parameters:
      T - the object type
      Parameters:
      violations - the set of constraint violations to check
      errorMessageCreator - a Function that transforms constraint violations into an error message for the exception
    • addError

      public static void addError(jakarta.validation.ConstraintValidatorContext context, String template)
      Adds an error to the ConstraintValidatorContext using the specified template, thereby overriding the constraint's default message.

      NOTE: As of Hibernate Validator 6.2 and higher, expression language (EL) is disabled by default for custom violations. This means custom validator error messages that use EL will show the un-interpolated value in the message template, unless the custom validator has explicitly enabled EL. For example, given a template: "'${validatedValue}' is not a valid ACME, Inc. product code.", the error message will just be the template itself, so the user will see the literal ${validatedValue} instead of the value that was validated. See this discussion for additional information, and specifically see the References.

      Parameters:
      context - the validator context
      template - the template to use
      See Also:
      • ConstraintValidatorContext.buildConstraintViolationWithTemplate(String)
    • addError

      public static void addError(jakarta.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.

      NOTE: Please see the note in addError(ConstraintValidatorContext, String) for important information regarding expression language (EL) being disabled by default in Hibernate Validator 6.2 and higher for custom violations.

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

      public static @Nullable 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, bean is null, or fieldName is blank
      Implementation Note:
      This uses KiwiReflection.findField(Object, String) to obtain the value.