cn.hutool.extra.validation.ValidationUtil Maven / Gradle / Ivy
package cn.hutool.extra.validation;
import cn.hutool.extra.validation.BeanValidationResult.ErrorMessage;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import java.util.Set;
/**
* java bean 校验工具类,此工具类基于validation-api(jakarta.validation-api)封装
*
* 在实际使用中,用户需引入validation-api的实现,如:hibernate-validator
* 注意:hibernate-validator还依赖了javax.el,需自行引入。
*
* @author chengqiang
* @since 5.5.0
*/
public class ValidationUtil {
/**
* 默认{@link Validator} 对象
*/
private static final Validator validator;
static {
try(ValidatorFactory factory = Validation.buildDefaultValidatorFactory()){
validator = factory.getValidator();
}
}
/**
* 获取原生{@link Validator} 对象
*
* @return {@link Validator} 对象
*/
public static Validator getValidator() {
return validator;
}
/**
* 校验对象
*
* @param Bean类型
* @param bean bean
* @param groups 校验组
* @return {@link Set}
*/
public static Set> validate(T bean, Class>... groups) {
return validator.validate(bean, groups);
}
/**
* 校验bean的某一个属性
*
* @param Bean类型
* @param bean bean
* @param propertyName 属性名称
* @param groups 验证分组
* @return {@link Set}
*/
public static Set> validateProperty(T bean, String propertyName, Class>... groups) {
return validator.validateProperty(bean, propertyName, groups);
}
/**
* 校验对象
*
* @param Bean类型
* @param bean bean
* @param groups 校验组
* @return {@link BeanValidationResult}
*/
public static BeanValidationResult warpValidate(T bean, Class>... groups) {
return warpBeanValidationResult(validate(bean, groups));
}
/**
* 校验bean的某一个属性
*
* @param bean类型
* @param bean bean
* @param propertyName 属性名称
* @param groups 验证分组
* @return {@link BeanValidationResult}
*/
public static BeanValidationResult warpValidateProperty(T bean, String propertyName, Class>... groups) {
return warpBeanValidationResult(validateProperty(bean, propertyName, groups));
}
/**
* 包装校验结果
*
* @param constraintViolations 校验结果集
* @return {@link BeanValidationResult}
*/
private static BeanValidationResult warpBeanValidationResult(Set> constraintViolations) {
BeanValidationResult result = new BeanValidationResult(constraintViolations.isEmpty());
for (ConstraintViolation constraintViolation : constraintViolations) {
ErrorMessage errorMessage = new ErrorMessage();
errorMessage.setPropertyName(constraintViolation.getPropertyPath().toString());
errorMessage.setMessage(constraintViolation.getMessage());
errorMessage.setValue(constraintViolation.getInvalidValue());
result.addErrorMessage(errorMessage);
}
return result;
}
}