diff --git a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java index e0bcae4..93c2e7d 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java @@ -93,7 +93,7 @@ public class BaseValidator { this.getter = getter; } - void withRule(Predicate condition, + void withRule(Predicate condition, Function exceptionCreator) { withRule(value -> { if (!condition.test(value)) { @@ -167,22 +167,22 @@ public class BaseValidator { // ===== isTrue ===== - public THIS isTrue(Predicate condition) { + public THIS isTrue(Predicate condition) { return isTrue(condition, "无效的用户输入"); } - public THIS isTrue(Predicate condition, String errMsg) { + public THIS isTrue(Predicate condition, String errMsg) { return isTrue(condition, convertExceptionCreator(errMsg)); } public THIS isTrue( - Predicate condition, + Predicate condition, Supplier exceptionCreator) { return isTrue(condition, convertExceptionCreator(exceptionCreator)); } public THIS isTrue( - Predicate condition, + Predicate condition, Function exceptionCreator) { withRule(condition, exceptionCreator); return thisObject(); diff --git a/src/test/java/xyz/zhouxy/plusone/map/MapValidatorTests.java b/src/test/java/xyz/zhouxy/plusone/map/MapValidatorTests.java index 0f90d14..aba5fdf 100644 --- a/src/test/java/xyz/zhouxy/plusone/map/MapValidatorTests.java +++ b/src/test/java/xyz/zhouxy/plusone/map/MapValidatorTests.java @@ -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)