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