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> 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> 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> 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.
        Type Parameters:
        T - type of object being validated
        Parameters:
        violations - set of non-empty violations set of non-empty violations
        Returns:
        the combined error message
        Throws:
        IllegalArgumentException - if violations is null or empty
      • 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.
        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
      • 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.
        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
      • 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".
        Type Parameters:
        T - type of object being validated
        Parameters:
        violations - set of non-empty violations
        Returns:
        a list of the error messages
        Throws:
        IllegalArgumentException - if violations is null or empty
      • 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".
        Type Parameters:
        T - type of object being validated
        Parameters:
        violations - set of non-empty violations
        Returns:
        a list of the error messages
        Throws:
        IllegalArgumentException - if violations is null or empty
      • 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.
        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
        Throws:
        IllegalArgumentException - if violations is null or empty
      • 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.
        Type Parameters:
        T - type of object being validated
        Parameters:
        violations - set of non-empty violations
        Returns:
        a map of error messages
        Throws:
        IllegalArgumentException - if violations is null or empty
      • 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.
        Type Parameters:
        T - type of object being validated
        Parameters:
        violations - set of non-empty violations
        Returns:
        a map of error messages
        Throws:
        IllegalArgumentException - if violations is null or empty
      • 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.
        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
        Throws:
        IllegalArgumentException - if violations is null or empty
      • 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