Эх сурвалжийг харах

新增自定义参数校验注解

woody 1 жил өмнө
parent
commit
52770a3984

+ 60 - 0
framework-common/src/main/java/com/chelvc/framework/common/annotation/Dates.java

@@ -0,0 +1,60 @@
+package com.chelvc.framework.common.annotation;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+import com.chelvc.framework.common.validator.DateValidator;
+
+/**
+ * 日期参数校验注解
+ *
+ * @author Woody
+ * @date 2024/4/27
+ */
+@Inherited
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+@Constraint(validatedBy = DateValidator.class)
+@Target({ElementType.FIELD, ElementType.PARAMETER})
+public @interface Dates {
+    /**
+     * 获取当前日期时间与最小日期时间差值(秒),0: 当前时间、<0: 过去时间、>0: 未来时间
+     *
+     * @return 时间差值(秒)
+     */
+    int min() default Integer.MIN_VALUE;
+
+    /**
+     * 获取当前日期时间与最大日期时间差值(秒),0: 当前时间、<0: 过去时间、>0: 未来时间
+     *
+     * @return 时间差值(秒)
+     */
+    int max() default Integer.MAX_VALUE;
+
+    /**
+     * 获取异常消息
+     *
+     * @return 异常消息
+     */
+    String message() default "Invalid date";
+
+    /**
+     * 获取对象分组(固定写法)
+     *
+     * @return 对象数组
+     */
+    Class<?>[] groups() default {};
+
+    /**
+     * 获取载体对象(固定写法)
+     *
+     * @return 载体对象数组
+     */
+    Class<? extends Payload>[] payload() default {};
+}

+ 60 - 0
framework-common/src/main/java/com/chelvc/framework/common/annotation/Pages.java

@@ -0,0 +1,60 @@
+package com.chelvc.framework.common.annotation;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+import com.chelvc.framework.common.validator.PagingValidator;
+
+/**
+ * 分页参数校验注解
+ *
+ * @author Woody
+ * @date 2024/4/27
+ */
+@Inherited
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+@Constraint(validatedBy = PagingValidator.class)
+@Target({ElementType.FIELD, ElementType.PARAMETER})
+public @interface Pages {
+    /**
+     * 获取最大页面大小,当值>0时生效
+     *
+     * @return 页面大小
+     */
+    int size() default -1;
+
+    /**
+     * 获取最大数据总量,当值>0时生效
+     *
+     * @return 数据总量
+     */
+    int total() default -1;
+
+    /**
+     * 获取异常消息
+     *
+     * @return 异常消息
+     */
+    String message() default "Invalid paging";
+
+    /**
+     * 获取对象分组(固定写法)
+     *
+     * @return 对象数组
+     */
+    Class<?>[] groups() default {};
+
+    /**
+     * 获取载体对象(固定写法)
+     *
+     * @return 载体对象数组
+     */
+    Class<? extends Payload>[] payload() default {};
+}

+ 53 - 0
framework-common/src/main/java/com/chelvc/framework/common/annotation/Regexes.java

@@ -0,0 +1,53 @@
+package com.chelvc.framework.common.annotation;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import javax.validation.Constraint;
+import javax.validation.Payload;
+
+import com.chelvc.framework.common.validator.RegexValidator;
+
+/**
+ * 参数正则表达式校验注解
+ *
+ * @author Woody
+ * @date 2024/4/27
+ */
+@Inherited
+@Documented
+@Retention(RetentionPolicy.RUNTIME)
+@Constraint(validatedBy = RegexValidator.class)
+@Target({ElementType.FIELD, ElementType.PARAMETER})
+public @interface Regexes {
+    /**
+     * 获取正则表达式
+     *
+     * @return 正则表达式
+     */
+    String value();
+
+    /**
+     * 获取异常消息
+     *
+     * @return 异常消息
+     */
+    String message() default "Invalid format";
+
+    /**
+     * 获取对象分组(固定写法)
+     *
+     * @return 对象数组
+     */
+    Class<?>[] groups() default {};
+
+    /**
+     * 获取载体对象(固定写法)
+     *
+     * @return 载体对象数组
+     */
+    Class<? extends Payload>[] payload() default {};
+}

