From 18a02ef74f2f90f7928d58c07866ff5cadea563f Mon Sep 17 00:00:00 2001 From: odboy Date: Fri, 6 Dec 2024 21:11:54 +0800 Subject: [PATCH] =?UTF-8?q?upd:=20=E8=A7=84=E8=8C=83=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/odboy/infra/validate/Mobile.java | 21 +++++++---- .../odboy/infra/validate/MobileValidator.java | 35 +++++++++---------- .../cn/odboy/infra/validate/NotEmptyList.java | 21 +++++++---- .../infra/validate/NotEmptyListValidator.java | 26 +++++++------- 4 files changed, 58 insertions(+), 45 deletions(-) diff --git a/kenaito-config-service/src/main/java/cn/odboy/infra/validate/Mobile.java b/kenaito-config-service/src/main/java/cn/odboy/infra/validate/Mobile.java index 736bd33..1de15e6 100644 --- a/kenaito-config-service/src/main/java/cn/odboy/infra/validate/Mobile.java +++ b/kenaito-config-service/src/main/java/cn/odboy/infra/validate/Mobile.java @@ -1,19 +1,26 @@ package cn.odboy.infra.validate; +import java.lang.annotation.*; import javax.validation.Constraint; import javax.validation.Payload; -import java.lang.annotation.*; @Documented @Constraint(validatedBy = {MobileValidator.class}) -@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE}) +@Target({ + ElementType.METHOD, + ElementType.FIELD, + ElementType.ANNOTATION_TYPE, + ElementType.CONSTRUCTOR, + ElementType.PARAMETER, + ElementType.TYPE_USE +}) @Retention(RetentionPolicy.RUNTIME) public @interface Mobile { - boolean required() default false; + boolean required() default false; - String message() default "手机号码格式错误"; + String message() default "手机号码格式错误"; - Class[] groups() default {}; + Class[] groups() default {}; - Class[] payload() default {}; -} \ No newline at end of file + Class[] payload() default {}; +} diff --git a/kenaito-config-service/src/main/java/cn/odboy/infra/validate/MobileValidator.java b/kenaito-config-service/src/main/java/cn/odboy/infra/validate/MobileValidator.java index 97e5f3c..b8a0520 100644 --- a/kenaito-config-service/src/main/java/cn/odboy/infra/validate/MobileValidator.java +++ b/kenaito-config-service/src/main/java/cn/odboy/infra/validate/MobileValidator.java @@ -3,7 +3,6 @@ package cn.odboy.infra.validate; import cn.hutool.core.util.ReUtil; import cn.hutool.core.util.StrUtil; import cn.odboy.constant.RegexConst; - import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; @@ -14,24 +13,24 @@ import javax.validation.ConstraintValidatorContext; * @date 2024-09-13 */ public class MobileValidator implements ConstraintValidator { - private boolean required = false; + private boolean required = false; - @Override - public void initialize(Mobile constraintAnnotation) { - required = constraintAnnotation.required(); - } + @Override + public void initialize(Mobile constraintAnnotation) { + required = constraintAnnotation.required(); + } - @Override - public boolean isValid(String value, ConstraintValidatorContext context) { - if (required) { - if (StrUtil.isBlank(value)) { - return false; - } - } else { - if (StrUtil.isBlank(value)) { - return true; - } - } - return ReUtil.isMatch(RegexConst.PHONE_NUMBER, value); + @Override + public boolean isValid(String value, ConstraintValidatorContext context) { + if (required) { + if (StrUtil.isBlank(value)) { + return false; + } + } else { + if (StrUtil.isBlank(value)) { + return true; + } } + return ReUtil.isMatch(RegexConst.PHONE_NUMBER, value); + } } diff --git a/kenaito-config-service/src/main/java/cn/odboy/infra/validate/NotEmptyList.java b/kenaito-config-service/src/main/java/cn/odboy/infra/validate/NotEmptyList.java index 1db77cf..90c3ab9 100644 --- a/kenaito-config-service/src/main/java/cn/odboy/infra/validate/NotEmptyList.java +++ b/kenaito-config-service/src/main/java/cn/odboy/infra/validate/NotEmptyList.java @@ -1,19 +1,26 @@ package cn.odboy.infra.validate; +import java.lang.annotation.*; import javax.validation.Constraint; import javax.validation.Payload; -import java.lang.annotation.*; @Documented @Constraint(validatedBy = {NotEmptyListValidator.class}) -@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE}) +@Target({ + ElementType.METHOD, + ElementType.FIELD, + ElementType.ANNOTATION_TYPE, + ElementType.CONSTRUCTOR, + ElementType.PARAMETER, + ElementType.TYPE_USE +}) @Retention(RetentionPolicy.RUNTIME) public @interface NotEmptyList { - boolean required() default false; + boolean required() default false; - String message() default "参数列表长度必须大于1"; + String message() default "参数列表长度必须大于1"; - Class[] groups() default {}; + Class[] groups() default {}; - Class[] payload() default {}; -} \ No newline at end of file + Class[] payload() default {}; +} diff --git a/kenaito-config-service/src/main/java/cn/odboy/infra/validate/NotEmptyListValidator.java b/kenaito-config-service/src/main/java/cn/odboy/infra/validate/NotEmptyListValidator.java index e9a7325..f8b97b0 100644 --- a/kenaito-config-service/src/main/java/cn/odboy/infra/validate/NotEmptyListValidator.java +++ b/kenaito-config-service/src/main/java/cn/odboy/infra/validate/NotEmptyListValidator.java @@ -1,8 +1,8 @@ package cn.odboy.infra.validate; +import java.util.List; import javax.validation.ConstraintValidator; import javax.validation.ConstraintValidatorContext; -import java.util.List; /** * 校验列表长度 @@ -11,19 +11,19 @@ import java.util.List; * @date 2024-09-13 */ public class NotEmptyListValidator implements ConstraintValidator> { - private boolean required = false; + private boolean required = false; - @Override - public void initialize(NotEmptyList constraintAnnotation) { - required = constraintAnnotation.required(); - } + @Override + public void initialize(NotEmptyList constraintAnnotation) { + required = constraintAnnotation.required(); + } - @Override - public boolean isValid(List values, ConstraintValidatorContext context) { - if (required) { - return values != null && !values.isEmpty(); - } else { - return true; - } + @Override + public boolean isValid(List values, ConstraintValidatorContext context) { + if (required) { + return values != null && !values.isEmpty(); + } else { + return true; } + } }