Class KiwiConstraintViolations
- java.lang.Object
-
- org.kiwiproject.validation.KiwiConstraintViolations
-
public class KiwiConstraintViolations extends Object
Static utilities for working withConstraintViolationobjects, generallySets of them.Dependency requirements:
The
jakarta.validation:jakarta.validation-apidependency and some implementation such as Hibernate Validator (org.hibernate.validator:hibernate-validatormust be available at runtime.In addition, currently the "pretty" methods use the
#humanizemethods, which rely onWordUtilsfrom commons-text. So if you use any of these, you will need to ensureorg.apache.commons:commons-textis available at runtime.
-
-
Constructor Summary
Constructors Constructor Description KiwiConstraintViolations()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> StringcombinedErrorMessage(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> StringcombinedErrorMessageOrNull(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 Stringhumanize(javax.validation.Path propertyPath)Transforms the given property path into a human-readable version.static Stringhumanize(javax.validation.Path propertyPath, String pathSeparator)Transforms the give property path into a human-readable version.static <T> StringprettyCombinedErrorMessage(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> StringprettyCombinedErrorMessageOrNull(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> StringsimpleCombinedErrorMessage(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> StringsimpleCombinedErrorMessageOrNull(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.
-
-
-
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 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<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
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<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 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<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
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<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
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<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 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<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 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<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 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
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 aConstraintViolation- 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 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
-
-