feat:验证框架进一步完善,支持前后端同步动态验证

This commit is contained in:
wangyu 2021-09-29 16:25:04 +08:00
parent 9ef064b4eb
commit 17184dd829
2 changed files with 17 additions and 0 deletions

View File

@ -1,5 +1,6 @@
package com.flyfish.framework.beans.enums;
import com.flyfish.framework.annotations.ConditionOn;
import com.flyfish.framework.beans.meta.BeanPropertyType;
import com.flyfish.framework.beans.meta.BeanValidation;
import com.flyfish.framework.validation.annotations.IdCard;
@ -7,6 +8,8 @@ import com.flyfish.framework.validation.annotations.Phone;
import com.flyfish.framework.validation.annotations.UniqueField;
import com.flyfish.framework.validation.enums.PhoneType;
import lombok.Getter;
import lombok.val;
import org.apache.commons.lang3.ArrayUtils;
import org.hibernate.validator.constraints.Currency;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
@ -18,6 +21,7 @@ import javax.validation.constraints.*;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
@ -111,6 +115,15 @@ public enum ValidationCandidate {
BeanValidation validation = new BeanValidation()
.setType(type.getValidType())
.setMessage(annotation.getString("message"));
Class<?>[] groups = annotation.getClassArray("groups");
// 存在分组情况为validation添加分组
if (ArrayUtils.isNotEmpty(groups)) {
val conditions = Arrays.stream(groups).map(clazz -> clazz.getAnnotation(ConditionOn.class))
.filter(Objects::nonNull)
.map(ConditionOn::expression)
.collect(Collectors.toList());
validation.setConditions(conditions);
}
mapper.accept(annotation, validation);
return validation;
}

View File

@ -4,6 +4,7 @@ import lombok.Data;
import lombok.experimental.Accessors;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
@ -40,6 +41,9 @@ public class BeanValidation {
// 内建的验证器
private String validator;
// 生效条件集合
private List<String> conditions;
// 额外的选项
private Map<String, Object> properties = new HashMap<>();