Class KiwiConstraintViolations
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 TypeMethodDescriptionConvert the set ofConstraintViolationto an unmodifiable map keyed by the property path.asMap(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path, String> pathTransformer) Convert the set ofConstraintViolationto 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 ofConstraintViolationto an unmodifiableMultimapkeyed 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 ofConstraintViolationto an unmodifiableMultimapkeyed by the property path.asMultiValuedMap(Set<jakarta.validation.ConstraintViolation<T>> violations) Convert the set ofConstraintViolationto an unmodifiable map keyed by the property path.asMultiValuedMap(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path, String> pathTransformer) Convert the set ofConstraintViolationto an unmodifiable map keyed by the property path.asSingleValuedMap(Set<jakarta.validation.ConstraintViolation<T>> violations) Convert the set ofConstraintViolationto an unmodifiable map keyed by the property path.asSingleValuedMap(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<jakarta.validation.Path, String> pathTransformer) Convert the set ofConstraintViolationto an unmodifiable map keyed by the property path.static <T> StringcombinedErrorMessage(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.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> StringcombinedErrorMessageOrNull(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.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.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 Stringhumanize(jakarta.validation.Path propertyPath) Transforms the given property path into a human-readable version.static StringTransforms the give property path into a human-readable version.static <T> StringpathStringOf(jakarta.validation.ConstraintViolation<T> violation) Convenience method to get the property path of theConstraintViolationas a String.static <T> StringprettyCombinedErrorMessage(Set<jakarta.validation.ConstraintViolation<T>> violations) Given a non-empty set of violations, produce a single string containing all violation messages separated by commas.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> StringprettyCombinedErrorMessageOrNull(Set<jakarta.validation.ConstraintViolation<T>> violations) Given a non-empty set of violations, produce a single string containing all violation messages separated by commas.prettyCombinedErrorMessages(Set<jakarta.validation.ConstraintViolation<T>> violations) Given a non-empty set of violations, produce a list of strings containing all violation messages.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> StringsimpleCombinedErrorMessage(Set<jakarta.validation.ConstraintViolation<T>> violations) Given a non-empty set of violations, produce a single string containing all violation messages separated by commas.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> StringsimpleCombinedErrorMessageOrNull(Set<jakarta.validation.ConstraintViolation<T>> violations) Given a set of non-empty violations, produce a single string containing all violation messages separated by commas.simpleCombinedErrorMessages(Set<jakarta.validation.ConstraintViolation<T>> violations) Given a non-empty set of violations, produce a list of strings containing all violation messages.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.
-
Method Details
-
asMap
public static <T> Map<String,jakarta.validation.ConstraintViolation<T>> asMap(Set<jakarta.validation.ConstraintViolation<T>> violations) Convert the set ofConstraintViolationto an unmodifiable map keyed by the property path.The map's values are the single
ConstraintViolationassociated with each property.WARNING: An
IllegalStateExceptionis 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 eitherasMultiValuedMap(Set)orasSingleValuedMap(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 ofConstraintViolationto an unmodifiable map keyed by the property path. The property path is determined by thepathTransformer.The map's values are the single
ConstraintViolationassociated with each property.WARNING: An
IllegalStateExceptionis 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 eitherasMultiValuedMap(Set)orasSingleValuedMap(Set).- Type Parameters:
T- the type of the root bean that was validated- Parameters:
violations- set of non-null but possibly empty violationspathTransformer- 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 ofConstraintViolationto an unmodifiable map keyed by the property path.The map's values are the last
ConstraintViolationassociated 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 ofConstraintViolationto an unmodifiable map keyed by the property path. The property path is determined by thepathTransformer.The map's values are the last
ConstraintViolationassociated 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 violationspathTransformer- 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 ofConstraintViolationto an unmodifiable map keyed by the property path.The map's values are the set of
ConstraintViolationassociated 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 ofConstraintViolationto an unmodifiable map keyed by the property path. The property path is determined by thepathTransformer.The map's values are unmodifiable sets of
ConstraintViolationassociated with each property.- Type Parameters:
T- the type of the root bean that was validated- Parameters:
violations- set of non-null but possibly empty violationspathTransformer- 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 ofConstraintViolationto an unmodifiableMultimapkeyed 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
Multimapwhose 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 ofConstraintViolationto an unmodifiableMultimapkeyed 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 violationspathTransformer- function to convert a Path into a String- Returns:
- a
Multimapwhose 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
Convenience method to get the property path of theConstraintViolationas 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 aPathand then callsPath.toString()to get the final value. Therefore, the issues onPath.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 thatHintcontains a String property namedtext. 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 ispasswordHints[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 convertingcamelCaseto sentence case, for examplefirstNamebecomes "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
camelCaseto sentence case, for examplefirstNamebecomes "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 specifiedpathTransformerfunction. 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 violationspathTransformer- 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
pathTransformerfunction.- Type Parameters:
T- type of object being validated- Parameters:
violations- set of violationspathTransformer- 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
pathTransformerfunction.- Type Parameters:
T- type of object being validated- Parameters:
violations- set of violationspathTransformer- 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 thefirstNameproperty, 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 specifiedpathTransformerfunction. 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 violationspathTransformer- 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 specifiedpathTransformerfunction. 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 violationspathTransformer- function to convert a Path into a String- Returns:
- a map of error messages
-
humanize
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 aConstraintViolation- Returns:
- a human-readable path
- Throws:
IllegalArgumentException- if either argument is null
-
humanize
Transforms the give property path into a human-readable version. Nested paths are separated by the givenpathSeparator.For example, contactInfo.email.address using ":" as the path separator would result in Contact Info:Email:Address.
- Parameters:
propertyPath- the property path from aConstraintViolationpathSeparator- the separator to use between path elements- Returns:
- a human-readable path
- Throws:
IllegalArgumentException- if either argument is null
-