REM:
1. 移除自定义校验器中对null与空数组的检测
2. 修正自定义枚举校验器附加返回消息
This commit is contained in:
HuangXin 2020-09-03 08:34:57 +08:00
parent 53f6ef7cca
commit a3aa4cc682
11 changed files with 70 additions and 34 deletions

View File

@ -31,20 +31,20 @@ public class AddCapacityInfo {
* The Capacity type.
*/
@NotNull(message = "capacityType 能力类型不能为空", groups = ValidGroups.AddDeviceValid.class)
@ValidBaseEnum(enumClass = DisposeCapacityType.class, message = "capacityType 字段取值为", groups = ValidGroups.AddDeviceValid.class)
@ValidBaseEnum(enumClass = DisposeCapacityType.class, groups = ValidGroups.AddDeviceValid.class)
private Integer capacityType;
/**
* The Object type.
*/
@NotNull(message = "objectType 处置对象类型不能为空", groups = ValidGroups.AddDeviceValid.class)
@ValidBaseEnum(enumClass = DisposeObjectType.class, message = "objectType 字段取值为", groups = ValidGroups.AddDeviceValid.class)
@ValidBaseEnum(enumClass = DisposeObjectType.class, groups = ValidGroups.AddDeviceValid.class)
private Integer objectType;
/**
* The Ip type.
*/
@ValidBaseEnum(enumClass = IpAddrType.class, message = "objectType 字段取值为", groups = ValidGroups.AddDeviceValid.class)
@ValidBaseEnum(enumClass = IpAddrType.class, groups = ValidGroups.AddDeviceValid.class)
private Integer ipType;
/**

View File

@ -16,6 +16,7 @@ import lombok.NoArgsConstructor;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;
/**
@ -49,8 +50,7 @@ public class AddDeviceInfo {
* The Device type.
*/
@NotNull(message = "deviceType 设备类型不能为空", groups = ValidGroups.UpgradeDeviceValid.class)
@ValidBaseEnum(enumClass = DisposeDeviceType.class, message = "deviceType 字段取值为", groups =
ValidGroups.UpgradeDeviceValid.class)
@ValidBaseEnum(enumClass = DisposeDeviceType.class, groups = ValidGroups.UpgradeDeviceValid.class)
private Integer deviceType;
/**
@ -99,7 +99,7 @@ public class AddDeviceInfo {
* The Url type.
*/
@NotNull(message = "urlType URL类型不能为空", groups = ValidGroups.AddDeviceValid.class)
@ValidBaseEnum(enumClass = HttpType.class, message = "urlType 字段取值为", groups = ValidGroups.AddDeviceValid.class)
@ValidBaseEnum(enumClass = HttpType.class, groups = ValidGroups.AddDeviceValid.class)
private Integer urlType;
/**
@ -111,6 +111,7 @@ public class AddDeviceInfo {
* The Capacity.
*/
@NotNull(message = "capacity 处置设备能力不能为空", groups = ValidGroups.AddDeviceValid.class)
@Size(min = 1, message = "capacity 至少需要包含一种能力", groups = ValidGroups.AddDeviceValid.class)
@Valid
private List<AddCapacityInfo> capacity;
}

View File

