Commit 9c71bc322055f8620adc4b18f2e4902df2830e35

Authored by 芋道源码
Committed by GitHub
2 parents ca65267f ce2a7d2c

Merge pull request #852 from java-acc/feat/785-jdk17-dict-validate-annotation

feat(dict): 实现参数数据字典校验注解 @InDict(#785)
yudao-framework/yudao-spring-boot-starter-excel/pom.xml
@@ -47,6 +47,12 @@ @@ -47,6 +47,12 @@
47 </dependency> 47 </dependency>
48 48
49 <dependency> 49 <dependency>
  50 + <groupId>jakarta.validation</groupId>
  51 + <artifactId>jakarta.validation-api</artifactId>
  52 + <scope>provided</scope>
  53 + </dependency>
  54 +
  55 + <dependency>
50 <groupId>com.google.guava</groupId> 56 <groupId>com.google.guava</groupId>
51 <artifactId>guava</artifactId> 57 <artifactId>guava</artifactId>
52 </dependency> 58 </dependency>
yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/dict/core/DictFrameworkUtils.java
@@ -76,4 +76,9 @@ public class DictFrameworkUtils { @@ -76,4 +76,9 @@ public class DictFrameworkUtils {
76 return dictData!= null ? dictData.getValue(): null; 76 return dictData!= null ? dictData.getValue(): null;
77 } 77 }
78 78
  79 + @SneakyThrows
  80 + public static List<String> getDictDataValueList(String dictType) {
  81 + List<DictDataRespDTO> dictDatas = GET_DICT_DATA_CACHE.get(dictType);
  82 + return convertList(dictDatas, DictDataRespDTO::getValue);
  83 + }
79 } 84 }
yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/dict/validation/InDict.java 0 → 100644
  1 +package cn.iocoder.yudao.framework.dict.validation;
  2 +
  3 +import jakarta.validation.Constraint;
  4 +import jakarta.validation.Payload;
  5 +
  6 +import java.lang.annotation.Documented;
  7 +import java.lang.annotation.ElementType;
  8 +import java.lang.annotation.Retention;
  9 +import java.lang.annotation.RetentionPolicy;
  10 +import java.lang.annotation.Target;
  11 +
  12 +@Target({
  13 + ElementType.METHOD,
  14 + ElementType.FIELD,
  15 + ElementType.ANNOTATION_TYPE,
  16 + ElementType.CONSTRUCTOR,
  17 + ElementType.PARAMETER,
  18 + ElementType.TYPE_USE
  19 +})
  20 +@Retention(RetentionPolicy.RUNTIME)
  21 +@Documented
  22 +@Constraint(
  23 + validatedBy = {InDictValidator.class, InDictCollectionValidator.class}
  24 +)
  25 +public @interface InDict {
  26 +
  27 + /**
  28 + * 数据字典type
  29 + */
  30 + String type();
  31 +
  32 + String message() default "必须在指定范围 {value}";
  33 +
  34 + Class<?>[] groups() default {};
  35 +
  36 + Class<? extends Payload>[] payload() default {};
  37 +
  38 +}
yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/dict/validation/InDictCollectionValidator.java 0 → 100644
  1 +package cn.iocoder.yudao.framework.dict.validation;
  2 +
  3 +import cn.iocoder.yudao.framework.common.util.string.StrUtils;
  4 +import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
  5 +import jakarta.validation.ConstraintValidator;
  6 +import jakarta.validation.ConstraintValidatorContext;
  7 +
  8 +import java.util.Collection;
  9 +import java.util.List;
  10 +
  11 +public class InDictCollectionValidator implements ConstraintValidator<InDict, Collection<?>> {
  12 +
  13 + private String dictType;
  14 +
  15 + @Override
  16 + public void initialize(InDict annotation) {
  17 + this.dictType = annotation.type();
  18 + }
  19 +
  20 + @Override
  21 + public boolean isValid(Collection<?> list, ConstraintValidatorContext context) {
  22 + if (list == null) {
  23 + return true;
  24 + }
  25 + List<String> dbValues = DictFrameworkUtils.getDictDataValueList(dictType);
  26 + boolean match = list.stream().allMatch(v -> dbValues.stream()
  27 + .anyMatch(dbValue -> dbValue.equalsIgnoreCase(v.toString())));
  28 + if (match) {
  29 + return true;
  30 + }
  31 + // 校验不通过,自定义提示语句
  32 + context.disableDefaultConstraintViolation(); // 禁用默认的 message 的值
  33 + context.buildConstraintViolationWithTemplate(
  34 + context.getDefaultConstraintMessageTemplate().replaceAll("\\{value}",dbValues.toString())
  35 + ).addConstraintViolation(); // 重新添加错误提示语句
  36 + return false;
  37 + }
  38 +
  39 +}
  40 +
yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/dict/validation/InDictValidator.java 0 → 100644
  1 +package cn.iocoder.yudao.framework.dict.validation;
  2 +
  3 +import cn.iocoder.yudao.framework.common.util.string.StrUtils;
  4 +import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
  5 +import jakarta.validation.ConstraintValidator;
  6 +import jakarta.validation.ConstraintValidatorContext;
  7 +
  8 +import java.util.List;
  9 +
  10 +public class InDictValidator implements ConstraintValidator<InDict, Object> {
  11 +
  12 + private String dictType;
  13 +
  14 + @Override
  15 + public void initialize(InDict annotation) {
  16 + this.dictType = annotation.type();
  17 + }
  18 +
  19 + @Override
  20 + public boolean isValid(Object value, ConstraintValidatorContext context) {
  21 + // 为空时,默认不校验,即认为通过
  22 + if (value == null) {
  23 + return true;
  24 + }
  25 + // 校验通过
  26 + final List<String> values = DictFrameworkUtils.getDictDataValueList(dictType);
  27 + boolean match = values.stream().anyMatch(v -> v.equalsIgnoreCase(value.toString()));
  28 + if (match) {
  29 + return true;
  30 + }
  31 + // 校验不通过,自定义提示语句
  32 + context.disableDefaultConstraintViolation(); // 禁用默认的 message 的值
  33 + context.buildConstraintViolationWithTemplate(
  34 + context.getDefaultConstraintMessageTemplate().replaceAll("\\{value}", values.toString())
  35 + ).addConstraintViolation(); // 重新添加错误提示语句
  36 + return false;
  37 + }
  38 +
  39 +}
  40 +