feat: 增加身份证验证

This commit is contained in:
wangyu 2021-09-29 09:43:48 +08:00
parent 8a5459c9be
commit 66e19e81f7
4 changed files with 84 additions and 5 deletions

View File

@ -0,0 +1,41 @@
package com.flyfish.framework.validation.annotations;
import com.flyfish.framework.validation.validators.IdCardValidator;
import com.flyfish.framework.validation.validators.MoneyValidator;
import javax.validation.Constraint;
import javax.validation.Payload;
import com.flyfish.framework.validation.annotations.IdCard.List;
import java.lang.annotation.Documented;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = {IdCardValidator.class})
@Repeatable(List.class)
public @interface IdCard {
String message() default "不是合法的身份证号!";
//分组
Class<?>[] groups() default {};
//负载
Class<? extends Payload>[] payload() default {};
//指定多个时使用
@Target({FIELD, METHOD, PARAMETER, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Documented
@interface List {
IdCard[] value();
}
}

View File

@ -0,0 +1,35 @@
package com.flyfish.framework.validation.validators;
import com.flyfish.framework.validation.annotations.IdCard;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;
/**
* 身份证验证使用正则
*
* @author wangyu
*/
public class IdCardValidator implements ConstraintValidator<IdCard, String> {
private static final Pattern PATTERN = Pattern.compile(
"^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$"
);
/**
* Implements the validation logic.
* The state of {@code value} must not be altered.
* <p>
* This method can be accessed concurrently, thread-safety must be ensured
* by the implementation.
*
* @param value object to validate
* @param context context in which the constraint is evaluated
* @return {@code false} if {@code value} does not pass the constraint
*/
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
return null == value || PATTERN.matcher(value).matches();
}
}

View File

@ -17,9 +17,9 @@ public class PhoneValidator implements ConstraintValidator<Phone, String> {
private PhoneType type; private PhoneType type;
private static final Pattern mobile = Pattern.compile("^1[3,5]\\d{9}||18[6,8,9]\\d{8}$"); private static final Pattern MOBILE = Pattern.compile("^1[3,5]\\d{9}||18[6,8,9]\\d{8}$");
private static final Pattern tel = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$"); private static final Pattern TEL = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$");
@Override @Override
public void initialize(Phone phone) { public void initialize(Phone phone) {
@ -49,10 +49,10 @@ public class PhoneValidator implements ConstraintValidator<Phone, String> {
} }
private boolean isMobile(String value) { private boolean isMobile(String value) {
return mobile.matcher(value).matches(); return MOBILE.matcher(value).matches();
} }
private boolean isTel(String value) { private boolean isTel(String value) {
return tel.matcher(value).matches(); return TEL.matcher(value).matches();
} }
} }

View File

@ -2,6 +2,7 @@ package com.flyfish.framework.beans.enums;
import com.flyfish.framework.beans.meta.BeanPropertyType; import com.flyfish.framework.beans.meta.BeanPropertyType;
import com.flyfish.framework.beans.meta.BeanValidation; import com.flyfish.framework.beans.meta.BeanValidation;
import com.flyfish.framework.validation.annotations.IdCard;
import com.flyfish.framework.validation.annotations.Phone; import com.flyfish.framework.validation.annotations.Phone;
import com.flyfish.framework.validation.enums.PhoneType; import com.flyfish.framework.validation.enums.PhoneType;
import lombok.Getter; import lombok.Getter;
@ -67,7 +68,9 @@ public enum ValidationCandidate {
CURRENCY((annotation, validation) -> validation.setValidator("currency"), Currency.class), CURRENCY((annotation, validation) -> validation.setValidator("currency"), Currency.class),
// 手机号 // 手机号
PHONE(((annotation, validation) -> validation.setValidator("phone").prop("type", PHONE(((annotation, validation) -> validation.setValidator("phone").prop("type",
annotation.getEnum("type", PhoneType.class).name().toLowerCase())), Phone.class); annotation.getEnum("type", PhoneType.class).name().toLowerCase())), Phone.class),
// 身份证号
ID_CARD(((annotation, validation) -> validation.setValidator("idCard")), IdCard.class);
private final BiConsumer<MergedAnnotation<?>, BeanValidation> mapper; private final BiConsumer<MergedAnnotation<?>, BeanValidation> mapper;