From 9e613488f1a01563913b4f6aade062980a315fea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E8=BE=9E=E6=9C=AA=E5=AF=92?= <545073804@qq.com> Date: Mon, 9 Dec 2024 09:03:31 +0000 Subject: [PATCH 1/4] =?UTF-8?q?add=20=E6=96=B0=E5=A2=9E=20validation?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=9E=9A=E4=B8=BE=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 秋辞未寒 <545073804@qq.com> --- .../core/validate/enumd/EnumPattern.java | 39 +++++++++++++++++ .../validate/enumd/EnumPatternValidator.java | 43 +++++++++++++++++++ .../core/validate/enumd/ValidateEnum.java | 29 +++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPattern.java create mode 100644 ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPatternValidator.java create mode 100644 ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/ValidateEnum.java diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPattern.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPattern.java new file mode 100644 index 0000000000..2f7de7550f --- /dev/null +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPattern.java @@ -0,0 +1,39 @@ +package org.dromara.common.core.validate.enumd; + +import jakarta.validation.Constraint; +import jakarta.validation.Payload; + +import java.lang.annotation.*; + +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +/** + * 自定义枚举校验注解 + * + * @author 秋辞未寒 + * @date 2024-12-09 + */ +@Documented +@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) +@Retention(RUNTIME) +@Repeatable(EnumPattern.List.class) // 允许在同一元素上多次使用该注解 +@Constraint(validatedBy = {EnumPatternValidator.class}) +public @interface EnumPattern { + + Class type() default ValidateEnum.class; + + String message() default "输入值不在枚举范围内"; + + Class[] groups() default {}; + + Class[] payload() default {}; + + @Documented + @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE}) + @Retention(RUNTIME) + @interface List { + EnumPattern[] value(); + } + +} diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPatternValidator.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPatternValidator.java new file mode 100644 index 0000000000..451dc71a76 --- /dev/null +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPatternValidator.java @@ -0,0 +1,43 @@ +package org.dromara.common.core.validate.enumd; + +import org.dromara.common.core.utils.StringUtils; +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; +import jakarta.validation.ValidationException; +import lombok.extern.slf4j.Slf4j; + +/** + * 自定义枚举校验注解实现 + * + * @author 秋辞未寒 + * @date 2024-12-09 + */ +public class EnumPatternValidator implements ConstraintValidator { + + private EnumPattern annotation;; + + @Override + public void initialize(EnumPattern annotation) { + ConstraintValidator.super.initialize(annotation); + this.annotation = annotation; + } + + @Override + public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) { + try { + if (StringUtils.isNotBlank(value)) { + Class type = annotation.type(); + ValidateEnum[] constants = type.getEnumConstants(); + for (ValidateEnum e : constants) { + if (e.validate(value)) { + return true; + } + } + } + return false; + } catch (Exception e) { + throw new ValidationException(e); + } + } + +} \ No newline at end of file diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/ValidateEnum.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/ValidateEnum.java new file mode 100644 index 0000000000..f3a367c8f1 --- /dev/null +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/ValidateEnum.java @@ -0,0 +1,29 @@ +package org.dromara.common.core.validate.enumd; + +import org.dromara.common.core.utils.StringUtils; + +/** + * 枚举类型校验接口 + * + * @author 秋辞未寒 + * @date 2024-12-09 + */ +public interface ValidateEnum { + + /** + * 获取枚举code + *
该code用于匹配
+ * @return 枚举code + */ + String getCode(); + + /** + * 校验枚举code + * @param code 枚举code + * @return 校验结果 + */ + default boolean validate(String code) { + return StringUtils.equals(code, getCode()); + } + +} -- Gitee From 9009a90ef27754a81ba86932663d33e176942d4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E8=BE=9E=E6=9C=AA=E5=AF=92?= <545073804@qq.com> Date: Mon, 9 Dec 2024 09:41:52 +0000 Subject: [PATCH 2/4] =?UTF-8?q?update=20=E6=9B=B4=E6=96=B0=20=E6=9E=9A?= =?UTF-8?q?=E4=B8=BE=E6=A0=A1=E9=AA=8C=E9=80=9A=E8=BF=87=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=9E=9A=E4=B8=BE=E5=80=BC=EF=BC=8C=E4=B8=8D=E5=86=8D=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E5=AE=9E=E7=8E=B0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 秋辞未寒 <545073804@qq.com> --- .../common/core/validate/enumd/EnumPattern.java | 10 +++++++++- .../core/validate/enumd/EnumPatternValidator.java | 10 ++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPattern.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPattern.java index 2f7de7550f..35f7ecbe3f 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPattern.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPattern.java @@ -21,7 +21,15 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; @Constraint(validatedBy = {EnumPatternValidator.class}) public @interface EnumPattern { - Class type() default ValidateEnum.class; + /** + * 需要校验的枚举类型 + */ + Class type() default Enum.class; + + /** + * 枚举类型校验值判断方法 + */ + String method() default "getCode"; String message() default "输入值不在枚举范围内"; diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPatternValidator.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPatternValidator.java index 451dc71a76..af87225a6f 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPatternValidator.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPatternValidator.java @@ -1,6 +1,7 @@ package org.dromara.common.core.validate.enumd; import org.dromara.common.core.utils.StringUtils; +import org.dromara.common.core.utils.reflect.ReflectUtils; import jakarta.validation.ConstraintValidator; import jakarta.validation.ConstraintValidatorContext; import jakarta.validation.ValidationException; @@ -26,10 +27,11 @@ public class EnumPatternValidator implements ConstraintValidator type = annotation.type(); - ValidateEnum[] constants = type.getEnumConstants(); - for (ValidateEnum e : constants) { - if (e.validate(value)) { + Class type = annotation.type(); + Object[] enumConstants = type.getEnumConstants(); + Method method = ReflectUtils.getMethod(type, annotation.method()); + for (Object e : enumConstants) { + if (value.equals(method.invoke(e))) { return true; } } -- Gitee From a1f82a7d0855db0eb19c66d909bed4c76a77f7a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E8=BE=9E=E6=9C=AA=E5=AF=92?= <545073804@qq.com> Date: Mon, 9 Dec 2024 09:43:01 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20ruoy?= =?UTF-8?q?i-common/ruoyi-common-core/src/main/java/org/dromara/common/cor?= =?UTF-8?q?e/validate/enumd/ValidateEnum.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/validate/enumd/ValidateEnum.java | 29 ------------------- 1 file changed, 29 deletions(-) delete mode 100644 ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/ValidateEnum.java diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/ValidateEnum.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/ValidateEnum.java deleted file mode 100644 index f3a367c8f1..0000000000 --- a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/ValidateEnum.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.dromara.common.core.validate.enumd; - -import org.dromara.common.core.utils.StringUtils; - -/** - * 枚举类型校验接口 - * - * @author 秋辞未寒 - * @date 2024-12-09 - */ -public interface ValidateEnum { - - /** - * 获取枚举code - *
该code用于匹配
- * @return 枚举code - */ - String getCode(); - - /** - * 校验枚举code - * @param code 枚举code - * @return 校验结果 - */ - default boolean validate(String code) { - return StringUtils.equals(code, getCode()); - } - -} -- Gitee From 4821902fdc73052b56a0b39ebe60e0c7287b254e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=8B=E8=BE=9E=E6=9C=AA=E5=AF=92?= <545073804@qq.com> Date: Mon, 9 Dec 2024 10:16:49 +0000 Subject: [PATCH 4/4] =?UTF-8?q?update=20=E6=9B=B4=E6=96=B0=20=E6=9E=9A?= =?UTF-8?q?=E4=B8=BE=E6=A0=A1=E9=AA=8C=E9=80=9A=E8=BF=87=E6=9E=9A=E4=B8=BE?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E7=9A=84getter=E6=96=B9=E6=B3=95=E5=8F=96?= =?UTF-8?q?=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 秋辞未寒 <545073804@qq.com> --- .../dromara/common/core/validate/enumd/EnumPattern.java | 7 ++++--- .../common/core/validate/enumd/EnumPatternValidator.java | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPattern.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPattern.java index 35f7ecbe3f..c31c7703c5 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPattern.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPattern.java @@ -9,7 +9,7 @@ import static java.lang.annotation.ElementType.*; import static java.lang.annotation.RetentionPolicy.RUNTIME; /** - * 自定义枚举校验注解 + * 自定义枚举校验 * * @author 秋辞未寒 * @date 2024-12-09 @@ -27,9 +27,10 @@ public @interface EnumPattern { Class type() default Enum.class; /** - * 枚举类型校验值判断方法 + * 枚举类型校验值字段名称 + * TODO 需确保该字段实现了 getter 方法 */ - String method() default "getCode"; + String fieldName() default "code"; String message() default "输入值不在枚举范围内"; diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPatternValidator.java b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPatternValidator.java index af87225a6f..0e748c0a44 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPatternValidator.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/org/dromara/common/core/validate/enumd/EnumPatternValidator.java @@ -28,10 +28,10 @@ public class EnumPatternValidator implements ConstraintValidator type = annotation.type(); + String fieldName = annotation.fieldName(); Object[] enumConstants = type.getEnumConstants(); - Method method = ReflectUtils.getMethod(type, annotation.method()); for (Object e : enumConstants) { - if (value.equals(method.invoke(e))) { + if (value.equals(ReflectUtils.invokeGetter(e, fieldName))) { return true; } } -- Gitee