mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
add ValidationUtil
This commit is contained in:
parent
db7d84807b
commit
f741f5317b
@ -3,11 +3,12 @@
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
# 5.4.8 (2020-11-03)
|
||||
# 5.5.0 (2020-11-08)
|
||||
|
||||
### 新特性
|
||||
* 【core 】 NumberUtil.parseInt等支持123,2.00这类数字(issue#I23ORQ@Gitee)
|
||||
* 【core 】 增加ArrayUtil.isSub、indexOfSub、lastIndexOfSub方法(issue#I23O1K@Gitee)
|
||||
* 【extra 】 增加ValidationUtil(pr#207@Gitee)
|
||||
|
||||
### Bug修复
|
||||
* 【core 】 修复DateUtil.current使用System.nanoTime的问题(issue#1198@Github)
|
||||
|
@ -246,6 +246,25 @@
|
||||
<scope>compile</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.validation</groupId>
|
||||
<artifactId>jakarta.validation-api</artifactId>
|
||||
<version>2.0.2</version>
|
||||
<scope>compile</scope>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>6.1.6.Final</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.el</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
@ -258,20 +277,5 @@
|
||||
<version>1.2.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>6.0.12.Final</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.el</groupId>
|
||||
<artifactId>javax.el-api</artifactId>
|
||||
<version>3.0.1-b01</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.web</groupId>
|
||||
<artifactId>javax.el</artifactId>
|
||||
<version>2.2.6</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
@ -14,29 +14,47 @@ public class BeanValidationResult {
|
||||
/**
|
||||
* 校验是否成功
|
||||
*/
|
||||
private Boolean success = Boolean.TRUE;
|
||||
|
||||
private boolean success;
|
||||
/**
|
||||
* 错误消息
|
||||
*/
|
||||
private List<ErrorMessage> errorMessages = new ArrayList<>();
|
||||
|
||||
public Boolean getSuccess() {
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param success 是否验证成功
|
||||
*/
|
||||
public BeanValidationResult(boolean success){
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public void setSuccess(Boolean success) {
|
||||
public BeanValidationResult setSuccess(boolean success) {
|
||||
this.success = success;
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<ErrorMessage> getErrorMessages() {
|
||||
return errorMessages;
|
||||
}
|
||||
|
||||
public void setErrorMessages(List<ErrorMessage> errorMessages) {
|
||||
public BeanValidationResult setErrorMessages(List<ErrorMessage> errorMessages) {
|
||||
this.errorMessages = errorMessages;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BeanValidationResult addErrorMessage(ErrorMessage errorMessage){
|
||||
this.errorMessages.add(errorMessage);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 错误消息,包括字段名(字段路径)和消息内容
|
||||
*/
|
||||
public static class ErrorMessage {
|
||||
/**
|
||||
* 属性字段名称
|
||||
@ -62,5 +80,13 @@ public class BeanValidationResult {
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ErrorMessage{" +
|
||||
"propertyName='" + propertyName + '\'' +
|
||||
", message='" + message + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
package cn.hutool.extra.validation;
|
||||
|
||||
import cn.hutool.extra.validation.BeanValidationResult.ErrorMessage;
|
||||
import org.hibernate.validator.HibernateValidator;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.Validation;
|
||||
@ -10,17 +9,25 @@ import java.util.Set;
|
||||
|
||||
|
||||
/**
|
||||
* java bean 校验工具类
|
||||
* java bean 校验工具类,此工具类基于validation-api(jakarta.validation-api)封装
|
||||
*
|
||||
* <p>在实际使用中,用户需引入validation-api的实现,如:hibernate-validator</p>
|
||||
* <p>注意:hibernate-validator还依赖了javax.el,需自行引入。</p>
|
||||
*
|
||||
* @author chengqiang
|
||||
*/
|
||||
public class BeanValidationUtil {
|
||||
public class ValidationUtil {
|
||||
|
||||
private static final Validator validator = Validation.byProvider(HibernateValidator.class).configure().failFast(false).buildValidatorFactory().getValidator();
|
||||
private static final Validator validator;
|
||||
|
||||
static {
|
||||
validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验对象
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param bean bean
|
||||
* @param groups 校验组
|
||||
* @return {@link Set}
|
||||
@ -29,20 +36,10 @@ public class BeanValidationUtil {
|
||||
return validator.validate(bean, groups);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验对象
|
||||
*
|
||||
* @param bean bean
|
||||
* @param groups 校验组
|
||||
* @return {@link BeanValidationResult}
|
||||
*/
|
||||
public static <T> BeanValidationResult warpValidate(T bean, Class<?>... groups) {
|
||||
return warpBeanValidationResult(validate(bean, groups));
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验bean的某一个属性
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param bean bean
|
||||
* @param propertyName 属性名称
|
||||
* @return {@link Set}
|
||||
@ -51,6 +48,18 @@ public class BeanValidationUtil {
|
||||
return validator.validateProperty(bean, propertyName, groups);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验对象
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param bean bean
|
||||
* @param groups 校验组
|
||||
* @return {@link BeanValidationResult}
|
||||
*/
|
||||
public static <T> BeanValidationResult warpValidate(T bean, Class<?>... groups) {
|
||||
return warpBeanValidationResult(validate(bean, groups));
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验bean的某一个属性
|
||||
*
|
||||
@ -69,9 +78,8 @@ public class BeanValidationUtil {
|
||||
* @return {@link BeanValidationResult}
|
||||
*/
|
||||
private static <T> BeanValidationResult warpBeanValidationResult(Set<ConstraintViolation<T>> constraintViolations) {
|
||||
BeanValidationResult result = new BeanValidationResult();
|
||||
BeanValidationResult result = new BeanValidationResult(constraintViolations.isEmpty());
|
||||
for (ConstraintViolation<T> constraintViolation : constraintViolations) {
|
||||
result.setSuccess(Boolean.FALSE);
|
||||
ErrorMessage errorMessage = new ErrorMessage();
|
||||
errorMessage.setPropertyName(constraintViolation.getPropertyPath().toString());
|
||||
errorMessage.setMessage(constraintViolation.getMessage());
|
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* java bean 校验工具
|
||||
* 基于JSR-303标准的校验工具类,封装了javax.validation的API
|
||||
*
|
||||
* @author chengqiang
|
||||
*/
|
||||
|
@ -1,6 +1,6 @@
|
||||
package cn.hutool.extra.validation;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
@ -39,13 +39,15 @@ public class BeanValidatorUtilTest {
|
||||
|
||||
@Test
|
||||
public void beanValidatorTest() {
|
||||
BeanValidationResult result = BeanValidationUtil.warpValidate(new TestClass());
|
||||
Assert.isTrue(result.getSuccess());
|
||||
BeanValidationResult result = ValidationUtil.warpValidate(new TestClass());
|
||||
Assert.assertFalse(result.isSuccess());
|
||||
Assert.assertEquals(2, result.getErrorMessages().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void propertyValidatorTest() {
|
||||
BeanValidationResult result = BeanValidationUtil.warpValidateProperty(new TestClass(), "name");
|
||||
Assert.isTrue(result.getSuccess());
|
||||
BeanValidationResult result = ValidationUtil.warpValidateProperty(new TestClass(), "name");
|
||||
Assert.assertFalse(result.isSuccess());
|
||||
Assert.assertEquals(1, result.getErrorMessages().size());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user