Created
June 9, 2022 06:14
-
-
Save appleshan/cc18edb907d7d3fdbdac55f8acda8e4a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hytto.gfw.dpg.util.validation; | |
import org.apache.commons.lang3.StringUtils; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import javax.validation.Constraint; | |
import javax.validation.ConstraintValidator; | |
import javax.validation.ConstraintValidatorContext; | |
import javax.validation.Payload; | |
import java.lang.annotation.Documented; | |
import java.lang.annotation.Retention; | |
import java.lang.annotation.Target; | |
import static java.lang.annotation.ElementType.FIELD; | |
import static java.lang.annotation.RetentionPolicy.RUNTIME; | |
/** | |
* @Description: 自定义constraint的实现,检查 Interest 的数量 | |
* @Author alecshan@hytto.com | |
* @Date 2022/5/11 | |
**/ | |
@Target({FIELD}) | |
@Retention(RUNTIME) | |
@Documented | |
@Constraint(validatedBy = {CharSplitQuantityValidator.QuantityValidatorInner.class}) | |
public @interface CharSplitQuantityValidator { | |
/** | |
* 必须的属性 | |
* 显示 校验信息 | |
* 利用 {} 获取 属性值,参考了官方的message编写方式 | |
* | |
* @see org.hibernate.validator 静态资源包里面 message 编写方式 | |
*/ | |
String message() default "数量不匹配"; | |
/** | |
* 必须的属性 | |
* 用于分组校验 | |
*/ | |
Class<?>[] groups() default {}; | |
Class<? extends Payload>[] payload() default {}; | |
/** | |
* 非必须 | |
*/ | |
int number() default 5; | |
char separatorChar() default ','; | |
/** | |
* 必须实现 ConstraintValidator接口 | |
*/ | |
class QuantityValidatorInner implements ConstraintValidator<CharSplitQuantityValidator, String> { | |
private static final Logger LOGGER = LoggerFactory.getLogger(QuantityValidatorInner.class); | |
private static final int USER_PHOTO_NUMBER = 5; | |
private int number; | |
private char separatorChar; | |
@Override | |
public void initialize(CharSplitQuantityValidator constraintAnnotation) { | |
this.number = constraintAnnotation.number(); | |
this.separatorChar = constraintAnnotation.separatorChar(); | |
} | |
/** | |
* 校验逻辑的实现 | |
* | |
* @param value 需要校验的 值 | |
* @return 布尔值结果 | |
*/ | |
@Override | |
public boolean isValid(String value, ConstraintValidatorContext context) { | |
if (StringUtils.isEmpty(value)) { | |
LOGGER.debug("======> value is empty."); | |
return true; | |
} | |
// StringUtils.split() 会丢弃空值 | |
String[] valueArray = StringUtils.split(value, this.separatorChar); | |
int valueArrayLength = valueArray.length; | |
LOGGER.debug("======> The length of the split value: {}.", valueArrayLength); | |
if (valueArrayLength > this.number) { | |
return false; | |
} | |
LOGGER.debug("======> isValid(value): true"); | |
return true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment