Commit c25d1b37193f6985af7583723cb3c0c9e240e869
1 parent
f05a66f8
review:@InDict 的实现,以及相关的使用案例
Showing
4 changed files
with
14 additions
and
8 deletions
yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/dict/validation/InDict.java
yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/dict/validation/InDictCollectionValidator.java
| 1 | 1 | package cn.iocoder.yudao.framework.dict.validation; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.framework.common.util.string.StrUtils; | |
| 3 | +import cn.hutool.core.collection.CollUtil; | |
| 4 | 4 | import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils; |
| 5 | 5 | import jakarta.validation.ConstraintValidator; |
| 6 | 6 | import jakarta.validation.ConstraintValidatorContext; |
| ... | ... | @@ -19,19 +19,22 @@ public class InDictCollectionValidator implements ConstraintValidator<InDict, Co |
| 19 | 19 | |
| 20 | 20 | @Override |
| 21 | 21 | public boolean isValid(Collection<?> list, ConstraintValidatorContext context) { |
| 22 | - if (list == null) { | |
| 22 | + // 为空时,默认不校验,即认为通过 | |
| 23 | + if (CollUtil.isEmpty(list)) { | |
| 23 | 24 | return true; |
| 24 | 25 | } |
| 26 | + // 校验全部通过 | |
| 25 | 27 | List<String> dbValues = DictFrameworkUtils.getDictDataValueList(dictType); |
| 26 | 28 | boolean match = list.stream().allMatch(v -> dbValues.stream() |
| 27 | 29 | .anyMatch(dbValue -> dbValue.equalsIgnoreCase(v.toString()))); |
| 28 | 30 | if (match) { |
| 29 | 31 | return true; |
| 30 | 32 | } |
| 33 | + | |
| 31 | 34 | // 校验不通过,自定义提示语句 |
| 32 | 35 | context.disableDefaultConstraintViolation(); // 禁用默认的 message 的值 |
| 33 | 36 | context.buildConstraintViolationWithTemplate( |
| 34 | - context.getDefaultConstraintMessageTemplate().replaceAll("\\{value}",dbValues.toString()) | |
| 37 | + context.getDefaultConstraintMessageTemplate().replaceAll("\\{value}", dbValues.toString()) | |
| 35 | 38 | ).addConstraintViolation(); // 重新添加错误提示语句 |
| 36 | 39 | return false; |
| 37 | 40 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-excel/src/main/java/cn/iocoder/yudao/framework/dict/validation/InDictValidator.java
| 1 | 1 | package cn.iocoder.yudao.framework.dict.validation; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.framework.common.util.string.StrUtils; | |
| 3 | +import cn.hutool.core.util.StrUtil; | |
| 4 | 4 | import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils; |
| 5 | 5 | import jakarta.validation.ConstraintValidator; |
| 6 | 6 | import jakarta.validation.ConstraintValidatorContext; |
| ... | ... | @@ -24,10 +24,11 @@ public class InDictValidator implements ConstraintValidator<InDict, Object> { |
| 24 | 24 | } |
| 25 | 25 | // 校验通过 |
| 26 | 26 | final List<String> values = DictFrameworkUtils.getDictDataValueList(dictType); |
| 27 | - boolean match = values.stream().anyMatch(v -> v.equalsIgnoreCase(value.toString())); | |
| 27 | + boolean match = values.stream().anyMatch(v -> StrUtil.equalsIgnoreCase(v, value.toString())); | |
| 28 | 28 | if (match) { |
| 29 | 29 | return true; |
| 30 | 30 | } |
| 31 | + | |
| 31 | 32 | // 校验不通过,自定义提示语句 |
| 32 | 33 | context.disableDefaultConstraintViolation(); // 禁用默认的 message 的值 |
| 33 | 34 | context.buildConstraintViolationWithTemplate( | ... | ... |
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/vo/user/UserUpdateStatusReqVO.java
| ... | ... | @@ -2,10 +2,11 @@ package cn.iocoder.yudao.module.system.controller.admin.user.vo.user; |
| 2 | 2 | |
| 3 | 3 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.validation.InEnum; |
| 5 | +import cn.iocoder.yudao.framework.dict.validation.InDict; | |
| 6 | +import cn.iocoder.yudao.module.system.enums.DictTypeConstants; | |
| 5 | 7 | import io.swagger.v3.oas.annotations.media.Schema; |
| 6 | -import lombok.Data; | |
| 7 | - | |
| 8 | 8 | import jakarta.validation.constraints.NotNull; |
| 9 | +import lombok.Data; | |
| 9 | 10 | |
| 10 | 11 | @Schema(description = "管理后台 - 用户更新状态 Request VO") |
| 11 | 12 | @Data |
| ... | ... | @@ -18,6 +19,7 @@ public class UserUpdateStatusReqVO { |
| 18 | 19 | @Schema(description = "状态,见 CommonStatusEnum 枚举", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") |
| 19 | 20 | @NotNull(message = "状态不能为空") |
| 20 | 21 | @InEnum(value = CommonStatusEnum.class, message = "修改状态必须是 {value}") |
| 22 | + @InDict(type = DictTypeConstants.COMMON_STATUS) | |
| 21 | 23 | private Integer status; |
| 22 | 24 | |
| 23 | 25 | } | ... | ... |