@ -37,8 +37,7 @@ public class TaskStartReq {
* The Type.
*/
@NotNull(message = "type 处置类型不能为空", groups = ValidGroups.TaskStartReqCommonValid.class)
@ValidBaseEnum(enumClass = DisposeCapacityType.class, message = "type 字段取值为", groups =
ValidGroups.TaskStartReqCommonValid.class)
@ValidBaseEnum(enumClass = DisposeCapacityType.class, groups = ValidGroups.TaskStartReqCommonValid.class)
private Integer type;
/**
* The Dispose ip.
@ -62,14 +61,12 @@ public class TaskStartReq {
/**
* The Flow direction.
*/
@ValidBaseEnum(enumClass = NetflowDirection.class, message = "flowDirection 字段取值为", groups =
ValidGroups.TaskStartReqCommonValid.class)
@ValidBaseEnum(enumClass = NetflowDirection.class, groups = ValidGroups.TaskStartReqCommonValid.class)
private Integer flowDirection;
/**
* The Attack type.
*/
@ValidBaseEnumArray(enumClass = DDoSAttackType.class, message = "attackType 字段取值为", groups =
ValidGroups.TaskStartReqCommonValid.class)
@ValidBaseEnumArray(enumClass = DDoSAttackType.class, groups = ValidGroups.TaskStartReqCommonValid.class)
@Size(min = 1, message = "attackType 必须指定最少一种攻击类型", groups = ValidGroups.TaskStartReqCommonValid.class)
private Integer[] attackType;
/**

View File

@ -33,7 +33,7 @@ public @interface ValidBaseEnum {
*
* @return the string
*/
String message();
String message() default "";
/**
* Groups class [ ].

View File

@ -33,7 +33,7 @@ public @interface ValidBaseEnumArray {
*
* @return the string
*/
String message();
String message() default "";
/**
* Groups class [ ].

View File

@ -15,11 +15,18 @@ import java.util.regex.Pattern;
* @author <huangxin@cmhi.chinamoblie.com>
*/
public class ValidArrayIpAddrImpl implements ConstraintValidator<ValidArrayIpAddr, String[]> {
/**
* Is valid boolean.
*
* @param strings the strings
* @param constraintValidatorContext the constraint validator context
* @return the boolean
*/
@Override
public boolean isValid(String[] strings, ConstraintValidatorContext constraintValidatorContext) {
if (strings == null || strings.length == 0) {
return false;
return true;
}
for (String s : strings) {
@ -43,6 +50,11 @@ public class ValidArrayIpAddrImpl implements ConstraintValidator<ValidArrayIpAdd
return true;
}
/**
* Initialize.
*
* @param constraintAnnotation the constraint annotation
*/
@Override
public void initialize(ValidArrayIpAddr constraintAnnotation) {
}

View File

@ -2,6 +2,7 @@ package com.dispose.validation.valids.impl;
import com.dispose.common.BaseEnum;
import com.dispose.validation.valids.ValidBaseEnumArray;
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
@ -10,7 +11,7 @@ import java.util.List;
import java.util.stream.Collectors;
/**
* The type Valid custom base enum array.
* The type Valid base enum array.
*
* @author <huangxin@cmhi.chinamoblie.com>
*/
@ -39,19 +40,29 @@ public class ValidBaseEnumArrayImpl implements ConstraintValidator<ValidBaseEnum
/**
* Is valid boolean.
*
* @param integers the integers
* @param constraintValidatorContext the constraint validator context
* @param integers the integers
* @param ctx the ctx
* @return the boolean
*/
@Override
public boolean isValid(Integer[] integers, ConstraintValidatorContext constraintValidatorContext) {
constraintValidatorContext.disableDefaultConstraintViolation();
List<Integer> enumValues =
Arrays.stream(enumClass.getEnumConstants()).map(BaseEnum::getValue).collect(Collectors.toList());
public boolean isValid(Integer[] integers, ConstraintValidatorContext ctx) {
String errMsg = this.message + " " + enumValues.toString();
// null 空元素由其它校验器处理
if (integers == null || integers.length == 0) {
return true;
}
constraintValidatorContext.buildConstraintViolationWithTemplate(errMsg).addConstraintViolation();
List<Integer> enumValues = Arrays.stream(enumClass.getEnumConstants())
.map(BaseEnum::getValue).collect(Collectors.toList());
String errMsg = this.message +
((ConstraintValidatorContextImpl) ctx).getConstraintViolationCreationContexts()
.get(0).getPath().getLeafNode().getName()
+ " 字段取值范围:" + enumValues.toString();
// 重新设置消息
ctx.disableDefaultConstraintViolation();
ctx.buildConstraintViolationWithTemplate(errMsg).addConstraintViolation();
return Arrays.stream(integers).allMatch(enumValues::contains);
}

View File

@ -3,6 +3,7 @@ package com.dispose.validation.valids.impl;
import com.dispose.common.BaseEnum;
import com.dispose.validation.valids.ValidBaseEnum;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorContextImpl;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
@ -42,19 +43,29 @@ public class ValidBaseEnumImpl implements ConstraintValidator<ValidBaseEnum, Int
/**
* Is valid boolean.
*
* @param integer the integer
* @param constraintValidatorContext the constraint validator context
* @param integer the integer
* @param ctx the constraint validator context
* @return the boolean
*/
@Override
public boolean isValid(Integer integer, ConstraintValidatorContext constraintValidatorContext) {
constraintValidatorContext.disableDefaultConstraintViolation();
List<Integer> enumValues =
Arrays.stream(enumClass.getEnumConstants()).map(BaseEnum::getValue).collect(Collectors.toList());
public boolean isValid(Integer integer, ConstraintValidatorContext ctx) {
String errMsg = this.message + " " + enumValues.toString();
// null 由其它校验器处理
if (integer == null) {
return true;
}
constraintValidatorContext.buildConstraintViolationWithTemplate(errMsg).addConstraintViolation();
List<Integer> enumValues = Arrays.stream(enumClass.getEnumConstants())
.map(BaseEnum::getValue).collect(Collectors.toList());
String errMsg = this.message +
((ConstraintValidatorContextImpl) ctx).getConstraintViolationCreationContexts()
.get(0).getPath().getLeafNode().getName()
+ " 字段取值范围:" + enumValues.toString();
// 重新设置消息
ctx.disableDefaultConstraintViolation();
ctx.buildConstraintViolationWithTemplate(errMsg).addConstraintViolation();
return enumValues.contains(integer);
}

View File

@ -36,7 +36,7 @@ public class ValidIpAddrImpl implements ConstraintValidator<ValidIpAddr, String>
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
if(s == null || s.length() == 0) {
return false;
return true;
}
if (!Pattern.matches(ConstValue.IP_ADDR_REG, s)) {

View File

@ -33,6 +33,10 @@ public class ValidIpPortImpl implements ConstraintValidator<ValidIpPort, String>
final int minSocketPort = 1;
final int maxSocketPort = 65535;
if (s == null || s.length() == 0) {
return true;
}
try {
int port = Integer.parseInt(s);

View File

@ -34,7 +34,7 @@ public class ValidIpSegmentImpl implements ConstraintValidator<ValidIpSegment, S
@Override
public boolean isValid(String str, ConstraintValidatorContext constraintValidatorContext) {
if (str == null || str.length() == 0) {
return false;
return true;
}
for (String s : str.split(DisposeConfigValue.SPLIT_CHAR)) {