feat(通用功能): 参数校验支持枚举值
This commit is contained in:
parent
6d2fe94a80
commit
06f2d9229c
|
@ -0,0 +1,15 @@
|
|||
package io.metersphere.sdk.constants;
|
||||
|
||||
/**
|
||||
* 用于参数校验注解 EnumValue 的枚举接口
|
||||
* 如果枚举定义了类似 value 的值,可以实现改接口,即可使用于 EnumValue 注解
|
||||
* 如果枚举值只需要通过 name() 获取,可以不实现该接口
|
||||
* @author jianxing
|
||||
*/
|
||||
public interface ValueEnum<T> {
|
||||
/**
|
||||
* 获取枚举值
|
||||
* @return 枚举值
|
||||
*/
|
||||
T getValue();
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package io.metersphere.sdk.dto.request;
|
||||
|
||||
import io.metersphere.sdk.constants.UserRoleType;
|
||||
import io.metersphere.sdk.valid.EnumValue;
|
||||
import io.metersphere.validation.groups.Created;
|
||||
import io.metersphere.validation.groups.Updated;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
@ -31,6 +33,7 @@ public class UserRoleUpdateRequest implements Serializable {
|
|||
|
||||
@Schema(title = "所属类型 SYSTEM ORGANIZATION PROJECT", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotBlank(message = "{user_role.type.not_blank}", groups = {Created.class})
|
||||
@EnumValue(enumClass = UserRoleType.class, groups = {Created.class})
|
||||
@Size(min = 1, max = 20, message = "{user_role.type.length_range}", groups = {Created.class, Updated.class})
|
||||
private String type;
|
||||
}
|
||||
|
|
|
@ -95,6 +95,7 @@ public class BaseUserRoleService {
|
|||
protected UserRole update(UserRole userRole) {
|
||||
userRole.setCreateUser(null);
|
||||
userRole.setCreateTime(null);
|
||||
userRole.setType(null);
|
||||
userRole.setUpdateTime(System.currentTimeMillis());
|
||||
userRoleMapper.updateByPrimaryKeySelective(userRole);
|
||||
return userRole;
|
||||
|
|
|
@ -0,0 +1,96 @@
|
|||
package io.metersphere.sdk.valid;
|
||||
|
||||
import io.metersphere.sdk.constants.ValueEnum;
|
||||
import io.metersphere.sdk.util.Translator;
|
||||
import jakarta.validation.Constraint;
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.ConstraintValidatorContext;
|
||||
import jakarta.validation.Payload;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 枚举值校验,即值只能是指定枚举类中的值
|
||||
* @author jianxing
|
||||
*/
|
||||
@Target({ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Constraint(validatedBy = EnumValue.EnumValueValidator.class)
|
||||
public @interface EnumValue {
|
||||
/**
|
||||
* 错误提示
|
||||
*/
|
||||
String message() default "{enum_value_valid_message}";
|
||||
|
||||
/**
|
||||
* 必须的属性
|
||||
* 用于分组校验
|
||||
*/
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
|
||||
/**
|
||||
* 枚举类
|
||||
*/
|
||||
Class<? extends Enum> enumClass();
|
||||
|
||||
/**
|
||||
* 校验时排除枚举中的某些值,只支持 String 类型
|
||||
*/
|
||||
String[] excludeValues() default {};
|
||||
|
||||
class EnumValueValidator implements ConstraintValidator<EnumValue, Object> {
|
||||
|
||||
private Class<? extends Enum> enumClass;
|
||||
private String[] excludeValues;
|
||||
|
||||
@Override
|
||||
public void initialize(EnumValue enumValue) {
|
||||
this.enumClass = enumValue.enumClass();
|
||||
this.excludeValues = enumValue.excludeValues();
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验参数是否在枚举值中
|
||||
* @param value 待校验的参数
|
||||
* @param context 上下文
|
||||
* @return 校验结果
|
||||
*/
|
||||
@Override
|
||||
public boolean isValid(Object value, ConstraintValidatorContext context) {
|
||||
if (value == null) {
|
||||
return true;
|
||||
}
|
||||
// 获取枚举的所有实例
|
||||
Enum<? extends Enum>[] enums = enumClass.getEnumConstants();
|
||||
// 获取所有的枚举值
|
||||
List<Object> values = new ArrayList<>();
|
||||
for (Enum<? extends Enum> item : enums) {
|
||||
if (item instanceof ValueEnum) {
|
||||
values.add(((ValueEnum) item).getValue());
|
||||
} else {
|
||||
values.add(item.name());
|
||||
}
|
||||
}
|
||||
// 如果设置了排除值,则判断是否在排除值中
|
||||
boolean isExcludeValue = excludeValues == null ? false : Arrays.stream(excludeValues).anyMatch(value::equals);
|
||||
boolean valid = values.contains(value) && !isExcludeValue;
|
||||
if (!valid) {
|
||||
// 禁用默认的message的值
|
||||
context.disableDefaultConstraintViolation();
|
||||
// values 与 excludeValues 的差集
|
||||
String errorValues = CollectionUtils.subtract(values, Arrays.asList(excludeValues)).toString();
|
||||
// 重新添加错误提示语句
|
||||
context.buildConstraintViolationWithTemplate(Translator.get("enum_value_valid_message") + errorValues).addConstraintViolation();
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -386,3 +386,5 @@ http_result_unknown_exception=system unknown exception
|
|||
http_result_validate=parameter validate failure
|
||||
http_result_unauthorized=user authentication failure
|
||||
http_result_forbidden=permission authentication failure
|
||||
|
||||
enum_value_valid_message=The enumeration value is invalid, must be
|
|
@ -382,4 +382,6 @@ http_result_success=操作成功
|
|||
http_result_unknown_exception=系统未知异常
|
||||
http_result_validate=参数校验失败
|
||||
http_result_unauthorized=用户认证失败
|
||||
http_result_forbidden=权限认证失败
|
||||
http_result_forbidden=权限认证失败
|
||||
|
||||
enum_value_valid_message=枚举值不合法,必须为
|
|
@ -381,4 +381,6 @@ http_result_success=操作成功
|
|||
http_result_unknown_exception=系統未知異常
|
||||
http_result_validate=參數校驗失敗
|
||||
http_result_unauthorized=用戶認證失敗
|
||||
http_result_forbidden=權限認證失敗
|
||||
http_result_forbidden=權限認證失敗
|
||||
|
||||
enum_value_valid_message=枚舉值不合法,必須為
|
Loading…
Reference in New Issue