+ 42 - 0
framework-common/src/main/java/com/chelvc/framework/common/validator/DateValidator.java

@@ -0,0 +1,42 @@
+package com.chelvc.framework.common.validator;
+
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.util.Date;
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+import com.chelvc.framework.common.annotation.Dates;
+import com.chelvc.framework.common.util.DateUtils;
+
+/**
+ * 日期参数验证器
+ *
+ * @author Woody
+ * @date 2024/4/27
+ */
+public class DateValidator implements ConstraintValidator<Dates, Object> {
+    private Dates annotation;
+
+    @Override
+    public void initialize(Dates annotation) {
+        this.annotation = annotation;
+    }
+
+    @Override
+    public boolean isValid(Object value, ConstraintValidatorContext context) {
+        if (this.annotation == null
+                || !(value instanceof Date || value instanceof LocalDate || value instanceof LocalDateTime)) {
+            return true;
+        }
+        int min = this.annotation.min(), max = this.annotation.max();
+        if (min == Integer.MIN_VALUE && max == Integer.MAX_VALUE) {
+            return true;
+        }
+        Date date = value instanceof Date ? (Date) value : value instanceof LocalDate ?
+                DateUtils.convert((LocalDate) value) : DateUtils.convert((LocalDateTime) value);
+        long datetime = date.getTime(), now = System.currentTimeMillis();
+        return (min == Integer.MIN_VALUE || datetime >= now + min * 1000)
+                && (max == Integer.MAX_VALUE || datetime <= now + max * 1000);
+    }
+}

+ 32 - 0
framework-common/src/main/java/com/chelvc/framework/common/validator/PagingValidator.java

@@ -0,0 +1,32 @@
+package com.chelvc.framework.common.validator;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+import com.chelvc.framework.common.annotation.Pages;
+import com.chelvc.framework.common.model.Paging;
+
+/**
+ * 分页参数验证器
+ *
+ * @author Woody
+ * @date 2024/4/27
+ */
+public class PagingValidator implements ConstraintValidator<Pages, Object> {
+    private Pages annotation;
+
+    @Override
+    public void initialize(Pages annotation) {
+        this.annotation = annotation;
+    }
+
+    @Override
+    public boolean isValid(Object value, ConstraintValidatorContext context) {
+        if (this.annotation == null || !(value instanceof Paging)) {
+            return true;
+        }
+        Paging paging = (Paging) value;
+        int size = this.annotation.size(), total = this.annotation.total();
+        return (size < 1 || paging.getSize() <= size) && (total < 1 || paging.getNumber() * paging.getSize() <= total);
+    }
+}

+ 31 - 0
framework-common/src/main/java/com/chelvc/framework/common/validator/RegexValidator.java

@@ -0,0 +1,31 @@
+package com.chelvc.framework.common.validator;
+
+import javax.validation.ConstraintValidator;
+import javax.validation.ConstraintValidatorContext;
+
+import com.chelvc.framework.common.annotation.Regexes;
+import com.chelvc.framework.common.util.StringUtils;
+
+/**
+ * 参数正则表达验证器
+ *
+ * @author Woody
+ * @date 2024/4/27
+ */
+public class RegexValidator implements ConstraintValidator<Regexes, Object> {
+    private Regexes annotation;
+
+    @Override
+    public void initialize(Regexes annotation) {
+        this.annotation = annotation;
+    }
+
+    @Override
+    public boolean isValid(Object value, ConstraintValidatorContext context) {
+        if (this.annotation == null || !(value instanceof CharSequence)) {
+            return true;
+        }
+        String regex = this.annotation.value();
+        return StringUtils.isEmpty(regex) || StringUtils.matches((CharSequence) value, regex);
+    }
+}