添加长度校验处理类

This commit is contained in:
forest羽 2021-05-17 15:18:44 +08:00 committed by Gitee
parent 670e46a740
commit 65a1405d0f
2 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,42 @@
package cn.iocoder.dashboard.framework.validator.custom.handler;
import org.slf4j.helpers.MessageFormatter;
import javax.validation.constraints.Size;
/**
* <p>长度校验处理类
*
* @author xyf
* @date 2021/5/13
*/
public class SizeHandler implements ValidateAnnotationHandler<Size> {
/**
* 小于等于
*/
private static final String VALUE_LT = "长度必须大于等于{}!";
/**
* 大于等于
*/
private static final String VALUE_GT = "长度必须小于等于{}!";
@Override
public Class<Size> getAnnotation() {
return Size.class;
}
@Override
public String validate(Size validateAnnotation, Object fieldValue) {
if (fieldValue.toString().length() > validateAnnotation.max()) {
return MessageFormatter.format(VALUE_GT, fieldValue).getMessage();
}
if (fieldValue.toString().length() < validateAnnotation.min()) {
return MessageFormatter.format(VALUE_LT, fieldValue).getMessage();
}
return null;
}
}

View File

@ -31,6 +31,7 @@ public class ValidateHandlerHelper {
handlerSet.add(new NotBlankHandler());
handlerSet.add(new MaxHandler());
handlerSet.add(new MinHandler());
handlerSet.add(new SizeHandler());
}