diff --git a/src/main/java/cn/iocoder/dashboard/framework/validator/custom/handler/SizeHandler.java b/src/main/java/cn/iocoder/dashboard/framework/validator/custom/handler/SizeHandler.java new file mode 100644 index 0000000000..7f07a552d4 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/validator/custom/handler/SizeHandler.java @@ -0,0 +1,42 @@ +package cn.iocoder.dashboard.framework.validator.custom.handler; + +import org.slf4j.helpers.MessageFormatter; + +import javax.validation.constraints.Size; + +/** + *

长度校验处理类 + * + * @author xyf + * @date 2021/5/13 + */ +public class SizeHandler implements ValidateAnnotationHandler { + + /** + * 小于等于 + */ + private static final String VALUE_LT = "长度必须大于等于{}!"; + /** + * 大于等于 + */ + private static final String VALUE_GT = "长度必须小于等于{}!"; + + @Override + public Class 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; + } + +} \ No newline at end of file diff --git a/src/main/java/cn/iocoder/dashboard/framework/validator/custom/handler/ValidateHandlerHelper.java b/src/main/java/cn/iocoder/dashboard/framework/validator/custom/handler/ValidateHandlerHelper.java index 1d4393ab6f..c9739c84df 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/validator/custom/handler/ValidateHandlerHelper.java +++ b/src/main/java/cn/iocoder/dashboard/framework/validator/custom/handler/ValidateHandlerHelper.java @@ -31,6 +31,7 @@ public class ValidateHandlerHelper { handlerSet.add(new NotBlankHandler()); handlerSet.add(new MaxHandler()); handlerSet.add(new MinHandler()); + handlerSet.add(new SizeHandler()); }