upd: 规范格式

This commit is contained in:
骑着蜗牛追导弹 2024-12-06 21:11:54 +08:00
parent 3a0a233f6a
commit 18a02ef74f
4 changed files with 58 additions and 45 deletions

View File

@ -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<? extends Payload>[] payload() default {};
Class<? extends Payload>[] payload() default {};
}

View File

@ -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<Mobile, String> {
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);
}
}

View File

@ -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<? extends Payload>[] payload() default {};
Class<? extends Payload>[] payload() default {};
}

View File

@ -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<NotEmptyList, List<?>> {
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;
}
}
}