代码调整。
This commit is contained in:
parent
a2e01a8b8e
commit
db2678803b
6
pom.xml
6
pom.xml
@ -41,12 +41,6 @@
|
||||
<version>5.9.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>cn.hutool</groupId>
|
||||
<artifactId>hutool-core</artifactId>
|
||||
<version>5.8.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
@ -13,7 +13,7 @@ public class BaseValidator<T> {
|
||||
private final List<PropertyValidator<T, ?, ?>> propertyValidators = new ArrayList<>();
|
||||
|
||||
protected void withRule(final Predicate<T> rule, final String errorMessage) {
|
||||
withRule(rule, value -> new InvalidInputException(errorMessage));
|
||||
withRule(rule, () -> new InvalidInputException(errorMessage));
|
||||
}
|
||||
|
||||
protected <E extends RuntimeException> void withRule(Predicate<T> rule, Supplier<E> exceptionBuilder) {
|
||||
|
@ -1,6 +1,6 @@
|
||||
package xyz.zhouxy.plusone.validator;
|
||||
|
||||
import xyz.zhouxy.plusone.exception.BaseException;
|
||||
import xyz.zhouxy.plusone.commons.exception.BaseException;
|
||||
|
||||
/**
|
||||
* 4040200 - 无效的用户输入
|
||||
@ -36,18 +36,4 @@ public class InvalidInputException extends BaseException {
|
||||
public InvalidInputException(String msg, Throwable cause) {
|
||||
this(ERROR_CODE, msg, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* 不支持的 Principal 类型出现时抛出的异常
|
||||
*/
|
||||
public static InvalidInputException unsupportedPrincipalTypeException() {
|
||||
return unsupportedPrincipalTypeException("不支持的 PrincipalType");
|
||||
}
|
||||
|
||||
/**
|
||||
* 不支持的 Principal 类型出现时抛出的异常
|
||||
*/
|
||||
public static InvalidInputException unsupportedPrincipalTypeException(String message) {
|
||||
return new InvalidInputException(4040201, message);
|
||||
}
|
||||
}
|
||||
|
@ -3,11 +3,12 @@ package xyz.zhouxy.plusone.validator;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import xyz.zhouxy.plusone.constant.RegexConsts;
|
||||
import xyz.zhouxy.plusone.util.RegexUtil;
|
||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||
import xyz.zhouxy.plusone.commons.util.RegexUtil;
|
||||
|
||||
public class StringValidator<DTO> extends PropertyValidator<DTO, String, StringValidator<DTO>> {
|
||||
|
||||
@ -21,18 +22,18 @@ public class StringValidator<DTO> extends PropertyValidator<DTO, String, StringV
|
||||
|
||||
// ===== matches =====
|
||||
|
||||
public StringValidator<DTO> matches(String regex, String errMsg) {
|
||||
public StringValidator<DTO> matches(Pattern regex, String errMsg) {
|
||||
return matches(regex, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> matches(
|
||||
String regex,
|
||||
Pattern regex,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return matches(regex, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> matches(
|
||||
String regex,
|
||||
Pattern regex,
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(input -> RegexUtil.matches(input, regex), exceptionCreator);
|
||||
return this;
|
||||
@ -40,18 +41,18 @@ public class StringValidator<DTO> extends PropertyValidator<DTO, String, StringV
|
||||
|
||||
// ===== matchesOr =====
|
||||
|
||||
public StringValidator<DTO> matchesOr(String[] regexs, String errMsg) {
|
||||
public StringValidator<DTO> matchesOr(Pattern[] regexs, String errMsg) {
|
||||
return matchesOr(regexs, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesOr(
|
||||
String[] regexs,
|
||||
Pattern[] regexs,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return matchesOr(regexs, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesOr(
|
||||
String[] regexs,
|
||||
Pattern[] regexs,
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(input -> RegexUtil.matchesOr(input, regexs), exceptionCreator);
|
||||
return this;
|
||||
@ -70,24 +71,24 @@ public class StringValidator<DTO> extends PropertyValidator<DTO, String, StringV
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesOr(
|
||||
List<String> regexs,
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(input -> RegexUtil.matchesOr(input, regexs.toArray(new String[regexs.size()])), exceptionCreator);
|
||||
withRule(input -> RegexUtil.matchesOr(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
|
||||
return this;
|
||||
}
|
||||
|
||||
// ===== matchesAnd =====
|
||||
|
||||
public StringValidator<DTO> matchesAnd(String[] regexs, String errMsg) {
|
||||
public StringValidator<DTO> matchesAnd(Pattern[] regexs, String errMsg) {
|
||||
return matchesAnd(regexs, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesAnd(
|
||||
String[] regexs,
|
||||
Pattern[] regexs,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return matchesAnd(regexs, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> matchesAnd(
|
||||
String[] regexs,
|
||||
Pattern[] regexs,
|
||||
Function<String, E> exceptionCreator) {
|
||||
withRule(input -> RegexUtil.matchesAnd(input, regexs), exceptionCreator);
|
||||
return this;
|
||||
@ -128,7 +129,7 @@ public class StringValidator<DTO> extends PropertyValidator<DTO, String, StringV
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> StringValidator<DTO> email(Function<String, E> exceptionCreator) {
|
||||
return matches(RegexConsts.EMAIL, exceptionCreator);
|
||||
return matches(PatternConsts.EMAIL, exceptionCreator);
|
||||
}
|
||||
|
||||
// ====== notEmpty =====
|
||||
|
@ -3,10 +3,11 @@ package xyz.zhouxy.plusone.validator.test;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import xyz.zhouxy.plusone.constant.RegexConsts;
|
||||
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||
import xyz.zhouxy.plusone.validator.BaseValidator;
|
||||
import xyz.zhouxy.plusone.validator.ValidateUtil;
|
||||
|
||||
@ -29,17 +30,17 @@ class RegisterCommandValidator extends BaseValidator<RegisterCommand> {
|
||||
private RegisterCommandValidator() {
|
||||
ruleForString(RegisterCommand::getUsername)
|
||||
.notNull("用户名不能为空")
|
||||
.matches(RegexConsts.USERNAME,
|
||||
.matches(PatternConsts.USERNAME,
|
||||
username -> new IllegalArgumentException(String.format("用户名\"%s\"不符合规范", username)));
|
||||
ruleForString(RegisterCommand::getAccount)
|
||||
.notNull("请输入邮箱地址或手机号")
|
||||
.matchesOr(new String[] { RegexConsts.EMAIL, RegexConsts.MOBILE_PHONE }, "请输入邮箱地址或手机号");
|
||||
.matchesOr(new Pattern[] { PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE }, "请输入邮箱地址或手机号");
|
||||
ruleForString(RegisterCommand::getCode)
|
||||
.notNull("验证码不能为空")
|
||||
.matches(RegexConsts.CAPTCHA, "验证码不符合规范");
|
||||
.matches(PatternConsts.CAPTCHA, "验证码不符合规范");
|
||||
ruleForString(RegisterCommand::getPassword)
|
||||
.notEmpty("密码不能为空")
|
||||
.matches(RegexConsts.PASSWORD, "密码不符合规范");
|
||||
.matches(PatternConsts.PASSWORD, "密码不符合规范");
|
||||
ruleForCollection(RegisterCommand::getRoles)
|
||||
.notEmpty(() -> new RuntimeException("角色列表不能为空"));
|
||||
|
||||
|
@ -6,10 +6,10 @@ import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import xyz.zhouxy.plusone.constant.RegexConsts;
|
||||
import xyz.zhouxy.plusone.commons.constant.RegexConsts;
|
||||
import xyz.zhouxy.plusone.validator.BaseValidator;
|
||||
|
||||
class BaseValidator2Test {
|
||||
@ -30,8 +30,8 @@ class RegisterCommandValidator2 extends BaseValidator<RegisterCommand> {
|
||||
private RegisterCommandValidator2() {
|
||||
ruleForString(RegisterCommand::getUsername)
|
||||
.state(((Predicate<String>) Objects::nonNull)
|
||||
.and(StrUtil::isNotEmpty)
|
||||
.and(StrUtil::isNotBlank)
|
||||
.and(StringUtils::isNotEmpty)
|
||||
.and(StringUtils::isNotBlank)
|
||||
.and(username -> Pattern.matches(RegexConsts.EMAIL, username)),
|
||||
(username -> new IllegalArgumentException(String.format("用户名\"%s\"不符合规范", username))));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user