优化代码。
parent
65c33d3a28
commit
e4221bc18b
|
@ -93,7 +93,7 @@ public class BaseValidator<T> {
|
|||
this.getter = getter;
|
||||
}
|
||||
|
||||
<E extends RuntimeException> void withRule(Predicate<PROPERTY> condition,
|
||||
<E extends RuntimeException> void withRule(Predicate<? super PROPERTY> condition,
|
||||
Function<PROPERTY, E> exceptionCreator) {
|
||||
withRule(value -> {
|
||||
if (!condition.test(value)) {
|
||||
|
@ -167,22 +167,22 @@ public class BaseValidator<T> {
|
|||
|
||||
// ===== isTrue =====
|
||||
|
||||
public THIS isTrue(Predicate<PROPERTY> condition) {
|
||||
public THIS isTrue(Predicate<? super PROPERTY> condition) {
|
||||
return isTrue(condition, "无效的用户输入");
|
||||
}
|
||||
|
||||
public THIS isTrue(Predicate<PROPERTY> condition, String errMsg) {
|
||||
public THIS isTrue(Predicate<? super PROPERTY> condition, String errMsg) {
|
||||
return isTrue(condition, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> THIS isTrue(
|
||||
Predicate<PROPERTY> condition,
|
||||
Predicate<? super PROPERTY> condition,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return isTrue(condition, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> THIS isTrue(
|
||||
Predicate<PROPERTY> condition,
|
||||
Predicate<? super PROPERTY> condition,
|
||||
Function<PROPERTY, E> exceptionCreator) {
|
||||
withRule(condition, exceptionCreator);
|
||||
return thisObject();
|
||||
|
|
|
@ -6,8 +6,10 @@ import java.util.Map;
|
|||
import java.util.Objects;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.util.Predicates;
|
||||
import xyz.zhouxy.plusone.constant.Patterns;
|
||||
import xyz.zhouxy.plusone.validator.MapValidator;
|
||||
|
||||
|
@ -42,7 +44,8 @@ class TestValidator extends MapValidator {
|
|||
TestValidator() {
|
||||
super(Arrays.asList(USERNAME, ACCOUNT, PASSWORD, AGE, BOOLEAN, ROLE_LIST));
|
||||
this.ruleForString(USERNAME)
|
||||
.notEmpty()
|
||||
.isTrue(Predicates.of(StringUtils::isNotEmpty)
|
||||
.and(Objects::nonNull))
|
||||
.matches(Patterns.USERNAME.getPattern(),
|
||||
username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username)));
|
||||
this.ruleForString(ACCOUNT)
|
||||
|
|
Loading…
Reference in New Issue