Class KiwiConstraintViolations


  • public 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

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> String combinedErrorMessage​(Set<javax.validation.ConstraintViolation<T>> violations, Function<javax.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<javax.validation.ConstraintViolation<T>> violations, Function<javax.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<javax.validation.ConstraintViolation<T>> violations, Function<javax.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<javax.validation.ConstraintViolation<T>> violations, Function<javax.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<javax.validation.ConstraintViolation<T>> violations, Function<javax.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​(javax.validation.Path propertyPath)
      Transforms the given property path into a human-readable version.
      static String humanize​(javax.validation.Path propertyPath, String pathSeparator)
      Transforms the give property path into a human-readable version.
      static <T> String prettyCombinedErrorMessage​(Set<javax.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<javax.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<javax.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<javax.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<javax.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<javax.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<javax.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<javax.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<javax.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<javax.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.
    • Constructor Detail

      • KiwiConstraintViolations

        public KiwiConstraintViolations()
    • Method Detail

      • simpleCombinedErrorMessage

        public static <T> String simpleCombinedErrorMessage​(Set<javax.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<javax.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<javax.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<javax.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<javax.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<javax.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<javax.validation.ConstraintViolation<T>> violations,
                                                      Function<javax.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<javax.validation.ConstraintViolation<T>> violations,
                                                            Function<javax.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<javax.validation.ConstraintViolation<T>> violations,
                                                                       Function<javax.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<javax.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<javax.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<javax.validation.ConstraintViolation<T>> violations,
                                                             Function<javax.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<javax.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<javax.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<javax.validation.ConstraintViolation<T>> violations,
                                                                               Function<javax.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​(javax.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​(javax.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