Class KiwiConstraintViolations

java.lang.Object
org.kiwiproject.validation.KiwiConstraintViolations

public final class KiwiConstraintViolations extends Object
Static utilities for working with ConstraintViolation objects, generally Sets of them.

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.

In addition, currently the "pretty" methods use the #humanize methods, which rely on WordUtils from commons-text. So if you use any of these, you will need to ensure org.apache.commons:commons-text is available at runtime.

  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Map<String,jakarta.validation.ConstraintViolation<T>>
    asMap(Set<jakarta.validation.ConstraintViolation<T>> violations)
    Convert the set of ConstraintViolation to an unmodifiable map keyed by the property path.
    static <T> Map<String,jakarta.validation.ConstraintViolation<T>>
    asMap(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
    Convert the set of ConstraintViolation to an unmodifiable map keyed by the property path.
    static <T> com.google.common.collect.Multimap<String,jakarta.validation.ConstraintViolation<T>>
    asMultimap(Set<jakarta.validation.ConstraintViolation<T>> violations)
    Convert the set of ConstraintViolation to an unmodifiable Multimap keyed by the property path.
    static <T> com.google.common.collect.Multimap<String,jakarta.validation.ConstraintViolation<T>>
    asMultimap(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
    Convert the set of ConstraintViolation to an unmodifiable Multimap keyed by the property path.
    static <T> Map<String,Set<jakarta.validation.ConstraintViolation<T>>>
    asMultiValuedMap(Set<jakarta.validation.ConstraintViolation<T>> violations)
    Convert the set of ConstraintViolation to an unmodifiable map keyed by the property path.
    static <T> Map<String,Set<jakarta.validation.ConstraintViolation<T>>>
    asMultiValuedMap(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
    Convert the set of ConstraintViolation to an unmodifiable map keyed by the property path.
    static <T> Map<String,jakarta.validation.ConstraintViolation<T>>
    asSingleValuedMap(Set<jakarta.validation.ConstraintViolation<T>> violations)
    Convert the set of ConstraintViolation to an unmodifiable map keyed by the property path.
    static <T> Map<String,jakarta.validation.ConstraintViolation<T>>
    asSingleValuedMap(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
    Convert the set of ConstraintViolation to an unmodifiable map keyed by the property path.
    static <T> String
    combinedErrorMessage(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
    Given a non-empty set of violations, produce a single string containing all violation messages separated by commas.
    static <T> Optional<String>
    combinedErrorMessageOrEmpty(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
    Given a non-empty set of violations, produce a single string containing all violation messages separated by commas.
    static <T> String
    combinedErrorMessageOrNull(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
    Given a non-empty set of violations, produce a single string containing all violation messages separated by commas.
    static <T> List<String>
    combinedErrorMessages(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
    Given a non-empty set of violations, produce a list of strings containing all violation messages.
    static <T> Map<String,String>
    combineErrorMessagesIntoMap(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
    Given a non-empty set of violations, produce map whose keys are the transformed properties and the corresponding values are strings containing all violation messages.
    static String
    humanize(jakarta.validation.Path propertyPath)
    Transforms the given property path into a human-readable version.
    static String
    humanize(jakarta.validation.Path propertyPath, String pathSeparator)
    Transforms the give property path into a human-readable version.
    static <T> String
    pathStringOf(jakarta.validation.ConstraintViolation<T> violation)
    Convenience method to get the property path of the ConstraintViolation as a String.
    static <T> String
    prettyCombinedErrorMessage(Set<jakarta.validation.ConstraintViolation<T>> violations)
    Given a non-empty set of violations, produce a single string containing all violation messages separated by commas.
    static <T> Optional<String>
    prettyCombinedErrorMessageOrEmpty(Set<jakarta.validation.ConstraintViolation<T>> violations)
    Given a non-empty set of violations, produce a single string containing all violation messages separated by commas.
    static <T> String
    prettyCombinedErrorMessageOrNull(Set<jakarta.validation.ConstraintViolation<T>> violations)
    Given a non-empty set of violations, produce a single string containing all violation messages separated by commas.
    static <T> List<String>
    prettyCombinedErrorMessages(Set<jakarta.validation.ConstraintViolation<T>> violations)
    Given a non-empty set of violations, produce a list of strings containing all violation messages.
    static <T> Map<String,String>
    prettyCombineErrorMessagesIntoMap(Set<jakarta.validation.ConstraintViolation<T>> violations)
    Given a non-empty set of violations, produce map whose keys are the "prettified" properties and the corresponding values are strings containing all violation messages.
    static <T> String
    simpleCombinedErrorMessage(Set<jakarta.validation.ConstraintViolation<T>> violations)
    Given a non-empty set of violations, produce a single string containing all violation messages separated by commas.
    static <T> Optional<String>
    simpleCombinedErrorMessageOrEmpty(Set<jakarta.validation.ConstraintViolation<T>> violations)
    Given a set of non-empty violations, produce a single string containing all violation messages separated by commas.
    static <T> String
    simpleCombinedErrorMessageOrNull(Set<jakarta.validation.ConstraintViolation<T>> violations)
    Given a set of non-empty violations, produce a single string containing all violation messages separated by commas.
    static <T> List<String>
    simpleCombinedErrorMessages(Set<jakarta.validation.ConstraintViolation<T>> violations)
    Given a non-empty set of violations, produce a list of strings containing all violation messages.
    static <T> Map<String,String>
    simpleCombineErrorMessagesIntoMap(Set<jakarta.validation.ConstraintViolation<T>> violations)
    Given a non-empty set of violations, produce map whose keys are the properties and the corresponding values are strings containing all violation messages.

    Methods inherited from class java.lang.Object

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

    • asMap

      public static <T> Map<String,jakarta.validation.ConstraintViolation<T>> asMap(Set<jakarta.validation.ConstraintViolation<T>> violations)
      Convert the set of ConstraintViolation to an unmodifiable map keyed by the property path.

      The map's values are the single ConstraintViolation associated with each property.

      WARNING: An IllegalStateException is thrown if there is more than one violation associated with any key. Therefore, this method should only be used if you are sure there can only be at most one violation per property. Otherwise, use either asMultiValuedMap(Set) or asSingleValuedMap(Set).

      Type Parameters:
      T - the type of the root bean that was validated
      Parameters:
      violations - set of non-null but possibly empty violations
      Returns:
      a map whose keys are the property path of the violations, and values are the violations
      Throws:
      IllegalStateException - if there is more than one violation associated with any key
      See Also:
    • asMap

      public static <T> Map<String,jakarta.validation.ConstraintViolation<T>> asMap(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
      Convert the set of ConstraintViolation to an unmodifiable map keyed by the property path. The property path is determined by the pathTransformer.

      The map's values are the single ConstraintViolation associated with each property.

      WARNING: An IllegalStateException is thrown if there is more than one violation associated with any key. Therefore, this method should only be used if you are sure there can only be at most one violation per property. Otherwise, use either asMultiValuedMap(Set) or asSingleValuedMap(Set).

      Type Parameters:
      T - the type of the root bean that was validated
      Parameters:
      violations - set of non-null but possibly empty violations
      pathTransformer - function to convert a Path into a String
      Returns:
      a map whose keys are the property path of the violations, and values are the violations
      Throws:
      IllegalStateException - if there is more than one violation associated with any key
      See Also:
    • asSingleValuedMap

      public static <T> Map<String,jakarta.validation.ConstraintViolation<T>> asSingleValuedMap(Set<jakarta.validation.ConstraintViolation<T>> violations)
      Convert the set of ConstraintViolation to an unmodifiable map keyed by the property path.

      The map's values are the last ConstraintViolation associated with each property. The definition of "last" depends on the iteration order of the provided set of violations, which may be non-deterministic if the set does not have a well-defined traversal order.

      WARNING: If there is more than one violation associated with any key, the last violation, as determined by the set traversal order, becomes they key. If you need to retain all violations associated with each key, use asMultiValuedMap(Set).

      Type Parameters:
      T - the type of the root bean that was validated
      Parameters:
      violations - set of non-null but possibly empty violations
      Returns:
      a map whose keys are the property path of the violations, and values are the violations
      See Also:
    • asSingleValuedMap

      public static <T> Map<String,jakarta.validation.ConstraintViolation<T>> asSingleValuedMap(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
      Convert the set of ConstraintViolation to an unmodifiable map keyed by the property path. The property path is determined by the pathTransformer.

      The map's values are the last ConstraintViolation associated with each property. The definition of "last" depends on the iteration order of the provided set of violations, which may be non-deterministic if the set does not have a well-defined traversal order.

      WARNING: If there is more than one violation associated with any key, the last violation, as determined by the set traversal order, becomes they key. If you need to retain all violations associated with each key, use asMultiValuedMap(Set).

      Type Parameters:
      T - the type of the root bean that was validated
      Parameters:
      violations - set of non-null but possibly empty violations
      pathTransformer - function to convert a Path into a String
      Returns:
      a map whose keys are the property path of the violations, and values are the violations
      See Also:
    • asMultiValuedMap

      public static <T> Map<String,Set<jakarta.validation.ConstraintViolation<T>>> asMultiValuedMap(Set<jakarta.validation.ConstraintViolation<T>> violations)
      Convert the set of ConstraintViolation to an unmodifiable map keyed by the property path.

      The map's values are the set of ConstraintViolation associated with each property.

      Type Parameters:
      T - the type of the root bean that was validated
      Parameters:
      violations - set of non-null but possibly empty violations
      Returns:
      a map whose keys are the property path of the violations, and values are a Set containing violations for the corresponding property
    • asMultiValuedMap

      public static <T> Map<String,Set<jakarta.validation.ConstraintViolation<T>>> asMultiValuedMap(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
      Convert the set of ConstraintViolation to an unmodifiable map keyed by the property path. The property path is determined by the pathTransformer.

      The map's values are unmodifiable sets of ConstraintViolation associated with each property.

      Type Parameters:
      T - the type of the root bean that was validated
      Parameters:
      violations - set of non-null but possibly empty violations
      pathTransformer - function to convert a Path into a String
      Returns:
      a map whose keys are the property path of the violations, and values are a Set containing violations for the corresponding property
    • asMultimap

      public static <T> com.google.common.collect.Multimap<String,jakarta.validation.ConstraintViolation<T>> asMultimap(Set<jakarta.validation.ConstraintViolation<T>> violations)
      Convert the set of ConstraintViolation to an unmodifiable Multimap keyed by the property path.
      Type Parameters:
      T - the type of the root bean that was validated
      Parameters:
      violations - set of non-null but possibly empty violations
      Returns:
      a Multimap whose keys are the property path of the violations, and values contain the violations for the corresponding property
      Implementation Note:
      The returned value is a LinkedHashMultimap; the iteration order of the values for each key is always the order in which the values were added, and there cannot be duplicate values for a key.
    • asMultimap

      public static <T> com.google.common.collect.Multimap<String,jakarta.validation.ConstraintViolation<T>> asMultimap(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
      Convert the set of ConstraintViolation to an unmodifiable Multimap keyed by the property path.
      Type Parameters:
      T - the type of the root bean that was validated
      Parameters:
      violations - set of non-null but possibly empty violations
      pathTransformer - function to convert a Path into a String
      Returns:
      a Multimap whose keys are the property path of the violations, and values contain the violations for the corresponding property
      Implementation Note:
      The returned value is a LinkedHashMultimap; the iteration order of the values for each key is always the order in which the values were added, and there cannot be duplicate values for a key.
    • pathStringOf

      public static <T> String pathStringOf(jakarta.validation.ConstraintViolation<T> violation)
      Convenience method to get the property path of the ConstraintViolation as a String.

      Please refer to the Implementation Note for details on the structure of the returned values and warnings about that structure.

      Type Parameters:
      T - the type of the root bean that was validated
      Parameters:
      violation - the constraint violation
      Returns:
      the property path of the violation, as a String
      Implementation Note:
      This uses ConstraintViolation.getPropertyPath() to obtain a Path and then calls Path.toString() to get the final value. Therefore, the issues on Path.toString() with regard to the structure of the return value apply here as well. However, in many years of usage, the implementation (in Hibernate Validator anyway) has always returned the same expected result, and is generally what you expect.

      The main exception is iterable types, such as Set, that don't have a consistent traversal order. For example, if you have a property named "nicknames" declared as Set<@NotBlank String> nicknames, the property path for violation errors look like "nicknames[].<iterable element>".

      Maps look similar to Sets. For example, in the Hibernate Validator reference documentation, one example shows the property path of a constraint violation on a Map as "fuelConsumption[HIGHWAY].<map value>", and similarly on a Map value as "fuelConsumption<K>[].<map key>".

      Indexed properties such as a List look more reasonable. For example, suppose a property named "passwordHints" is declared as List<@NotNull @Valid Hint> passwordHints, and that Hint contains a String property named text. The property path for violation errors includes the zero-based index as well as the path. For example, if the second password hint is not valid, the property path is passwordHints[1].text.

    • simpleCombinedErrorMessage

      public static <T> String simpleCombinedErrorMessage(Set<jakarta.validation.ConstraintViolation<T>> violations)
      Given a non-empty set of violations, produce a single string containing all violation messages separated by commas. If the given set is empty (or null), then throw IllegalArgumentException.
      Type Parameters:
      T - type of object being validated
      Parameters:
      violations - set of non-empty violations
      Returns:
      the combined error message
      Throws:
      IllegalArgumentException - if violations is null or empty
    • simpleCombinedErrorMessageOrNull

      public static <T> String simpleCombinedErrorMessageOrNull(Set<jakarta.validation.ConstraintViolation<T>> violations)
      Given a set of non-empty violations, produce a single string containing all violation messages separated by commas. If the given set is empty (or null), then return null.
      Type Parameters:
      T - type of object being validated
      Parameters:
      violations - set of violations
      Returns:
      the combined error message, or null
    • simpleCombinedErrorMessageOrEmpty

      public static <T> Optional<String> simpleCombinedErrorMessageOrEmpty(Set<jakarta.validation.ConstraintViolation<T>> violations)
      Given a set of non-empty violations, produce a single string containing all violation messages separated by commas. If the given set is empty (or null), then return an empty Optional.
      Type Parameters:
      T - type of object being validated
      Parameters:
      violations - set of violations
      Returns:
      the combined error message, or en empty Optional
    • prettyCombinedErrorMessage

      public static <T> String prettyCombinedErrorMessage(Set<jakarta.validation.ConstraintViolation<T>> violations)
      Given a non-empty set of violations, produce a single string containing all violation messages separated by commas. Each property name is "prettified" by converting camelCase to sentence case, for example firstName becomes "First Name" in the resulting error message. If the given set is empty (or null), then throw IllegalArgumentException.
      Type Parameters:
      T - type of object being validated
      Parameters:
      violations - set of non-empty violations
      Returns:
      the combined error message
      Throws:
      IllegalArgumentException - if violations is null or empty
    • prettyCombinedErrorMessageOrNull

      public static <T> String prettyCombinedErrorMessageOrNull(Set<jakarta.validation.ConstraintViolation<T>> violations)
      Given a non-empty set of violations, produce a single string containing all violation messages separated by commas. If the given set is empty (or null), then return null.

      Each property name is "prettified" by converting camelCase to sentence case, for example firstName becomes "First Name" in the resulting error message.

      Type Parameters:
      T - type of object being validated
      Parameters:
      violations - set of violations
      Returns:
      the combined error message, or null
    • prettyCombinedErrorMessageOrEmpty

      public static <T> Optional<String> prettyCombinedErrorMessageOrEmpty(Set<jakarta.validation.ConstraintViolation<T>> violations)
      Given a non-empty set of violations, produce a single string containing all violation messages separated by commas. If the given set is empty (or null), then return an empty Optional.
      Type Parameters:
      T - type of object being validated
      Parameters:
      violations - set of violations
      Returns:
      the combined error message, or an empty Optional
    • combinedErrorMessage

      public static <T> String combinedErrorMessage(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
      Given a non-empty set of violations, produce a single string containing all violation messages separated by commas. Each property name is transformed using the specified pathTransformer function. If the given set is empty (or null), then throw IllegalArgumentException.
      Type Parameters:
      T - type of object being validated
      Parameters:
      violations - set of non-empty violations
      pathTransformer - function to convert a Path into a String
      Returns:
      the combined error message
      Throws:
      IllegalArgumentException - if violations is null or empty
    • combinedErrorMessageOrNull

      public static <T> String combinedErrorMessageOrNull(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
      Given a non-empty set of violations, produce a single string containing all violation messages separated by commas. If the given set is empty (or null), then return null.

      Each property name is transformed using the specified pathTransformer function.

      Type Parameters:
      T - type of object being validated
      Parameters:
      violations - set of violations
      pathTransformer - function to convert a Path into a String
      Returns:
      the combined error message, or null
    • combinedErrorMessageOrEmpty

      public static <T> Optional<String> combinedErrorMessageOrEmpty(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
      Given a non-empty set of violations, produce a single string containing all violation messages separated by commas. If the given set is empty (or null), then return an empty Optional.

      Each property name is transformed using the specified pathTransformer function.

      Type Parameters:
      T - type of object being validated
      Parameters:
      violations - set of violations
      pathTransformer - function to convert a Path into a String
      Returns:
      the combined error message, or an empty Optional
    • simpleCombinedErrorMessages

      public static <T> List<String> simpleCombinedErrorMessages(Set<jakarta.validation.ConstraintViolation<T>> violations)
      Given a non-empty set of violations, produce a list of strings containing all violation messages. Each message will contain the property followed by the error message, e.g. "firstName must not be blank". If the given set is empty (or null), then return an empty list.
      Type Parameters:
      T - type of object being validated
      Parameters:
      violations - set of non-empty violations
      Returns:
      a list of the error messages
    • prettyCombinedErrorMessages

      public static <T> List<String> prettyCombinedErrorMessages(Set<jakarta.validation.ConstraintViolation<T>> violations)
      Given a non-empty set of violations, produce a list of strings containing all violation messages. Each message will contain the "prettified" property name followed by the error message, e.g., for a violation on the firstName property, the message would look like "First Name must not be blank". If the given set is empty (or null), then return an empty list.
      Type Parameters:
      T - type of object being validated
      Parameters:
      violations - set of non-empty violations
      Returns:
      a list of the error messages
    • combinedErrorMessages

      public static <T> List<String> combinedErrorMessages(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
      Given a non-empty set of violations, produce a list of strings containing all violation messages. Each message will contain the transformed property name followed by the error message, e.g. "firstName must not be blank". Each property name is transformed using the specified pathTransformer function. If the given set is empty (or null), then return an empty list.
      Type Parameters:
      T - type of object being validated
      Parameters:
      violations - set of non-empty violations
      pathTransformer - function to convert a Path into a String
      Returns:
      a list of the error messages
    • simpleCombineErrorMessagesIntoMap

      public static <T> Map<String,String> simpleCombineErrorMessagesIntoMap(Set<jakarta.validation.ConstraintViolation<T>> violations)
      Given a non-empty set of violations, produce map whose keys are the properties and the corresponding values are strings containing all violation messages. If the given set is empty (or null), then return an empty map.
      Type Parameters:
      T - type of object being validated
      Parameters:
      violations - set of non-empty violations
      Returns:
      a map of error messages
    • prettyCombineErrorMessagesIntoMap

      public static <T> Map<String,String> prettyCombineErrorMessagesIntoMap(Set<jakarta.validation.ConstraintViolation<T>> violations)
      Given a non-empty set of violations, produce map whose keys are the "prettified" properties and the corresponding values are strings containing all violation messages. If the given set is empty (or null), then return an empty map.
      Type Parameters:
      T - type of object being validated
      Parameters:
      violations - set of non-empty violations
      Returns:
      a map of error messages
    • combineErrorMessagesIntoMap

      public static <T> Map<String,String> combineErrorMessagesIntoMap(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path,String> pathTransformer)
      Given a non-empty set of violations, produce map whose keys are the transformed properties and the corresponding values are strings containing all violation messages. Each property name is transformed using the specified pathTransformer function. If the given set is empty (or null), then return an empty map.
      Type Parameters:
      T - type of object being validated
      Parameters:
      violations - set of non-empty violations
      pathTransformer - function to convert a Path into a String
      Returns:
      a map of error messages
    • humanize

      public static String humanize(jakarta.validation.Path propertyPath)
      Transforms the given property path into a human-readable version. Nested paths are separated by a slash character. Examples:
      • age becomes Age
      • firstName becomes First Name
      • contactInfo.email.address becomes Contact Info / Email / Address
      Parameters:
      propertyPath - the property path from a ConstraintViolation
      Returns:
      a human-readable path
      Throws:
      IllegalArgumentException - if either argument is null
    • humanize

      public static String humanize(jakarta.validation.Path propertyPath, String pathSeparator)
      Transforms the give property path into a human-readable version. Nested paths are separated by the given pathSeparator.

      For example, contactInfo.email.address using ":" as the path separator would result in Contact Info:Email:Address.

      Parameters:
      propertyPath - the property path from a ConstraintViolation
      pathSeparator - the separator to use between path elements
      Returns:
      a human-readable path
      Throws:
      IllegalArgumentException - if either argument is null