Class KiwiValidations
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.
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidAdds an error to theConstraintValidatorContextusing the specified template, thereby overriding the constraint's default message.static voidaddError(jakarta.validation.ConstraintValidatorContext context, String template, String propertyName) Adds an error to theConstraintValidatorContextusing the specified template and property name, thereby overriding the constraint's default message.static <T> voidcheckArgumentNoViolations(Set<jakarta.validation.ConstraintViolation<T>> violations) Ensures the set of constraint violations is empty, throwing anIllegalArgumentExceptionotherwise.static <T> voidcheckArgumentNoViolations(Set<jakarta.validation.ConstraintViolation<T>> violations, String errorMessage) Ensures the set of constraint violations is empty, throwing anIllegalArgumentExceptionotherwise.static <T> voidcheckArgumentNoViolations(Set<jakarta.validation.ConstraintViolation<T>> violations, String errorMessageTemplate, Object... errorMessageArgs) Ensures the set of constraint violations is empty, throwing anIllegalArgumentExceptionotherwise.static <T> voidcheckArgumentNoViolations(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<Set<jakarta.validation.ConstraintViolation<T>>, String> errorMessageCreator) Ensures the set of constraint violations is empty, throwing anIllegalArgumentExceptionotherwise.static <T> voidcheckArgumentValid(T object) Validate the given object using the singleton validator instance against theDefaultgroup.static <T> voidcheckArgumentValid(T object, Class<?>... groupClasses) Validate the given object using the singleton validator instance against the specified validation groups.static <T> voidcheckArgumentValid(T object, String errorMessage) Validate the given object using the singleton validator instance against theDefaultgroup.static <T> voidcheckArgumentValid(T object, String errorMessage, Class<?>... groupClasses) Validate the given object using the singleton validator instance against the specified validation groups.static <T> voidcheckArgumentValid(T object, String errorMessageTemplate, Object... errorMessageArgs) Validate the given object using the singleton validator instance against theDefaultgroup.static <T> voidcheckArgumentValid(T object, String errorMessageTemplate, List<Object> errorMessageArgs, Class<?>... groupClasses) Validate the given object using the singleton validator instance against the specified validation groups.static <T> voidcheckArgumentValid(T object, Function<Set<jakarta.validation.ConstraintViolation<T>>, String> errorMessageCreator) Validate the given object using the singleton validator instance against theDefaultgroup.static <T> voidcheckArgumentValid(T object, Function<Set<jakarta.validation.ConstraintViolation<T>>, String> errorMessageCreator, Class<?>... groupClasses) Validate the given object using the singleton validator instance against the specified validation groups.static @Nullable ObjectgetPropertyValue(Object bean, String fieldName) Finds the value of the specified property by direct field access.static jakarta.validation.ValidatorReturn the re-usable (singleton) Validator instance.static jakarta.validation.ValidatorCreates a new, defaultValidatorinstance using the default validator factory provided by the underlying bean validation implementation, for example Hibernate Valdiator.static voidsetValidator(jakarta.validation.Validator newValidator) Reset the singleton Validator instance.static <T> voidthrowConstraintViolationExceptionIfNotEmpty(Set<jakarta.validation.ConstraintViolation<T>> violations) If the set of constraint violations is not empty, throw aConstraintViolationException.static <T> Set<jakarta.validation.ConstraintViolation<T>>validate(T object) Validate the given object using the singleton validator instance against theDefaultgroup.static <T> Set<jakarta.validation.ConstraintViolation<T>>Validate the given object using the singleton validator instance against the specified validation groups.static <T> voidvalidateThrowing(T object) Validate the given object using the singleton validator instance against theDefaultgroup, and throw aConstraintViolationExceptionif validation fails.static <T> voidvalidateThrowing(T object, Class<?>... groupClasses) Validate the given object using the singleton validator instance against the specified validation groups, and throw aConstraintViolationExceptionif validation fails.
-
Method Details
-
newValidator
public static jakarta.validation.Validator newValidator()Creates a new, defaultValidatorinstance using the default validator factory provided by the underlying bean validation implementation, for example Hibernate Valdiator.- Returns:
- a new
Validatorinstance
-
getValidator
public static jakarta.validation.Validator getValidator()Return the re-usable (singleton) Validator instance.- Returns:
- singleton
Validatorinstance
-
setValidator
public static void setValidator(jakarta.validation.Validator newValidator) Reset the singleton Validator instance.This is intended primarily for use by unit tests, to permit resetting the Validator. Use with caution and remember: with great power, come great responsibility.
- Parameters:
newValidator- the new Validator to use as the singleton instance- Implementation Note:
- This method is intentionally not synchronized. Since it is expected only to be used once at application startup, and during unit tests, we can skip adding synchronization to this and all other methods that use the singleton Validator instance. In other words, because we assume this will not be called in real production code, except perhaps one time at application startup, we don't think it's worth adding locking or synchronization on every method that uses the singleton Validator instance.
-
validate
Validate the given object using the singleton validator instance against theDefaultgroup.- Type Parameters:
T- the object type- Parameters:
object- the object to validate- Returns:
- validation results
-
validate
public static <T> Set<jakarta.validation.ConstraintViolation<T>> validate(T object, Class<?>... groupClasses) Validate the given object using the singleton validator instance against the specified validation groups.- Type Parameters:
T- the object type- Parameters:
object- the object to validategroupClasses- zero or more validation group classes- Returns:
- validation results
-
validateThrowing
public static <T> void validateThrowing(T object) Validate the given object using the singleton validator instance against theDefaultgroup, and throw aConstraintViolationExceptionif validation fails.- Type Parameters:
T- the object type- Parameters:
object- the object to validate
-
validateThrowing
Validate the given object using the singleton validator instance against the specified validation groups, and throw aConstraintViolationExceptionif validation fails.- Type Parameters:
T- the object type- Parameters:
object- the object to validategroupClasses- zero or more validation group classes
-
throwConstraintViolationExceptionIfNotEmpty
public static <T> void throwConstraintViolationExceptionIfNotEmpty(Set<jakarta.validation.ConstraintViolation<T>> violations) If the set of constraint violations is not empty, throw aConstraintViolationException.- Type Parameters:
T- the object type- Parameters:
violations- the constraint violations
-
checkArgumentValid
public static <T> void checkArgumentValid(T object) Validate the given object using the singleton validator instance against theDefaultgroup. If the argument is not valid, throws anIllegalArgumentException. The exception message is supplied bycheckArgumentNoViolations(Set).- Type Parameters:
T- the object type- Parameters:
object- the object to validate- See Also:
-
checkArgumentValid
Validate the given object using the singleton validator instance against theDefaultgroup. If the argument is not valid, throws anIllegalArgumentException.- Type Parameters:
T- the object type- Parameters:
object- the object to validateerrorMessage- the error message for the exception
-
checkArgumentValid
public static <T> void checkArgumentValid(T object, String errorMessageTemplate, Object... errorMessageArgs) Validate the given object using the singleton validator instance against theDefaultgroup. If the argument is not valid, throws anIllegalArgumentException.- Type Parameters:
T- the object type- Parameters:
object- the object to validateerrorMessageTemplate- a template for the exception message should the check fail, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to be substituted into the message template. Arguments are converted to Strings usingString.valueOf(Object).
-
checkArgumentValid
public static <T> void checkArgumentValid(T object, Function<Set<jakarta.validation.ConstraintViolation<T>>, String> errorMessageCreator) Validate the given object using the singleton validator instance against theDefaultgroup. If the argument is not valid, throws anIllegalArgumentException.- Type Parameters:
T- the object type- Parameters:
object- the object to validateerrorMessageCreator- a Function that transforms constraint violations into an error message for the exception
-
checkArgumentValid
Validate the given object using the singleton validator instance against the specified validation groups. If the argument is not valid, throws anIllegalArgumentException. The exception message is supplied bycheckArgumentNoViolations(Set).- Type Parameters:
T- the object type- Parameters:
object- the object to validategroupClasses- zero or more validation group classes- See Also:
-
checkArgumentValid
Validate the given object using the singleton validator instance against the specified validation groups. If the argument is not valid, throws anIllegalArgumentException.- Type Parameters:
T- the object type- Parameters:
object- the object to validateerrorMessage- the error message for the exceptiongroupClasses- zero or more validation group classes
-
checkArgumentValid
public static <T> void checkArgumentValid(T object, String errorMessageTemplate, List<Object> errorMessageArgs, Class<?>... groupClasses) Validate the given object using the singleton validator instance against the specified validation groups. If the argument is not valid, throws anIllegalArgumentException.- Type Parameters:
T- the object type- Parameters:
object- the object to validateerrorMessageTemplate- a template for the exception message should the check fail, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to be substituted into the message template. Arguments are converted to Strings usingString.valueOf(Object).groupClasses- zero or more validation group classes
-
checkArgumentValid
public static <T> void checkArgumentValid(T object, Function<Set<jakarta.validation.ConstraintViolation<T>>, String> errorMessageCreator, Class<?>... groupClasses) Validate the given object using the singleton validator instance against the specified validation groups. If the argument is not valid, throws anIllegalArgumentException.- Type Parameters:
T- the object type- Parameters:
object- the object to validateerrorMessageCreator- a Function that transforms constraint violations into an error message for the exceptiongroupClasses- zero or more validation group classes
-
checkArgumentNoViolations
public static <T> void checkArgumentNoViolations(Set<jakarta.validation.ConstraintViolation<T>> violations) Ensures the set of constraint violations is empty, throwing anIllegalArgumentExceptionotherwise. The exception message is supplied byKiwiConstraintViolations.simpleCombinedErrorMessageOrNull(Set).- Type Parameters:
T- the object type- Parameters:
violations- the set of constraint violations to check
-
checkArgumentNoViolations
public static <T> void checkArgumentNoViolations(Set<jakarta.validation.ConstraintViolation<T>> violations, String errorMessage) Ensures the set of constraint violations is empty, throwing anIllegalArgumentExceptionotherwise.- Type Parameters:
T- the object type- Parameters:
violations- the set of constraint violations to checkerrorMessage- the error message for the exception
-
checkArgumentNoViolations
public static <T> void checkArgumentNoViolations(Set<jakarta.validation.ConstraintViolation<T>> violations, String errorMessageTemplate, Object... errorMessageArgs) Ensures the set of constraint violations is empty, throwing anIllegalArgumentExceptionotherwise.- Type Parameters:
T- the object type- Parameters:
violations- the set of constraint violations to checkerrorMessageTemplate- a template for the exception message should the check fail, according to howKiwiStrings.format(String, Object...)handles placeholderserrorMessageArgs- the arguments to be substituted into the message template. Arguments are converted to Strings usingString.valueOf(Object).
-
checkArgumentNoViolations
public static <T> void checkArgumentNoViolations(Set<jakarta.validation.ConstraintViolation<T>> violations, Function<Set<jakarta.validation.ConstraintViolation<T>>, String> errorMessageCreator) Ensures the set of constraint violations is empty, throwing anIllegalArgumentExceptionotherwise.- Type Parameters:
T- the object type- Parameters:
violations- the set of constraint violations to checkerrorMessageCreator- a Function that transforms constraint violations into an error message for the exception
-
addError
Adds an error to theConstraintValidatorContextusing the specified template, thereby overriding the constraint's default message.NOTE: As of Hibernate Validator 6.2 and higher, expression language (EL) is disabled by default for custom violations. This means custom validator error messages that use EL will show the un-interpolated value in the message template, unless the custom validator has explicitly enabled EL. For example, given a template:
"'${validatedValue}' is not a valid ACME, Inc. product code.", the error message will just be the template itself, so the user will see the literal${validatedValue}instead of the value that was validated. See this discussion for additional information, and specifically see the References.- Parameters:
context- the validator contexttemplate- the template to use- See Also:
-
ConstraintValidatorContext.buildConstraintViolationWithTemplate(String)
-
addError
public static void addError(jakarta.validation.ConstraintValidatorContext context, String template, String propertyName) Adds an error to theConstraintValidatorContextusing the specified template and property name, thereby overriding the constraint's default message.This is intended to be used when a validation annotation applies at the type level, as opposed to a field or method. The
propertyNamecan be used to specify the property node that the violation should be attached to. For example, if a validator validates multiple fields and the annotation is applied to the type, then this can be used to specify which field a constraint violation applies to.NOTE: Please see the note in
addError(ConstraintValidatorContext, String)for important information regarding expression language (EL) being disabled by default in Hibernate Validator 6.2 and higher for custom violations.- Parameters:
context- the validator contexttemplate- the template to usepropertyName- the property name to attach constrain violations to
-
getPropertyValue
Finds the value of the specified property by direct field access.This is provided for validators to obtain the value of a specific field, and will only be useful when for validation annotations that are comparing multiple fields. For example, an annotation that validates a range of values between two fields in an object.
- Parameters:
bean- the objectfieldName- the property/field name- Returns:
- the value of the property in the object, or null if any problem occurs,
beanis null, orfieldNameis blank - Implementation Note:
- This uses
KiwiReflection.findField(Object, String)to obtain the value.
-