From b6e823a21137c46104355982c8d8cba833a1539a Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Fri, 24 Feb 2023 09:45:04 +0800 Subject: [PATCH 01/26] =?UTF-8?q?=E5=8D=87=E7=BA=A7=20Junit5=EF=BC=8C?= =?UTF-8?q?=E7=A7=BB=E9=99=A4=20hutool=EF=BC=8C=E4=BD=BF=E7=94=A8=20common?= =?UTF-8?q?s-lang3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 31 +++++++++++++++---- .../plusone/validator/StringValidator.java | 5 +-- .../validator/test/BaseValidatorTest.java | 6 ++-- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index 8c9f4fd..5b572d2 100644 --- a/pom.xml +++ b/pom.xml @@ -1,7 +1,7 @@ - + 4.0.0 xyz.zhouxy.plusone @@ -15,6 +15,7 @@ UTF-8 1.8 1.8 + 3.12.0 @@ -22,17 +23,35 @@ xyz.zhouxy.plusone plusone-commons 0.1.0-SNAPSHOT + + + cn.hutool + hutool-core + + - junit - junit - 4.11 + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + org.junit.jupiter + junit-jupiter-api + 5.9.2 + test + + + cn.hutool + hutool-core + 5.8.12 test - + + diff --git a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java index 7e02756..b424c46 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java @@ -4,7 +4,8 @@ import java.util.List; import java.util.function.Function; import java.util.function.Supplier; -import cn.hutool.core.util.StrUtil; +import org.apache.commons.lang3.StringUtils; + import xyz.zhouxy.plusone.constant.RegexConsts; import xyz.zhouxy.plusone.util.RegexUtil; @@ -108,7 +109,7 @@ public class StringValidator extends PropertyValidator StringValidator notBlank( Function exceptionCreator) { - withRule(input -> StrUtil.isNotBlank(input), exceptionCreator); + withRule(StringUtils::isNotBlank, exceptionCreator); return this; } diff --git a/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java b/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java index d9ffe81..92082c0 100644 --- a/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java +++ b/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java @@ -4,15 +4,15 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; -import org.junit.Test; +import org.junit.jupiter.api.Test; import xyz.zhouxy.plusone.constant.RegexConsts; import xyz.zhouxy.plusone.validator.BaseValidator; import xyz.zhouxy.plusone.validator.ValidateUtil; -public class BaseValidatorTest { +class BaseValidatorTest { @Test - public void testValidate() { + void testValidate() { RegisterCommand registerCommand = new RegisterCommand("zhouxy108", "luquanlion@outlook.com", "22336", "A1b2C3d4", "A1b2C3d4", Arrays.asList(new String[] { "admin", "editor" })); From a2e01a8b8e5c72f1252e26d9c2d3d047100c0702 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Fri, 24 Feb 2023 09:45:33 +0800 Subject: [PATCH 02/26] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../validator2/test/BaseValidator2Test.java | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java diff --git a/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java b/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java new file mode 100644 index 0000000..c067cc5 --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java @@ -0,0 +1,121 @@ +package xyz.zhouxy.plusone.validator2.test; + +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import java.util.function.Predicate; +import java.util.regex.Pattern; + +import org.junit.jupiter.api.Test; + +import cn.hutool.core.util.StrUtil; +import xyz.zhouxy.plusone.constant.RegexConsts; +import xyz.zhouxy.plusone.validator.BaseValidator; + +class BaseValidator2Test { + @Test + void testValidate() { + RegisterCommand registerCommand = new RegisterCommand("null", "luquanlion@outlook.com", "22336", "A1b2C3d4", + "A1b2C3d4", + Arrays.asList(new String[] { "admin", "editor" })); + RegisterCommandValidator2.INSTANCE.validate(registerCommand); + System.out.println(registerCommand); + } +} + +class RegisterCommandValidator2 extends BaseValidator { + + static final RegisterCommandValidator2 INSTANCE = new RegisterCommandValidator2(); + + private RegisterCommandValidator2() { + ruleForString(RegisterCommand::getUsername) + .state(((Predicate) Objects::nonNull) + .and(StrUtil::isNotEmpty) + .and(StrUtil::isNotBlank) + .and(username -> Pattern.matches(RegexConsts.EMAIL, username)), + (username -> new IllegalArgumentException(String.format("用户名\"%s\"不符合规范", username)))); + } +} + +/** + * RegisterCommand + */ +class RegisterCommand { + + private String username; + private String account; + private String code; + private String password; + private String password2; + private List roles; + + public RegisterCommand() { + } + + public RegisterCommand(String username, String account, String code, String password, String password2, + List roles) { + this.username = username; + this.account = account; + this.code = code; + this.password = password; + this.password2 = password2; + this.roles = roles; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getAccount() { + return account; + } + + public void setAccount(String account) { + this.account = account; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getPassword2() { + return password2; + } + + public void setPassword2(String password2) { + this.password2 = password2; + } + + public List getRoles() { + return roles; + } + + public void setRoles(List roles) { + this.roles = roles; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("RegisterCommand [username=").append(username).append(", account=").append(account) + .append(", code=").append(code).append(", password=").append(password).append(", password2=") + .append(password2).append(", roles=").append(roles).append("]"); + return builder.toString(); + } +} From db2678803bba6dbd8e79c0e745fabb91542d2d96 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Fri, 24 Feb 2023 11:20:15 +0800 Subject: [PATCH 03/26] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ----- .../plusone/validator/BaseValidator.java | 2 +- .../validator/InvalidInputException.java | 16 +---------- .../plusone/validator/StringValidator.java | 27 ++++++++++--------- .../validator/test/BaseValidatorTest.java | 11 ++++---- .../validator2/test/BaseValidator2Test.java | 8 +++--- 6 files changed, 26 insertions(+), 44 deletions(-) diff --git a/pom.xml b/pom.xml index 5b572d2..c21194c 100644 --- a/pom.xml +++ b/pom.xml @@ -41,12 +41,6 @@ 5.9.2 test - - cn.hutool - hutool-core - 5.8.12 - test - diff --git a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java index 1782fc9..ae00ac2 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java @@ -13,7 +13,7 @@ public class BaseValidator { private final List> propertyValidators = new ArrayList<>(); protected void withRule(final Predicate rule, final String errorMessage) { - withRule(rule, value -> new InvalidInputException(errorMessage)); + withRule(rule, () -> new InvalidInputException(errorMessage)); } protected void withRule(Predicate rule, Supplier exceptionBuilder) { diff --git a/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java b/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java index b770344..b8066cf 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java @@ -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); - } } diff --git a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java index b424c46..e3c51c5 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java @@ -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 extends PropertyValidator> { @@ -21,18 +22,18 @@ public class StringValidator extends PropertyValidator matches(String regex, String errMsg) { + public StringValidator matches(Pattern regex, String errMsg) { return matches(regex, convertExceptionCreator(errMsg)); } public StringValidator matches( - String regex, + Pattern regex, Supplier exceptionCreator) { return matches(regex, convertExceptionCreator(exceptionCreator)); } public StringValidator matches( - String regex, + Pattern regex, Function exceptionCreator) { withRule(input -> RegexUtil.matches(input, regex), exceptionCreator); return this; @@ -40,18 +41,18 @@ public class StringValidator extends PropertyValidator matchesOr(String[] regexs, String errMsg) { + public StringValidator matchesOr(Pattern[] regexs, String errMsg) { return matchesOr(regexs, convertExceptionCreator(errMsg)); } public StringValidator matchesOr( - String[] regexs, + Pattern[] regexs, Supplier exceptionCreator) { return matchesOr(regexs, convertExceptionCreator(exceptionCreator)); } public StringValidator matchesOr( - String[] regexs, + Pattern[] regexs, Function exceptionCreator) { withRule(input -> RegexUtil.matchesOr(input, regexs), exceptionCreator); return this; @@ -70,24 +71,24 @@ public class StringValidator extends PropertyValidator StringValidator matchesOr( List regexs, Function 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 matchesAnd(String[] regexs, String errMsg) { + public StringValidator matchesAnd(Pattern[] regexs, String errMsg) { return matchesAnd(regexs, convertExceptionCreator(errMsg)); } public StringValidator matchesAnd( - String[] regexs, + Pattern[] regexs, Supplier exceptionCreator) { return matchesAnd(regexs, convertExceptionCreator(exceptionCreator)); } public StringValidator matchesAnd( - String[] regexs, + Pattern[] regexs, Function exceptionCreator) { withRule(input -> RegexUtil.matchesAnd(input, regexs), exceptionCreator); return this; @@ -128,7 +129,7 @@ public class StringValidator extends PropertyValidator StringValidator email(Function exceptionCreator) { - return matches(RegexConsts.EMAIL, exceptionCreator); + return matches(PatternConsts.EMAIL, exceptionCreator); } // ====== notEmpty ===== diff --git a/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java b/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java index 92082c0..135cf14 100644 --- a/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java +++ b/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java @@ -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 { 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("角色列表不能为空")); diff --git a/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java b/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java index c067cc5..9aa5dec 100644 --- a/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java +++ b/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java @@ -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 { private RegisterCommandValidator2() { ruleForString(RegisterCommand::getUsername) .state(((Predicate) 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)))); } From 55089dbb258663ecf474265aa68e3d99e949649e Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Fri, 24 Feb 2023 11:49:13 +0800 Subject: [PATCH 04/26] bugfix. --- .../plusone/validator/StringValidator.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java index e3c51c5..b833533 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java @@ -1,5 +1,6 @@ package xyz.zhouxy.plusone.validator; +import java.util.Collection; import java.util.List; import java.util.function.Function; import java.util.function.Supplier; @@ -58,18 +59,18 @@ public class StringValidator extends PropertyValidator matchesOr(List regexs, String errMsg) { + public StringValidator matchesOr(List regexs, String errMsg) { return matchesOr(regexs, convertExceptionCreator(errMsg)); } public StringValidator matchesOr( - List regexs, + List regexs, Supplier exceptionCreator) { return matchesOr(regexs, convertExceptionCreator(exceptionCreator)); } public StringValidator matchesOr( - List regexs, + List regexs, Function exceptionCreator) { withRule(input -> RegexUtil.matchesOr(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); return this; @@ -94,6 +95,23 @@ public class StringValidator extends PropertyValidator matchesAnd(Collection regexs, String errMsg) { + return matchesAnd(regexs, convertExceptionCreator(errMsg)); + } + + public StringValidator matchesAnd( + Collection regexs, + Supplier exceptionCreator) { + return matchesAnd(regexs, convertExceptionCreator(exceptionCreator)); + } + + public StringValidator matchesAnd( + Collection regexs, + Function exceptionCreator) { + withRule(input -> RegexUtil.matchesAnd(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); + return this; + } + // ===== notBlank ===== public StringValidator notBlank() { From fd4c4db4112ddc1f586481d3ac75a5ac7874ce7b Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 3 Apr 2023 23:41:21 +0800 Subject: [PATCH 05/26] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BE=9D=E8=B5=96?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/pom.xml b/pom.xml index c21194c..c4aac16 100644 --- a/pom.xml +++ b/pom.xml @@ -23,12 +23,6 @@ xyz.zhouxy.plusone plusone-commons 0.1.0-SNAPSHOT - - - cn.hutool - hutool-core - - org.apache.commons From 5dc37c081e80320541d436aca3b9da01c0c4c02d Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 3 Apr 2023 23:41:51 +0800 Subject: [PATCH 06/26] =?UTF-8?q?VS=20Code=20=E9=85=8D=E7=BD=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 13f5352..4fa5537 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,4 +1,5 @@ { "java.configuration.updateBuildConfiguration": "automatic", - "java.dependency.packagePresentation": "hierarchical" -} \ No newline at end of file + "java.dependency.packagePresentation": "hierarchical", + "java.compile.nullAnalysis.mode": "automatic" +} From 17f7a1f4ac2e7660028f76fc22b112c4434b5894 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 3 Apr 2023 23:56:32 +0800 Subject: [PATCH 07/26] fix bug. --- src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java index b833533..7996810 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java @@ -185,7 +185,7 @@ public class StringValidator extends PropertyValidator exceptionCreator) { withRule(value -> { if (value == null) { - return false; + return true; } return value.isEmpty(); }, exceptionCreator); From 3c14fb06561e3f6ce0ae0a39b62d69d36de58145 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 4 Apr 2023 00:03:46 +0800 Subject: [PATCH 08/26] =?UTF-8?q?=E7=AE=80=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/validator/StringValidator.java | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java index 7996810..8494fd9 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java @@ -162,12 +162,7 @@ public class StringValidator extends PropertyValidator StringValidator notEmpty( Function exceptionCreator) { - withRule(value -> { - if (value == null) { - return false; - } - return !(value.isEmpty()); - }, exceptionCreator); + withRule(value -> (value != null) && (!value.isEmpty()), exceptionCreator); return this; } @@ -183,12 +178,7 @@ public class StringValidator extends PropertyValidator StringValidator isEmpty( Function exceptionCreator) { - withRule(value -> { - if (value == null) { - return true; - } - return value.isEmpty(); - }, exceptionCreator); + withRule(value -> value == null || value.isEmpty(), exceptionCreator); return this; } From e9918b274a2f96195dce6d94e0221c4ce9577250 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 4 Apr 2023 00:06:34 +0800 Subject: [PATCH 09/26] =?UTF-8?q?=E7=AE=80=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/xyz/zhouxy/plusone/validator/StringValidator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java index 8494fd9..f1676b0 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java @@ -162,7 +162,7 @@ public class StringValidator extends PropertyValidator StringValidator notEmpty( Function exceptionCreator) { - withRule(value -> (value != null) && (!value.isEmpty()), exceptionCreator); + withRule(StringUtils::isNotEmpty, exceptionCreator); return this; } @@ -178,7 +178,7 @@ public class StringValidator extends PropertyValidator StringValidator isEmpty( Function exceptionCreator) { - withRule(value -> value == null || value.isEmpty(), exceptionCreator); + withRule(StringUtils::isEmpty, exceptionCreator); return this; } From db909ff93f7675485e615c35ebb258be92d62ba5 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 4 Apr 2023 00:07:32 +0800 Subject: [PATCH 10/26] fix bug. --- .../java/xyz/zhouxy/plusone/validator/CollectionValidator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java index 5a79048..d4ab7ba 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java @@ -45,7 +45,7 @@ public class CollectionValidator extends PropertyValidator, E> exceptionCreator) { withRule(value -> { if (value == null) { - return false; + return true; } return ((Collection) value).isEmpty(); }, exceptionCreator); From b84db2ce569ba176fe7fac2d39d7e6cd52dadb08 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 4 Apr 2023 00:19:56 +0800 Subject: [PATCH 11/26] =?UTF-8?q?=E5=BC=95=E5=85=A5=20commons-collections4?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pom.xml b/pom.xml index c4aac16..1a99675 100644 --- a/pom.xml +++ b/pom.xml @@ -16,6 +16,7 @@ 1.8 1.8 3.12.0 + 4.4 @@ -29,6 +30,11 @@ commons-lang3 ${commons-lang3.version} + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + org.junit.jupiter junit-jupiter-api From 0a4ad3a5bac48418f17fee26eedb272af3049803 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 4 Apr 2023 00:20:02 +0800 Subject: [PATCH 12/26] =?UTF-8?q?=E7=AE=80=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/validator/BoolValidator.java | 8 +++++--- .../plusone/validator/CollectionValidator.java | 16 ++++------------ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/BoolValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/BoolValidator.java index 9912c37..fa408f6 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/BoolValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/BoolValidator.java @@ -3,6 +3,8 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; import java.util.function.Supplier; +import org.apache.commons.lang3.BooleanUtils; + public class BoolValidator extends PropertyValidator> { BoolValidator(Function getter) { @@ -25,7 +27,7 @@ public class BoolValidator extends PropertyValidator BoolValidator isTrue( Function exceptionCreator) { - withRule(Boolean.TRUE::equals, exceptionCreator); + withRule(BooleanUtils::isTrue, exceptionCreator); return this; } @@ -45,10 +47,10 @@ public class BoolValidator extends PropertyValidator BoolValidator isFalse( Function exceptionCreator) { - withRule(Boolean.FALSE::equals, exceptionCreator); + withRule(BooleanUtils::isFalse, exceptionCreator); return this; } - + @Override protected BoolValidator thisObject() { return this; diff --git a/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java index d4ab7ba..4526988 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java @@ -4,6 +4,8 @@ import java.util.Collection; import java.util.function.Function; import java.util.function.Supplier; +import org.apache.commons.collections4.CollectionUtils; + public class CollectionValidator extends PropertyValidator, CollectionValidator> { CollectionValidator(Function> getter) { @@ -22,12 +24,7 @@ public class CollectionValidator extends PropertyValidator CollectionValidator notEmpty( Function, E> exceptionCreator) { - withRule(value -> { - if (value == null) { - return false; - } - return !((Collection) value).isEmpty(); - }, exceptionCreator); + withRule(CollectionUtils::isNotEmpty, exceptionCreator); return this; } @@ -43,12 +40,7 @@ public class CollectionValidator extends PropertyValidator CollectionValidator isEmpty( Function, E> exceptionCreator) { - withRule(value -> { - if (value == null) { - return true; - } - return ((Collection) value).isEmpty(); - }, exceptionCreator); + withRule(CollectionUtils::isEmpty, exceptionCreator); return this; } From 3b040a442d00e980be6ae16f9339297b0928a3ca Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 4 Apr 2023 00:20:51 +0800 Subject: [PATCH 13/26] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E5=8F=B7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 1a99675..9301502 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ xyz.zhouxy.plusone plusone-validator - 0.1.2-SNAPSHOT + 0.1.3-SNAPSHOT plusone-validator http://zhouxy.xyz From e4af5b33b3cff966383797c07c606f68f5cebc76 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 4 Apr 2023 14:58:41 +0800 Subject: [PATCH 14/26] =?UTF-8?q?=E6=9A=82=E6=97=B6=E7=94=A8=E4=B8=8D?= =?UTF-8?q?=E5=88=B0=20commons-collections4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ------ .../xyz/zhouxy/plusone/validator/CollectionValidator.java | 6 ++---- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/pom.xml b/pom.xml index 9301502..12d02a5 100644 --- a/pom.xml +++ b/pom.xml @@ -16,7 +16,6 @@ 1.8 1.8 3.12.0 - 4.4 @@ -30,11 +29,6 @@ commons-lang3 ${commons-lang3.version} - - org.apache.commons - commons-collections4 - ${commons-collections4.version} - org.junit.jupiter junit-jupiter-api diff --git a/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java index 4526988..cb434a1 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java @@ -4,8 +4,6 @@ import java.util.Collection; import java.util.function.Function; import java.util.function.Supplier; -import org.apache.commons.collections4.CollectionUtils; - public class CollectionValidator extends PropertyValidator, CollectionValidator> { CollectionValidator(Function> getter) { @@ -24,7 +22,7 @@ public class CollectionValidator extends PropertyValidator CollectionValidator notEmpty( Function, E> exceptionCreator) { - withRule(CollectionUtils::isNotEmpty, exceptionCreator); + withRule(value -> value != null && !value.isEmpty(), exceptionCreator); return this; } @@ -40,7 +38,7 @@ public class CollectionValidator extends PropertyValidator CollectionValidator isEmpty( Function, E> exceptionCreator) { - withRule(CollectionUtils::isEmpty, exceptionCreator); + withRule(value -> value == null || value.isEmpty(), exceptionCreator); return this; } From 4a841db2ee6b09294257045b5fe116efd7f8857e Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 16 Apr 2023 00:52:18 +0800 Subject: [PATCH 15/26] =?UTF-8?q?=E8=B0=83=E6=95=B4=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java index f1676b0..d601241 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java @@ -2,6 +2,7 @@ package xyz.zhouxy.plusone.validator; import java.util.Collection; import java.util.List; +import java.util.Objects; import java.util.function.Function; import java.util.function.Supplier; import java.util.regex.Pattern; From 0e2b1ffd4836947eada4caef7abc94dfd31b82b0 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 23 Apr 2023 22:48:31 +0800 Subject: [PATCH 16/26] Organize imports. --- src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java index d601241..f1676b0 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java @@ -2,7 +2,6 @@ package xyz.zhouxy.plusone.validator; import java.util.Collection; import java.util.List; -import java.util.Objects; import java.util.function.Function; import java.util.function.Supplier; import java.util.regex.Pattern; From 7ae8a5b6d361f7c2e7c29cc7b16a29791af67966 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 19 Jul 2023 02:38:52 +0800 Subject: [PATCH 17/26] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E6=94=B9=E6=96=B9=E6=B3=95=E5=90=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/validator/PropertyValidator.java | 32 ++++++------ .../plusone/validator/StringValidator.java | 52 +++++++++---------- .../validator/test/BaseValidatorTest.java | 3 +- .../validator2/test/BaseValidator2Test.java | 25 +++++---- 4 files changed, 55 insertions(+), 57 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java index 2adf9b9..a0298d2 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java @@ -87,46 +87,46 @@ abstract class PropertyValidator { return thisObject(); } - // ===== state ===== + // ===== isTrue ===== - public THIS state(Predicate condition) { - return state(condition, "无效的用户输入"); + public THIS isTrue(Predicate condition) { + return isTrue(condition, "无效的用户输入"); } - public THIS state(Predicate condition, String errMsg) { - return state(condition, convertExceptionCreator(errMsg)); + public THIS isTrue(Predicate condition, String errMsg) { + return isTrue(condition, convertExceptionCreator(errMsg)); } - public THIS state( + public THIS isTrue( Predicate condition, Supplier exceptionCreator) { - return state(condition, convertExceptionCreator(exceptionCreator)); + return isTrue(condition, convertExceptionCreator(exceptionCreator)); } - public THIS state( + public THIS isTrue( Predicate condition, Function exceptionCreator) { withRule(condition, exceptionCreator); return thisObject(); } - // ===== state ===== + // ===== isTrue ===== - public THIS state(Collection> conditions) { - return state(conditions, "无效的用户输入"); + public THIS isTrue(Collection> conditions) { + return isTrue(conditions, "无效的用户输入"); } - public THIS state(Collection> conditions, String errMsg) { - return state(conditions, convertExceptionCreator(errMsg)); + public THIS isTrue(Collection> conditions, String errMsg) { + return isTrue(conditions, convertExceptionCreator(errMsg)); } - public THIS state( + public THIS isTrue( Collection> conditions, Supplier exceptionCreator) { - return state(conditions, convertExceptionCreator(exceptionCreator)); + return isTrue(conditions, convertExceptionCreator(exceptionCreator)); } - public THIS state( + public THIS isTrue( Collection> conditions, Function exceptionCreator) { for (Predicate condition : conditions) { diff --git a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java index f1676b0..77824e3 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java @@ -40,75 +40,75 @@ public class StringValidator extends PropertyValidator matchesOr(Pattern[] regexs, String errMsg) { - return matchesOr(regexs, convertExceptionCreator(errMsg)); + public StringValidator matchesOne(Pattern[] regexs, String errMsg) { + return matchesOne(regexs, convertExceptionCreator(errMsg)); } - public StringValidator matchesOr( + public StringValidator matchesOne( Pattern[] regexs, Supplier exceptionCreator) { - return matchesOr(regexs, convertExceptionCreator(exceptionCreator)); + return matchesOne(regexs, convertExceptionCreator(exceptionCreator)); } - public StringValidator matchesOr( + public StringValidator matchesOne( Pattern[] regexs, Function exceptionCreator) { - withRule(input -> RegexUtil.matchesOr(input, regexs), exceptionCreator); + withRule(input -> RegexUtil.matchesOne(input, regexs), exceptionCreator); return this; } - public StringValidator matchesOr(List regexs, String errMsg) { - return matchesOr(regexs, convertExceptionCreator(errMsg)); + public StringValidator matchesOne(List regexs, String errMsg) { + return matchesOne(regexs, convertExceptionCreator(errMsg)); } - public StringValidator matchesOr( + public StringValidator matchesOne( List regexs, Supplier exceptionCreator) { - return matchesOr(regexs, convertExceptionCreator(exceptionCreator)); + return matchesOne(regexs, convertExceptionCreator(exceptionCreator)); } - public StringValidator matchesOr( + public StringValidator matchesOne( List regexs, Function exceptionCreator) { - withRule(input -> RegexUtil.matchesOr(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); + withRule(input -> RegexUtil.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); return this; } - // ===== matchesAnd ===== + // ===== matchesAll ===== - public StringValidator matchesAnd(Pattern[] regexs, String errMsg) { - return matchesAnd(regexs, convertExceptionCreator(errMsg)); + public StringValidator matchesAll(Pattern[] regexs, String errMsg) { + return matchesAll(regexs, convertExceptionCreator(errMsg)); } - public StringValidator matchesAnd( + public StringValidator matchesAll( Pattern[] regexs, Supplier exceptionCreator) { - return matchesAnd(regexs, convertExceptionCreator(exceptionCreator)); + return matchesAll(regexs, convertExceptionCreator(exceptionCreator)); } - public StringValidator matchesAnd( + public StringValidator matchesAll( Pattern[] regexs, Function exceptionCreator) { - withRule(input -> RegexUtil.matchesAnd(input, regexs), exceptionCreator); + withRule(input -> RegexUtil.matchesAll(input, regexs), exceptionCreator); return this; } - public StringValidator matchesAnd(Collection regexs, String errMsg) { - return matchesAnd(regexs, convertExceptionCreator(errMsg)); + public StringValidator matchesAll(Collection regexs, String errMsg) { + return matchesAll(regexs, convertExceptionCreator(errMsg)); } - public StringValidator matchesAnd( + public StringValidator matchesAll( Collection regexs, Supplier exceptionCreator) { - return matchesAnd(regexs, convertExceptionCreator(exceptionCreator)); + return matchesAll(regexs, convertExceptionCreator(exceptionCreator)); } - public StringValidator matchesAnd( + public StringValidator matchesAll( Collection regexs, Function exceptionCreator) { - withRule(input -> RegexUtil.matchesAnd(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); + withRule(input -> RegexUtil.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); return this; } diff --git a/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java b/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java index 135cf14..6cc351b 100644 --- a/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java +++ b/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java @@ -3,7 +3,6 @@ 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; @@ -34,7 +33,7 @@ class RegisterCommandValidator extends BaseValidator { username -> new IllegalArgumentException(String.format("用户名\"%s\"不符合规范", username))); ruleForString(RegisterCommand::getAccount) .notNull("请输入邮箱地址或手机号") - .matchesOr(new Pattern[] { PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE }, "请输入邮箱地址或手机号"); + .matchesOne(Arrays.asList(PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE), "请输入邮箱地址或手机号"); ruleForString(RegisterCommand::getCode) .notNull("验证码不能为空") .matches(PatternConsts.CAPTCHA, "验证码不符合规范"); diff --git a/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java b/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java index 9aa5dec..257bd30 100644 --- a/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java +++ b/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java @@ -3,37 +3,36 @@ package xyz.zhouxy.plusone.validator2.test; import java.util.Arrays; import java.util.List; 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 xyz.zhouxy.plusone.commons.constant.RegexConsts; +import xyz.zhouxy.plusone.commons.constant.PatternConsts; +import xyz.zhouxy.plusone.commons.function.Predicates; +import xyz.zhouxy.plusone.commons.util.RegexUtil; import xyz.zhouxy.plusone.validator.BaseValidator; class BaseValidator2Test { @Test void testValidate() { - RegisterCommand registerCommand = new RegisterCommand("null", "luquanlion@outlook.com", "22336", "A1b2C3d4", - "A1b2C3d4", - Arrays.asList(new String[] { "admin", "editor" })); - RegisterCommandValidator2.INSTANCE.validate(registerCommand); + RegisterCommand registerCommand = new RegisterCommand("null", "luquanlion@outlook.com", "22336", + "A1b2C3d4", "A1b2C3d4", Arrays.asList(new String[] { "admin", "editor" })); + RegisterCommandValidator.INSTANCE.validate(registerCommand); System.out.println(registerCommand); } } -class RegisterCommandValidator2 extends BaseValidator { +class RegisterCommandValidator extends BaseValidator { - static final RegisterCommandValidator2 INSTANCE = new RegisterCommandValidator2(); + static final RegisterCommandValidator INSTANCE = new RegisterCommandValidator(); - private RegisterCommandValidator2() { + private RegisterCommandValidator() { ruleForString(RegisterCommand::getUsername) - .state(((Predicate) Objects::nonNull) + .isTrue(Predicates.of(Objects::nonNull) .and(StringUtils::isNotEmpty) .and(StringUtils::isNotBlank) - .and(username -> Pattern.matches(RegexConsts.EMAIL, username)), - (username -> new IllegalArgumentException(String.format("用户名\"%s\"不符合规范", username)))); + .and(username -> RegexUtil.matches(username, PatternConsts.EMAIL)), + username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username))); } } From dc09453a1a7f5f0975a70f955eb33905027f316a Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 19 Jul 2023 02:39:02 +0800 Subject: [PATCH 18/26] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/xyz/zhouxy/plusone/validator/IntValidator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/IntValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/IntValidator.java index 29fc24f..1cdf093 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/IntValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/IntValidator.java @@ -10,7 +10,7 @@ public class IntValidator extends PropertyValidator between(int min, int max) { - return between(min, max, String.format("数值不在 %s 和 %s 之间", String.valueOf(min), String.valueOf(max))); + return between(min, max, String.format("数值不在 %d 和 %d 之间", min, max)); } public IntValidator between(int min, int max, String errMsg) { From 5dbdcc7d101a78a722922557789f1891c80a6d02 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 19 Jul 2023 10:34:02 +0800 Subject: [PATCH 19/26] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=B1=BB=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../validator2/test/BaseValidator2Test.java | 120 -------------- .../validator2/test/ValidatorTests.java | 147 ++++++++++++++++++ 2 files changed, 147 insertions(+), 120 deletions(-) delete mode 100644 src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java create mode 100644 src/test/java/xyz/zhouxy/plusone/validator2/test/ValidatorTests.java diff --git a/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java b/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java deleted file mode 100644 index 257bd30..0000000 --- a/src/test/java/xyz/zhouxy/plusone/validator2/test/BaseValidator2Test.java +++ /dev/null @@ -1,120 +0,0 @@ -package xyz.zhouxy.plusone.validator2.test; - -import java.util.Arrays; -import java.util.List; -import java.util.Objects; - -import org.apache.commons.lang3.StringUtils; -import org.junit.jupiter.api.Test; - -import xyz.zhouxy.plusone.commons.constant.PatternConsts; -import xyz.zhouxy.plusone.commons.function.Predicates; -import xyz.zhouxy.plusone.commons.util.RegexUtil; -import xyz.zhouxy.plusone.validator.BaseValidator; - -class BaseValidator2Test { - @Test - void testValidate() { - RegisterCommand registerCommand = new RegisterCommand("null", "luquanlion@outlook.com", "22336", - "A1b2C3d4", "A1b2C3d4", Arrays.asList(new String[] { "admin", "editor" })); - RegisterCommandValidator.INSTANCE.validate(registerCommand); - System.out.println(registerCommand); - } -} - -class RegisterCommandValidator extends BaseValidator { - - static final RegisterCommandValidator INSTANCE = new RegisterCommandValidator(); - - private RegisterCommandValidator() { - ruleForString(RegisterCommand::getUsername) - .isTrue(Predicates.of(Objects::nonNull) - .and(StringUtils::isNotEmpty) - .and(StringUtils::isNotBlank) - .and(username -> RegexUtil.matches(username, PatternConsts.EMAIL)), - username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username))); - } -} - -/** - * RegisterCommand - */ -class RegisterCommand { - - private String username; - private String account; - private String code; - private String password; - private String password2; - private List roles; - - public RegisterCommand() { - } - - public RegisterCommand(String username, String account, String code, String password, String password2, - List roles) { - this.username = username; - this.account = account; - this.code = code; - this.password = password; - this.password2 = password2; - this.roles = roles; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getAccount() { - return account; - } - - public void setAccount(String account) { - this.account = account; - } - - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = code; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getPassword2() { - return password2; - } - - public void setPassword2(String password2) { - this.password2 = password2; - } - - public List getRoles() { - return roles; - } - - public void setRoles(List roles) { - this.roles = roles; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("RegisterCommand [username=").append(username).append(", account=").append(account) - .append(", code=").append(code).append(", password=").append(password).append(", password2=") - .append(password2).append(", roles=").append(roles).append("]"); - return builder.toString(); - } -} diff --git a/src/test/java/xyz/zhouxy/plusone/validator2/test/ValidatorTests.java b/src/test/java/xyz/zhouxy/plusone/validator2/test/ValidatorTests.java new file mode 100644 index 0000000..372052d --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/validator2/test/ValidatorTests.java @@ -0,0 +1,147 @@ +package xyz.zhouxy.plusone.validator2.test; + +import static xyz.zhouxy.plusone.commons.constant.PatternConsts.*; + +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; + +import com.google.common.base.Preconditions; + +import xyz.zhouxy.plusone.commons.function.Predicates; +import xyz.zhouxy.plusone.commons.util.MoreCollections; +import xyz.zhouxy.plusone.commons.util.RegexUtil; +import xyz.zhouxy.plusone.validator.Validator; + +class ValidatorTests { + @Test + void testValidate() { + RegisterCommand registerCommand = new RegisterCommand( + "null", "luquanlion@outlook.com", "22336", + "A1b2C3d4", "A1b2C3d4", + Arrays.asList(new String[] { "admin", "editor" })); + + Validator registerCommandValidator = new Validator() + // 传入 predicate 和 Function + .addRule(command -> { + String username = command.getUsername(); + return Objects.nonNull(username) + && StringUtils.isNotEmpty(username) + && StringUtils.isNotBlank(username) + && RegexUtil.matches(username, USERNAME); + }, command -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", command.getUsername()))) + // 传入 predicate 和 error message + .addRule(command -> Predicates + .of(Objects::nonNull) + .and(account -> RegexUtil.matchesOne(account, new Pattern[] { EMAIL, MOBILE_PHONE })) + .test(command.getAccount()), + "请输入邮箱地址或手机号") + // 传入 rule + .addRule(command -> { + String code = command.getCode(); + Preconditions.checkArgument(Objects.nonNull(code), "验证码不能为空"); + Preconditions.checkArgument(RegexUtil.matches(code, CAPTCHA), "验证码不符合规范"); + }) + // 传入 rule + .addRule(command -> { + String password = command.getPassword(); + Preconditions.checkArgument(StringUtils.isNotEmpty(password), "密码不能为空"); + Preconditions.checkArgument(RegexUtil.matches(password, PASSWORD), "密码不符合规范"); + }) + // 传入 predicate 和 Supplier + .addRule(command -> MoreCollections.isNotEmpty(command.getRoles()), + () -> new RuntimeException("角色列表不能为空")) + // 传入 predicate 和 error message + .addRule(command -> Objects.equals(command.getPassword(), command.getPassword2()), + "两次输入的密码不一致"); + registerCommandValidator.validate(registerCommand); + System.out.println(registerCommand); + } +} + +/** + * RegisterCommand + */ +class RegisterCommand { + + private String username; + private String account; + private String code; + private String password; + private String password2; + private List roles; + + public RegisterCommand() { + } + + public RegisterCommand(String username, String account, String code, String password, String password2, + List roles) { + this.username = username; + this.account = account; + this.code = code; + this.password = password; + this.password2 = password2; + this.roles = roles; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getAccount() { + return account; + } + + public void setAccount(String account) { + this.account = account; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getPassword2() { + return password2; + } + + public void setPassword2(String password2) { + this.password2 = password2; + } + + public List getRoles() { + return roles; + } + + public void setRoles(List roles) { + this.roles = roles; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("RegisterCommand [username=").append(username).append(", account=").append(account) + .append(", code=").append(code).append(", password=").append(password).append(", password2=") + .append(password2).append(", roles=").append(roles).append("]"); + return builder.toString(); + } +} From 1d4eadaefeb24a25deaab14c948fc42d15700152 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 19 Jul 2023 10:34:21 +0800 Subject: [PATCH 20/26] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=B1=BB=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/validator/test/BaseValidatorTest.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java b/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java index 6cc351b..d6e2927 100644 --- a/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java +++ b/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java @@ -4,9 +4,12 @@ import java.util.Arrays; import java.util.List; import java.util.Objects; +import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Test; import xyz.zhouxy.plusone.commons.constant.PatternConsts; +import xyz.zhouxy.plusone.commons.function.Predicates; +import xyz.zhouxy.plusone.commons.util.RegexUtil; import xyz.zhouxy.plusone.validator.BaseValidator; import xyz.zhouxy.plusone.validator.ValidateUtil; @@ -28,9 +31,11 @@ class RegisterCommandValidator extends BaseValidator { private RegisterCommandValidator() { ruleForString(RegisterCommand::getUsername) - .notNull("用户名不能为空") - .matches(PatternConsts.USERNAME, - username -> new IllegalArgumentException(String.format("用户名\"%s\"不符合规范", username))); + .isTrue(Predicates.of(Objects::nonNull) + .and(StringUtils::isNotEmpty) + .and(StringUtils::isNotBlank) + .and(username -> RegexUtil.matches(username, PatternConsts.USERNAME)), + username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username))); ruleForString(RegisterCommand::getAccount) .notNull("请输入邮箱地址或手机号") .matchesOne(Arrays.asList(PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE), "请输入邮箱地址或手机号"); From 0ea68678562f4e950bf8271e11914878329ca703 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sat, 9 Sep 2023 18:45:02 +0800 Subject: [PATCH 21/26] =?UTF-8?q?plusone-commons=20=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E4=BA=86=20BaseException?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/validator/BaseValidator.java | 2 +- .../validator/InvalidInputException.java | 20 +++++++++---------- .../plusone/validator/PropertyValidator.java | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java index ae00ac2..847a01d 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java @@ -13,7 +13,7 @@ public class BaseValidator { private final List> propertyValidators = new ArrayList<>(); protected void withRule(final Predicate rule, final String errorMessage) { - withRule(rule, () -> new InvalidInputException(errorMessage)); + withRule(rule, () -> InvalidInputException.of(errorMessage)); } protected void withRule(Predicate rule, Supplier exceptionBuilder) { diff --git a/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java b/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java index b8066cf..90310ce 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java @@ -11,29 +11,29 @@ public class InvalidInputException extends BaseException { private static final long serialVersionUID = 7956661913360059670L; - public static final int ERROR_CODE = 4040200; + public static final String ERROR_CODE = "4040200"; - private InvalidInputException(int code, String msg) { + private InvalidInputException(String code, String msg) { super(code, msg); } - private InvalidInputException(int code, Throwable cause) { + private InvalidInputException(String code, Throwable cause) { super(code, cause); } - private InvalidInputException(int code, String msg, Throwable cause) { + private InvalidInputException(String code, String msg, Throwable cause) { super(code, msg, cause); } - public InvalidInputException(String msg) { - this(ERROR_CODE, msg); + public static InvalidInputException of(String msg) { + return new InvalidInputException(ERROR_CODE, msg); } - public InvalidInputException(Throwable cause) { - this(ERROR_CODE, cause); + public static InvalidInputException of(Throwable cause) { + return new InvalidInputException(ERROR_CODE, cause); } - public InvalidInputException(String msg, Throwable cause) { - this(ERROR_CODE, msg, cause); + public static InvalidInputException of(String msg, Throwable cause) { + return new InvalidInputException(ERROR_CODE, msg, cause); } } diff --git a/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java index a0298d2..2dafff3 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java @@ -69,7 +69,7 @@ abstract class PropertyValidator { // ===== equals ===== public THIS equalsThat(Object that) { - return equalsThat(that, value -> new InvalidInputException(String.format("(%s) 必须与 (%s) 相等", value, that))); + return equalsThat(that, value -> InvalidInputException.of(String.format("(%s) 必须与 (%s) 相等", value, that))); } public THIS equalsThat(Object that, String errMsg) { @@ -143,7 +143,7 @@ abstract class PropertyValidator { } static Function convertExceptionCreator(String errMsg) { - return value -> new InvalidInputException(errMsg); + return value -> InvalidInputException.of(errMsg); } static Function convertExceptionCreator( From 791b3bb14c3bbbb81486991748e7bd074f18c1d5 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 10 Sep 2023 16:42:36 +0800 Subject: [PATCH 22/26] =?UTF-8?q?=E5=85=81=E8=AE=B8=E7=BB=A7=E6=89=BF=20In?= =?UTF-8?q?validInputException?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xyz/zhouxy/plusone/validator/InvalidInputException.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java b/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java index 90310ce..559e235 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java @@ -13,15 +13,15 @@ public class InvalidInputException extends BaseException { public static final String ERROR_CODE = "4040200"; - private InvalidInputException(String code, String msg) { + protected InvalidInputException(String code, String msg) { super(code, msg); } - private InvalidInputException(String code, Throwable cause) { + protected InvalidInputException(String code, Throwable cause) { super(code, cause); } - private InvalidInputException(String code, String msg, Throwable cause) { + protected InvalidInputException(String code, String msg, Throwable cause) { super(code, msg, cause); } From 629a28ff62a685f27548047f6378cabeaba5d332 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Sun, 10 Sep 2023 16:42:45 +0800 Subject: [PATCH 23/26] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/xyz/zhouxy/plusone/validator/Validator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/Validator.java b/src/main/java/xyz/zhouxy/plusone/validator/Validator.java index bbe12ad..f5a730a 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/Validator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/Validator.java @@ -47,7 +47,7 @@ public final class Validator extends BaseValidator { withRule(rule, exceptionCreator); return this; } - + public final Validator addRule(Predicate rule, Function exceptionCreator) { withRule(rule, exceptionCreator); return this; From 04ac9b1c8d7a6d82a8da43f4f1591ea3fe21fb92 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 18 Oct 2023 11:06:14 +0800 Subject: [PATCH 24/26] =?UTF-8?q?plusone-commons=20=E4=B8=AD=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20BaseRuntimeException=20=E4=BD=9C=E4=B8=BA=E5=9F=BA?= =?UTF-8?q?=E7=A1=80=E8=BF=90=E8=A1=8C=E6=97=B6=E5=BC=82=E5=B8=B8=EF=BC=8C?= =?UTF-8?q?=E5=8E=9F=20BaseException=20=E4=BD=9C=E4=B8=BA=E5=9F=BA?= =?UTF-8?q?=E7=A1=80=E6=A3=80=E6=9F=A5=E5=9E=8B=E5=BC=82=E5=B8=B8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xyz/zhouxy/plusone/validator/InvalidInputException.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java b/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java index 559e235..cb4726f 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java @@ -1,13 +1,13 @@ package xyz.zhouxy.plusone.validator; -import xyz.zhouxy.plusone.commons.exception.BaseException; +import xyz.zhouxy.plusone.commons.exception.BaseRuntimeException; /** * 4040200 - 无效的用户输入 * * @author ZhouXY */ -public class InvalidInputException extends BaseException { +public class InvalidInputException extends BaseRuntimeException { private static final long serialVersionUID = 7956661913360059670L; From a61869b6b2754cba64b98176b0dd4568bca047d5 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 18 Oct 2023 12:12:07 +0800 Subject: [PATCH 25/26] =?UTF-8?q?=E4=BF=AE=E6=94=B9=20InvalidInputExceptio?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xyz/zhouxy/plusone/validator/InvalidInputException.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java b/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java index cb4726f..1bc1c30 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java @@ -3,7 +3,7 @@ package xyz.zhouxy.plusone.validator; import xyz.zhouxy.plusone.commons.exception.BaseRuntimeException; /** - * 4040200 - 无效的用户输入 + * 4040000 - 用户请求参数错误 * * @author ZhouXY */ @@ -11,7 +11,7 @@ public class InvalidInputException extends BaseRuntimeException { private static final long serialVersionUID = 7956661913360059670L; - public static final String ERROR_CODE = "4040200"; + public static final String ERROR_CODE = "4040000"; protected InvalidInputException(String code, String msg) { super(code, msg); From 92e142dcf32857782058f1ef3badac10e41ceab9 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Mon, 5 Aug 2024 10:42:28 +0800 Subject: [PATCH 26/26] =?UTF-8?q?=E9=87=8D=E6=9E=84=E3=80=82=E5=B9=B6?= =?UTF-8?q?=E6=95=B4=E5=90=88=20Map=20=E6=A0=A1=E9=AA=8C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .editorconfig | 12 + pom.xml | 3 +- .../validator/BasePropertyValidator.java | 171 +++++++++++++ .../plusone/validator/BaseValidator.java | 50 ++-- .../plusone/validator/BoolValidator.java | 8 +- .../validator/CollectionValidator.java | 9 +- .../plusone/validator/DoubleValidator.java | 2 +- .../plusone/validator/IntValidator.java | 2 +- .../validator/InvalidInputException.java | 39 --- .../plusone/validator/ObjectValidator.java | 2 +- .../plusone/validator/PropertyValidator.java | 155 ------------ .../plusone/validator/StringValidator.java | 89 +++++-- .../plusone/validator/map/EntryValidator.java | 22 ++ .../plusone/validator/map/MapValidator.java | 83 +++++++ .../validator/map/test/MapValidatorTests.java | 86 +++++++ .../validator/test/BaseValidatorTest.java | 224 +++++++++--------- .../validator/test/ValidatorTests.java | 147 ++++++++++++ .../validator2/test/ValidatorTests.java | 147 ------------ 18 files changed, 744 insertions(+), 507 deletions(-) create mode 100644 .editorconfig create mode 100644 src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java delete mode 100644 src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java delete mode 100644 src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java create mode 100644 src/main/java/xyz/zhouxy/plusone/validator/map/EntryValidator.java create mode 100644 src/main/java/xyz/zhouxy/plusone/validator/map/MapValidator.java create mode 100644 src/test/java/xyz/zhouxy/plusone/validator/map/test/MapValidatorTests.java create mode 100644 src/test/java/xyz/zhouxy/plusone/validator/test/ValidatorTests.java delete mode 100644 src/test/java/xyz/zhouxy/plusone/validator2/test/ValidatorTests.java diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..2087dd7 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = false +insert_final_newline = true diff --git a/pom.xml b/pom.xml index 12d02a5..d7d1aad 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ xyz.zhouxy.plusone plusone-validator - 0.1.3-SNAPSHOT + 0.1.4-SNAPSHOT plusone-validator http://zhouxy.xyz @@ -28,6 +28,7 @@ org.apache.commons commons-lang3 ${commons-lang3.version} + test org.junit.jupiter diff --git a/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java new file mode 100644 index 0000000..4913aa5 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java @@ -0,0 +1,171 @@ +package xyz.zhouxy.plusone.validator; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.Supplier; + +public abstract class BasePropertyValidator< // + TObj, // + TProperty, // + TPropertyValidator extends BasePropertyValidator> { + + private final Function getter; + + private final List> consumers = new LinkedList<>(); + + protected BasePropertyValidator(Function getter) { + this.getter = getter; + } + + public final TPropertyValidator withRule(Predicate rule) { + return withRule(rule, v -> new IllegalArgumentException()); + } + + public final TPropertyValidator withRule( + Predicate rule, String errMsg) { + return withRule(rule, convertExceptionCreator(errMsg)); + } + + public final TPropertyValidator withRule( + Predicate rule, Supplier e) { + return withRule(rule, convertExceptionCreator(e)); + } + + public final TPropertyValidator withRule( + Predicate rule, Function e) { + this.consumers.add(v -> { + if (!rule.test(v)) { + throw e.apply(v); + } + }); + return thisObject(); + } + + public final void validate(T obj) { + for (Consumer consumer : consumers) { + consumer.accept(getter.apply(obj)); + } + } + + protected abstract TPropertyValidator thisObject(); + + // ==================== + // ====== Object ====== + // ==================== + + // ====== notNull ===== + + public TPropertyValidator notNull() { + return notNull("Value could not be null."); + } + + public TPropertyValidator notNull(String errMsg) { + return notNull(convertExceptionCreator(errMsg)); + } + + public TPropertyValidator notNull(Supplier exceptionCreator) { + return notNull(convertExceptionCreator(exceptionCreator)); + } + + public TPropertyValidator notNull(Function exceptionCreator) { + withRule(Objects::nonNull, exceptionCreator); + return thisObject(); + } + + // ====== isNull ===== + + public TPropertyValidator isNull(String errMsg) { + return isNull(convertExceptionCreator(errMsg)); + } + + public TPropertyValidator isNull(Supplier exceptionCreator) { + return isNull(convertExceptionCreator(exceptionCreator)); + } + + public TPropertyValidator isNull(Function exceptionCreator) { + withRule(Objects::isNull, exceptionCreator); + return thisObject(); + } + + // ===== equals ===== + + public TPropertyValidator equalsThat(Object that) { + return equalsThat(that, value -> new IllegalArgumentException(String.format("(%s) 必须与 (%s) 相等", value, that))); + } + + public TPropertyValidator equalsThat(Object that, String errMsg) { + return equalsThat(that, convertExceptionCreator(errMsg)); + } + + public TPropertyValidator equalsThat( + Object that, Supplier exceptionCreator) { + return equalsThat(that, convertExceptionCreator(exceptionCreator)); + } + + public TPropertyValidator equalsThat( + Object that, Function exceptionCreator) { + withRule(value -> Objects.equals(value, that), exceptionCreator); + return thisObject(); + } + + // ===== isTrue ===== + + public TPropertyValidator isTrue(Predicate condition) { + return isTrue(condition, "无效的用户输入"); + } + + public TPropertyValidator isTrue(Predicate condition, String errMsg) { + return isTrue(condition, convertExceptionCreator(errMsg)); + } + + public TPropertyValidator isTrue( + Predicate condition, + Supplier exceptionCreator) { + return isTrue(condition, convertExceptionCreator(exceptionCreator)); + } + + public TPropertyValidator isTrue( + Predicate condition, + Function exceptionCreator) { + withRule(condition, exceptionCreator); + return thisObject(); + } + + // ===== isTrue ===== + + public TPropertyValidator isTrue(Collection> conditions) { + return isTrue(conditions, "无效的用户输入"); + } + + public TPropertyValidator isTrue(Collection> conditions, String errMsg) { + return isTrue(conditions, convertExceptionCreator(errMsg)); + } + + public TPropertyValidator isTrue( + Collection> conditions, + Supplier exceptionCreator) { + return isTrue(conditions, convertExceptionCreator(exceptionCreator)); + } + + public TPropertyValidator isTrue( + Collection> conditions, + Function exceptionCreator) { + for (Predicate condition : conditions) { + withRule(condition, exceptionCreator); + } + return thisObject(); + } + + static Function convertExceptionCreator(String errMsg) { + return value -> new IllegalArgumentException(errMsg); + } + + static Function convertExceptionCreator(Supplier exceptionSupplier) { + return value -> exceptionSupplier.get(); + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java index 847a01d..655b9f4 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java @@ -10,18 +10,17 @@ import java.util.function.Supplier; public class BaseValidator { private final List> rules = new ArrayList<>(); - private final List> propertyValidators = new ArrayList<>(); protected void withRule(final Predicate rule, final String errorMessage) { - withRule(rule, () -> InvalidInputException.of(errorMessage)); + withRule(rule, () -> new IllegalArgumentException(errorMessage)); } protected void withRule(Predicate rule, Supplier exceptionBuilder) { withRule(rule, value -> exceptionBuilder.get()); } - protected void withRule(Predicate condition, - Function exceptionBuilder) { + protected void withRule( + Predicate condition, Function exceptionBuilder) { withRule(value -> { if (!condition.test(value)) { throw exceptionBuilder.apply(value); @@ -34,47 +33,42 @@ public class BaseValidator { } protected final ObjectValidator ruleFor(Function getter) { - ObjectValidator validValueHolder = new ObjectValidator<>(getter); - propertyValidators.add(validValueHolder); - return validValueHolder; + ObjectValidator validator = new ObjectValidator<>(getter); + this.rules.add(validator::validate); + return validator; } protected final IntValidator ruleForInt(Function getter) { - IntValidator validValueHolder = new IntValidator<>(getter); - propertyValidators.add(validValueHolder); - return validValueHolder; + IntValidator validator = new IntValidator<>(getter); + this.rules.add(validator::validate); + return validator; } protected final DoubleValidator ruleForDouble(Function getter) { - DoubleValidator validValueHolder = new DoubleValidator<>(getter); - propertyValidators.add(validValueHolder); - return validValueHolder; + DoubleValidator validator = new DoubleValidator<>(getter); + this.rules.add(validator::validate); + return validator; } protected final BoolValidator ruleForBool(Function getter) { - BoolValidator validValueHolder = new BoolValidator<>(getter); - propertyValidators.add(validValueHolder); - return validValueHolder; + BoolValidator validator = new BoolValidator<>(getter); + this.rules.add(validator::validate); + return validator; } protected final StringValidator ruleForString(Function getter) { - StringValidator validValueHolder = new StringValidator<>(getter); - propertyValidators.add(validValueHolder); - return validValueHolder; + StringValidator validator = new StringValidator<>(getter); + this.rules.add(validator::validate); + return validator; } protected final CollectionValidator ruleForCollection(Function> getter) { - CollectionValidator validValueHolder = new CollectionValidator<>(getter); - propertyValidators.add(validValueHolder); - return validValueHolder; + CollectionValidator validator = new CollectionValidator<>(getter); + this.rules.add(validator::validate); + return validator; } public void validate(T obj) { - for (Consumer rule : this.rules) { - rule.accept(obj); - } - for (PropertyValidator valueValidator : this.propertyValidators) { - valueValidator.validate(obj); - } + this.rules.forEach(rule -> rule.accept(obj)); } } diff --git a/src/main/java/xyz/zhouxy/plusone/validator/BoolValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/BoolValidator.java index fa408f6..726c164 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/BoolValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/BoolValidator.java @@ -3,9 +3,7 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; import java.util.function.Supplier; -import org.apache.commons.lang3.BooleanUtils; - -public class BoolValidator extends PropertyValidator> { +public class BoolValidator extends BasePropertyValidator> { BoolValidator(Function getter) { super(getter); @@ -27,7 +25,7 @@ public class BoolValidator extends PropertyValidator BoolValidator isTrue( Function exceptionCreator) { - withRule(BooleanUtils::isTrue, exceptionCreator); + withRule(Boolean.TRUE::equals, exceptionCreator); return this; } @@ -47,7 +45,7 @@ public class BoolValidator extends PropertyValidator BoolValidator isFalse( Function exceptionCreator) { - withRule(BooleanUtils::isFalse, exceptionCreator); + withRule(Boolean.FALSE::equals, exceptionCreator); return this; } diff --git a/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java index cb434a1..afbd02e 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java @@ -4,7 +4,10 @@ import java.util.Collection; import java.util.function.Function; import java.util.function.Supplier; -public class CollectionValidator extends PropertyValidator, CollectionValidator> { +import xyz.zhouxy.plusone.commons.collection.CollectionTools; + +public class CollectionValidator + extends BasePropertyValidator, CollectionValidator> { CollectionValidator(Function> getter) { super(getter); @@ -22,7 +25,7 @@ public class CollectionValidator extends PropertyValidator CollectionValidator notEmpty( Function, E> exceptionCreator) { - withRule(value -> value != null && !value.isEmpty(), exceptionCreator); + withRule(CollectionTools::isNotEmpty, exceptionCreator); return this; } @@ -38,7 +41,7 @@ public class CollectionValidator extends PropertyValidator CollectionValidator isEmpty( Function, E> exceptionCreator) { - withRule(value -> value == null || value.isEmpty(), exceptionCreator); + withRule(CollectionTools::isEmpty, exceptionCreator); return this; } diff --git a/src/main/java/xyz/zhouxy/plusone/validator/DoubleValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/DoubleValidator.java index 4174e79..f2bbf4e 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/DoubleValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/DoubleValidator.java @@ -3,7 +3,7 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; import java.util.function.Supplier; -public class DoubleValidator extends PropertyValidator> { +public class DoubleValidator extends BasePropertyValidator> { DoubleValidator(Function getter) { super(getter); diff --git a/src/main/java/xyz/zhouxy/plusone/validator/IntValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/IntValidator.java index 1cdf093..1250f95 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/IntValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/IntValidator.java @@ -3,7 +3,7 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; import java.util.function.Supplier; -public class IntValidator extends PropertyValidator> { +public class IntValidator extends BasePropertyValidator> { IntValidator(Function getter) { super(getter); diff --git a/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java b/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java deleted file mode 100644 index 1bc1c30..0000000 --- a/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java +++ /dev/null @@ -1,39 +0,0 @@ -package xyz.zhouxy.plusone.validator; - -import xyz.zhouxy.plusone.commons.exception.BaseRuntimeException; - -/** - * 4040000 - 用户请求参数错误 - * - * @author ZhouXY - */ -public class InvalidInputException extends BaseRuntimeException { - - private static final long serialVersionUID = 7956661913360059670L; - - public static final String ERROR_CODE = "4040000"; - - protected InvalidInputException(String code, String msg) { - super(code, msg); - } - - protected InvalidInputException(String code, Throwable cause) { - super(code, cause); - } - - protected InvalidInputException(String code, String msg, Throwable cause) { - super(code, msg, cause); - } - - public static InvalidInputException of(String msg) { - return new InvalidInputException(ERROR_CODE, msg); - } - - public static InvalidInputException of(Throwable cause) { - return new InvalidInputException(ERROR_CODE, cause); - } - - public static InvalidInputException of(String msg, Throwable cause) { - return new InvalidInputException(ERROR_CODE, msg, cause); - } -} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/ObjectValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/ObjectValidator.java index 6f2de5f..3941bc0 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/ObjectValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/ObjectValidator.java @@ -2,7 +2,7 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; -public class ObjectValidator extends PropertyValidator> { +public class ObjectValidator extends BasePropertyValidator> { ObjectValidator(Function getter) { super(getter); diff --git a/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java deleted file mode 100644 index 2dafff3..0000000 --- a/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java +++ /dev/null @@ -1,155 +0,0 @@ -package xyz.zhouxy.plusone.validator; - -import java.util.Collection; -import java.util.Objects; -import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.Predicate; -import java.util.function.Supplier; - -abstract class PropertyValidator { - Function getter; - Validator validator = new Validator<>(); - - PropertyValidator(Function getter) { - this.getter = getter; - } - - void withRule(Predicate condition, - Function exceptionCreator) { - withRule(value -> { - if (!condition.test(value)) { - throw exceptionCreator.apply(value); - } - }); - } - - private void withRule(Consumer rule) { - this.validator.addRule(rule); - } - - // ==================== - // ====== Object ====== - // ==================== - - // ====== notNull ===== - - public THIS notNull() { - return notNull("Value could not be null."); - } - - public THIS notNull(String errMsg) { - return notNull(convertExceptionCreator(errMsg)); - } - - public THIS notNull(Supplier exceptionCreator) { - return notNull(convertExceptionCreator(exceptionCreator)); - } - - public THIS notNull(Function exceptionCreator) { - withRule(Objects::nonNull, exceptionCreator); - return thisObject(); - } - - // ====== isNull ===== - - public THIS isNull(String errMsg) { - return isNull(convertExceptionCreator(errMsg)); - } - - public THIS isNull(Supplier exceptionCreator) { - return isNull(convertExceptionCreator(exceptionCreator)); - } - - public THIS isNull(Function exceptionCreator) { - withRule(Objects::isNull, exceptionCreator); - return thisObject(); - } - - // ===== equals ===== - - public THIS equalsThat(Object that) { - return equalsThat(that, value -> InvalidInputException.of(String.format("(%s) 必须与 (%s) 相等", value, that))); - } - - public THIS equalsThat(Object that, String errMsg) { - return equalsThat(that, convertExceptionCreator(errMsg)); - } - - public THIS equalsThat( - Object that, Supplier exceptionCreator) { - return equalsThat(that, convertExceptionCreator(exceptionCreator)); - } - - public THIS equalsThat( - Object that, Function exceptionCreator) { - withRule(value -> Objects.equals(value, that), exceptionCreator); - return thisObject(); - } - - // ===== isTrue ===== - - public THIS isTrue(Predicate condition) { - return isTrue(condition, "无效的用户输入"); - } - - public THIS isTrue(Predicate condition, String errMsg) { - return isTrue(condition, convertExceptionCreator(errMsg)); - } - - public THIS isTrue( - Predicate condition, - Supplier exceptionCreator) { - return isTrue(condition, convertExceptionCreator(exceptionCreator)); - } - - public THIS isTrue( - Predicate condition, - Function exceptionCreator) { - withRule(condition, exceptionCreator); - return thisObject(); - } - - // ===== isTrue ===== - - public THIS isTrue(Collection> conditions) { - return isTrue(conditions, "无效的用户输入"); - } - - public THIS isTrue(Collection> conditions, String errMsg) { - return isTrue(conditions, convertExceptionCreator(errMsg)); - } - - public THIS isTrue( - Collection> conditions, - Supplier exceptionCreator) { - return isTrue(conditions, convertExceptionCreator(exceptionCreator)); - } - - public THIS isTrue( - Collection> conditions, - Function exceptionCreator) { - for (Predicate condition : conditions) { - withRule(condition, exceptionCreator); - } - return thisObject(); - } - - // ======================================================================== - - void validate(DTO obj) { - PROPERTY value = this.getter.apply(obj); - this.validator.validate(value); - } - - static Function convertExceptionCreator(String errMsg) { - return value -> InvalidInputException.of(errMsg); - } - - static Function convertExceptionCreator( - Supplier exceptionSupplier) { - return value -> exceptionSupplier.get(); - } - - protected abstract THIS thisObject(); -} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java index 77824e3..c102318 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java @@ -6,12 +6,12 @@ import java.util.function.Function; import java.util.function.Supplier; import java.util.regex.Pattern; -import org.apache.commons.lang3.StringUtils; +import com.google.common.base.Preconditions; import xyz.zhouxy.plusone.commons.constant.PatternConsts; -import xyz.zhouxy.plusone.commons.util.RegexUtil; +import xyz.zhouxy.plusone.commons.util.RegexTools; -public class StringValidator extends PropertyValidator> { +public class StringValidator extends BasePropertyValidator> { StringValidator(Function getter) { super(getter); @@ -36,7 +36,7 @@ public class StringValidator extends PropertyValidator StringValidator matches( Pattern regex, Function exceptionCreator) { - withRule(input -> RegexUtil.matches(input, regex), exceptionCreator); + withRule(input -> RegexTools.matches(input, regex), exceptionCreator); return this; } @@ -55,7 +55,7 @@ public class StringValidator extends PropertyValidator StringValidator matchesOne( Pattern[] regexs, Function exceptionCreator) { - withRule(input -> RegexUtil.matchesOne(input, regexs), exceptionCreator); + withRule(input -> RegexTools.matchesOne(input, regexs), exceptionCreator); return this; } @@ -72,7 +72,7 @@ public class StringValidator extends PropertyValidator StringValidator matchesOne( List regexs, Function exceptionCreator) { - withRule(input -> RegexUtil.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); + withRule(input -> RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); return this; } @@ -91,7 +91,7 @@ public class StringValidator extends PropertyValidator StringValidator matchesAll( Pattern[] regexs, Function exceptionCreator) { - withRule(input -> RegexUtil.matchesAll(input, regexs), exceptionCreator); + withRule(input -> RegexTools.matchesAll(input, regexs), exceptionCreator); return this; } @@ -108,12 +108,24 @@ public class StringValidator extends PropertyValidator StringValidator matchesAll( Collection regexs, Function exceptionCreator) { - withRule(input -> RegexUtil.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); + withRule(input -> RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); return this; } // ===== notBlank ===== + static boolean isNotBlank(final String cs) { + if (cs == null || cs.isEmpty()) { + return false; + } + for (int i = 0; i < cs.length(); i++) { + if (!Character.isWhitespace(cs.charAt(i))) { + return false; + } + } + return true; + } + public StringValidator notBlank() { return notBlank("This String argument must have text; it must not be null, empty, or blank"); } @@ -128,7 +140,7 @@ public class StringValidator extends PropertyValidator StringValidator notBlank( Function exceptionCreator) { - withRule(StringUtils::isNotBlank, exceptionCreator); + withRule(StringValidator::isNotBlank, exceptionCreator); return this; } @@ -162,23 +174,66 @@ public class StringValidator extends PropertyValidator StringValidator notEmpty( Function exceptionCreator) { - withRule(StringUtils::isNotEmpty, exceptionCreator); + withRule(s -> s != null && !s.isEmpty(), exceptionCreator); return this; } - // ====== isEmpty ===== + // ====== isNullOrEmpty ===== - public StringValidator isEmpty(String errMsg) { - return isEmpty(convertExceptionCreator(errMsg)); + public StringValidator isNullOrEmpty(String errMsg) { + return isNullOrEmpty(convertExceptionCreator(errMsg)); } - public StringValidator isEmpty(Supplier exceptionCreator) { - return isEmpty(convertExceptionCreator(exceptionCreator)); + public StringValidator isNullOrEmpty(Supplier exceptionCreator) { + return isNullOrEmpty(convertExceptionCreator(exceptionCreator)); } - public StringValidator isEmpty( + public StringValidator isNullOrEmpty( Function exceptionCreator) { - withRule(StringUtils::isEmpty, exceptionCreator); + withRule(s -> s == null || s.isEmpty(), exceptionCreator); + return this; + } + + // ====== length ===== + + public StringValidator length(int length, String errMsg) { + return length(length, convertExceptionCreator(errMsg)); + } + + public StringValidator length(int length, + Supplier exceptionCreator) { + return length(length, convertExceptionCreator(exceptionCreator)); + } + + public StringValidator length(int length, + Function exceptionCreator) { + Preconditions.checkArgument(length >= 0, "The minimum value must be less than the maximum value."); + withRule(s -> s != null && s.length() == length, exceptionCreator); + return this; + } + + static boolean length(String str, int min, int max) { + if (str == null) { + return false; + } + final int len = str.length(); + return len >= min && len < max; + } + + public StringValidator length(int min, int max, String errMsg) { + return length(min, max, convertExceptionCreator(errMsg)); + } + + public StringValidator length(int min, int max, + Supplier exceptionCreator) { + return length(min, max, convertExceptionCreator(exceptionCreator)); + } + + public StringValidator length(int min, int max, + Function exceptionCreator) { + Preconditions.checkArgument(min >= 0, "The minimum value must be greater than equal to 0."); + Preconditions.checkArgument(min < max, "The minimum value must be less than the maximum value."); + withRule(s -> length(s, min, max), exceptionCreator); return this; } diff --git a/src/main/java/xyz/zhouxy/plusone/validator/map/EntryValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/map/EntryValidator.java new file mode 100644 index 0000000..fd42bb0 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/validator/map/EntryValidator.java @@ -0,0 +1,22 @@ +package xyz.zhouxy.plusone.validator.map; + +import java.util.Map; + +import xyz.zhouxy.plusone.validator.BasePropertyValidator; + +public class EntryValidator + extends BasePropertyValidator, V, EntryValidator> { + + public EntryValidator(K key) { + super(m -> { + @SuppressWarnings("unchecked") + V v = (V) m.get(key); + return v; + }); + } + + @Override + protected EntryValidator thisObject() { + return this; + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/map/MapValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/map/MapValidator.java new file mode 100644 index 0000000..e7432dc --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/validator/map/MapValidator.java @@ -0,0 +1,83 @@ +package xyz.zhouxy.plusone.validator.map; + +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.function.Supplier; +import java.util.stream.Collectors; + +public abstract class MapValidator { + + private final List>> consumers = new LinkedList<>(); + + private final Set keys; + + protected MapValidator(K[] keys) { + this(Arrays.asList(keys)); + } + + protected MapValidator(Collection keys) { + this.keys = keys.stream().collect(Collectors.toSet()); + } + + public final Map validateAndCopy(Map obj) { + return validateAndCopyInternal(obj, this.keys); + } + + public final Map validateAndCopy(Map obj, Collection keys) { + return validateAndCopyInternal(obj, keys); + } + + @SafeVarargs + public final Map validateAndCopy(Map obj, K... keys) { + return validateAndCopyInternal(obj, Arrays.asList(keys)); + } + + private final Map validateAndCopyInternal(Map obj, Collection keys) { + validate(obj); + return obj.entrySet().stream() + .filter(kv -> keys.contains(kv.getKey())) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + } + + public final void validate(Map obj) { + this.consumers.forEach(consumer -> consumer.accept(obj)); + } + + @SuppressWarnings("unused") + protected final EntryValidator checkValue(K key, Class clazz) { + return checkValue(key); + } + + protected final EntryValidator checkValue(K key) { + EntryValidator validator = new EntryValidator<>(key); + this.consumers.add(validator::validate); + return validator; + } + + protected final void withRule(Predicate> rule, String errMsg) { + withRule(rule, map -> new IllegalArgumentException(errMsg)); + } + + protected final void withRule(Predicate> rule, String errMsgFormat, Object... args) { + withRule(rule, map -> new IllegalArgumentException(String.format(errMsgFormat, args))); + } + + protected final void withRule(Predicate> rule, Supplier e) { + withRule(rule, map -> e.get()); + } + + protected final void withRule(Predicate> rule, Function, E> e) { + this.consumers.add(map -> { + if (!rule.test(map)) { + throw e.apply(map); + } + }); + } +} diff --git a/src/test/java/xyz/zhouxy/plusone/validator/map/test/MapValidatorTests.java b/src/test/java/xyz/zhouxy/plusone/validator/map/test/MapValidatorTests.java new file mode 100644 index 0000000..f8da6dc --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/validator/map/test/MapValidatorTests.java @@ -0,0 +1,86 @@ +package xyz.zhouxy.plusone.validator.map.test; + +import static org.junit.jupiter.api.Assertions.*; + +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +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.collection.CollectionTools; +import xyz.zhouxy.plusone.commons.constant.PatternConsts; +import xyz.zhouxy.plusone.commons.function.PredicateTools; +import xyz.zhouxy.plusone.commons.util.RegexTools; +import xyz.zhouxy.plusone.validator.map.MapValidator; + +public // +class MapValidatorTests { + + private static final String USERNAME = "username"; + private static final String ACCOUNT = "account"; + private static final String PASSWORD = "password"; + private static final String PASSWORD2 = "password2"; + private static final String AGE = "age"; + private static final String BOOLEAN = "boolean"; + private static final String ROLE_LIST = "roleList"; + + private static final MapValidator validator = new MapValidator( + new String[] { USERNAME, ACCOUNT, PASSWORD, AGE, BOOLEAN, ROLE_LIST }) { + { + + checkValue(USERNAME, String.class).withRule( + PredicateTools.from(StringUtils::isNotBlank) + .and(username -> RegexTools.matches(username, PatternConsts.USERNAME)), + username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username))); + + checkValue(ACCOUNT, String.class).withRule( + PredicateTools.from(StringUtils::isNotBlank) + .and(account -> RegexTools.matchesOne(account, + new Pattern[] { PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE })), + "请输入正确的邮箱地址或手机号"); + + checkValue(PASSWORD, String.class) + .withRule(StringUtils::isNotEmpty, "密码不能为空") + .withRule(pwd -> RegexTools.matches(pwd, PatternConsts.PASSWORD), "密码不符合规范"); + + // 校验到多个属性,只能针对 map 本身进行校验 + withRule(m -> Objects.equals(m.get(PASSWORD), m.get(PASSWORD2)), + "两次输入的密码不一样!"); + + // 通过泛型方式调用方法,指定数据类型 + this.checkValue(AGE) + .withRule(Objects::nonNull) + .withRule(age -> (18 <= age && 60 >= age)); + + checkValue(BOOLEAN, Boolean.class) + .withRule(Objects::nonNull, "Boolean property could not be null.") + .withRule(b -> b, "Boolean property must be true."); + + this.>checkValue(ROLE_LIST) + .withRule(CollectionTools::isNotEmpty, "角色列表不能为空!") + .withRule(l -> l.stream().allMatch(StringUtils::isNotBlank), + () -> new IllegalArgumentException("角色标识不能为空!")); + + } + }; + + @Test + void testValidateAndCopy() { + Map params = new HashMap<>(); + params.put("username", "ZhouXY"); + params.put("account", "zhouxy@code108.cn"); + params.put("password", "99Code108"); + params.put("password2", "99Code108"); + params.put("age", 18); + params.put("boolean", true); + params.put("roleList", Arrays.asList("admin", "")); + + Map validedParams = validator.validateAndCopy(params); + System.out.println(validedParams); + } +} diff --git a/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java b/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java index d6e2927..fc4f5a0 100644 --- a/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java +++ b/src/test/java/xyz/zhouxy/plusone/validator/test/BaseValidatorTest.java @@ -8,130 +8,136 @@ import org.apache.commons.lang3.StringUtils; import org.junit.jupiter.api.Test; import xyz.zhouxy.plusone.commons.constant.PatternConsts; -import xyz.zhouxy.plusone.commons.function.Predicates; -import xyz.zhouxy.plusone.commons.util.RegexUtil; +import xyz.zhouxy.plusone.commons.function.PredicateTools; +import xyz.zhouxy.plusone.commons.util.RegexTools; import xyz.zhouxy.plusone.validator.BaseValidator; import xyz.zhouxy.plusone.validator.ValidateUtil; class BaseValidatorTest { @Test void testValidate() { - RegisterCommand registerCommand = new RegisterCommand("zhouxy108", "luquanlion@outlook.com", "22336", "A1b2C3d4", + RegisterCommand registerCommand = new RegisterCommand("zhouxy108", "luquanlion@outlook.com", "22336", + "A1b2C3d4", "A1b2C3d4", Arrays.asList(new String[] { "admin", "editor" })); RegisterCommandValidator.INSTANCE.validate(registerCommand); ValidateUtil.validate(registerCommand, RegisterCommandValidator.INSTANCE); System.out.println(registerCommand); } -} -class RegisterCommandValidator extends BaseValidator { + static class RegisterCommandValidator extends BaseValidator { - static final RegisterCommandValidator INSTANCE = new RegisterCommandValidator(); + static final RegisterCommandValidator INSTANCE = new RegisterCommandValidator(); - private RegisterCommandValidator() { - ruleForString(RegisterCommand::getUsername) - .isTrue(Predicates.of(Objects::nonNull) - .and(StringUtils::isNotEmpty) - .and(StringUtils::isNotBlank) - .and(username -> RegexUtil.matches(username, PatternConsts.USERNAME)), - username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username))); - ruleForString(RegisterCommand::getAccount) - .notNull("请输入邮箱地址或手机号") - .matchesOne(Arrays.asList(PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE), "请输入邮箱地址或手机号"); - ruleForString(RegisterCommand::getCode) - .notNull("验证码不能为空") - .matches(PatternConsts.CAPTCHA, "验证码不符合规范"); - ruleForString(RegisterCommand::getPassword) - .notEmpty("密码不能为空") - .matches(PatternConsts.PASSWORD, "密码不符合规范"); - ruleForCollection(RegisterCommand::getRoles) - .notEmpty(() -> new RuntimeException("角色列表不能为空")); + private RegisterCommandValidator() { + ruleForString(RegisterCommand::getUsername) + .isTrue(PredicateTools.from(Objects::nonNull) + .and(StringUtils::isNotEmpty) + .and(StringUtils::isNotBlank) + .and(username -> RegexTools.matches(username, PatternConsts.USERNAME)), + username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username))); + ruleForString(RegisterCommand::getAccount) + .notNull("请输入邮箱地址或手机号") + .matchesOne(Arrays.asList(PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE), "请输入邮箱地址或手机号"); + ruleForString(RegisterCommand::getCode) + .notNull("验证码不能为空") + .matches(PatternConsts.CAPTCHA, "验证码不符合规范"); + ruleForString(RegisterCommand::getPassword) + .notEmpty("密码不能为空") + .matches(PatternConsts.PASSWORD, "密码不符合规范"); + ruleForCollection(RegisterCommand::getRoles) + .notEmpty(() -> new RuntimeException("角色列表不能为空")); - withRule(registerCommand -> Objects.equals(registerCommand.getPassword(), registerCommand.getPassword2()), - "两次输入的密码不一致"); - } -} - -/** - * RegisterCommand - */ -class RegisterCommand { - - private String username; - private String account; - private String code; - private String password; - private String password2; - private List roles; - - public RegisterCommand() { - } - - public RegisterCommand(String username, String account, String code, String password, String password2, - List roles) { - this.username = username; - this.account = account; - this.code = code; - this.password = password; - this.password2 = password2; - this.roles = roles; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getAccount() { - return account; - } - - public void setAccount(String account) { - this.account = account; - } - - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = code; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getPassword2() { - return password2; - } - - public void setPassword2(String password2) { - this.password2 = password2; - } - - public List getRoles() { - return roles; - } - - public void setRoles(List roles) { - this.roles = roles; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("RegisterCommand [username=").append(username).append(", account=").append(account) - .append(", code=").append(code).append(", password=").append(password).append(", password2=") - .append(password2).append(", roles=").append(roles).append("]"); - return builder.toString(); + withRule(registerCommand -> Objects.equals(registerCommand.getPassword(), registerCommand.getPassword2()), + "两次输入的密码不一致"); + } + } + + /** + * RegisterCommand + */ + static class RegisterCommand { + + private String username; + private String account; + private String code; + private String password; + private String password2; + private List roles; + + public RegisterCommand() { + } + + public RegisterCommand(String username, String account, String code, String password, String password2, + List roles) { + this.username = username; + this.account = account; + this.code = code; + this.password = password; + this.password2 = password2; + this.roles = roles; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getAccount() { + return account; + } + + public void setAccount(String account) { + this.account = account; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getPassword2() { + return password2; + } + + public void setPassword2(String password2) { + this.password2 = password2; + } + + public List getRoles() { + return roles; + } + + public void setRoles(List roles) { + this.roles = roles; + } + + @Override + public String toString() { + return new StringBuilder() + .append("RegisterCommand [") + .append("username=").append(username) + .append(", account=").append(account) + .append(", code=").append(code) + .append(", password=").append(password) + .append(", password2=").append(password2) + .append(", roles=").append(roles) + .append("]") + .toString(); + } } } diff --git a/src/test/java/xyz/zhouxy/plusone/validator/test/ValidatorTests.java b/src/test/java/xyz/zhouxy/plusone/validator/test/ValidatorTests.java new file mode 100644 index 0000000..b5cc7f4 --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/validator/test/ValidatorTests.java @@ -0,0 +1,147 @@ +package xyz.zhouxy.plusone.validator.test; + +import static xyz.zhouxy.plusone.commons.constant.PatternConsts.*; + +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import java.util.regex.Pattern; + +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; + +import com.google.common.base.Preconditions; + +import xyz.zhouxy.plusone.commons.collection.CollectionTools; +import xyz.zhouxy.plusone.commons.function.PredicateTools; +import xyz.zhouxy.plusone.commons.util.RegexTools; +import xyz.zhouxy.plusone.validator.Validator; + +class ValidatorTests { + @Test + void testValidate() { + RegisterCommand registerCommand = new RegisterCommand( + "null", "luquanlion@outlook.com", "22336", + "A1b2C3d4", "A1b2C3d4", + Arrays.asList(new String[] { "admin", "editor" })); + + Validator registerCommandValidator = new Validator() + // 传入 predicate 和 Function + .addRule(command -> { + String username = command.getUsername(); + return Objects.nonNull(username) + && StringUtils.isNotEmpty(username) + && StringUtils.isNotBlank(username) + && RegexTools.matches(username, USERNAME); + }, command -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", command.getUsername()))) + // 传入 predicate 和 error message + .addRule(command -> PredicateTools + .from(Objects::nonNull) + .and(account -> RegexTools.matchesOne(account, new Pattern[] { EMAIL, MOBILE_PHONE })) + .test(command.getAccount()), + "请输入邮箱地址或手机号") + // 传入 rule + .addRule(command -> { + String code = command.getCode(); + Preconditions.checkArgument(Objects.nonNull(code), "验证码不能为空"); + Preconditions.checkArgument(RegexTools.matches(code, CAPTCHA), "验证码不符合规范"); + }) + // 传入 rule + .addRule(command -> { + String password = command.getPassword(); + Preconditions.checkArgument(StringUtils.isNotEmpty(password), "密码不能为空"); + Preconditions.checkArgument(RegexTools.matches(password, PASSWORD), "密码不符合规范"); + }) + // 传入 predicate 和 Supplier + .addRule(command -> CollectionTools.isNotEmpty(command.getRoles()), + () -> new RuntimeException("角色列表不能为空")) + // 传入 predicate 和 error message + .addRule(command -> Objects.equals(command.getPassword(), command.getPassword2()), + "两次输入的密码不一致"); + registerCommandValidator.validate(registerCommand); + System.out.println(registerCommand); + } + + /** + * RegisterCommand + */ + static class RegisterCommand { + + private String username; + private String account; + private String code; + private String password; + private String password2; + private List roles; + + public RegisterCommand() { + } + + public RegisterCommand(String username, String account, String code, String password, String password2, + List roles) { + this.username = username; + this.account = account; + this.code = code; + this.password = password; + this.password2 = password2; + this.roles = roles; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getAccount() { + return account; + } + + public void setAccount(String account) { + this.account = account; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getPassword2() { + return password2; + } + + public void setPassword2(String password2) { + this.password2 = password2; + } + + public List getRoles() { + return roles; + } + + public void setRoles(List roles) { + this.roles = roles; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("RegisterCommand [username=").append(username).append(", account=").append(account) + .append(", code=").append(code).append(", password=").append(password).append(", password2=") + .append(password2).append(", roles=").append(roles).append("]"); + return builder.toString(); + } + } +} diff --git a/src/test/java/xyz/zhouxy/plusone/validator2/test/ValidatorTests.java b/src/test/java/xyz/zhouxy/plusone/validator2/test/ValidatorTests.java deleted file mode 100644 index 372052d..0000000 --- a/src/test/java/xyz/zhouxy/plusone/validator2/test/ValidatorTests.java +++ /dev/null @@ -1,147 +0,0 @@ -package xyz.zhouxy.plusone.validator2.test; - -import static xyz.zhouxy.plusone.commons.constant.PatternConsts.*; - -import java.util.Arrays; -import java.util.List; -import java.util.Objects; -import java.util.regex.Pattern; - -import org.apache.commons.lang3.StringUtils; -import org.junit.jupiter.api.Test; - -import com.google.common.base.Preconditions; - -import xyz.zhouxy.plusone.commons.function.Predicates; -import xyz.zhouxy.plusone.commons.util.MoreCollections; -import xyz.zhouxy.plusone.commons.util.RegexUtil; -import xyz.zhouxy.plusone.validator.Validator; - -class ValidatorTests { - @Test - void testValidate() { - RegisterCommand registerCommand = new RegisterCommand( - "null", "luquanlion@outlook.com", "22336", - "A1b2C3d4", "A1b2C3d4", - Arrays.asList(new String[] { "admin", "editor" })); - - Validator registerCommandValidator = new Validator() - // 传入 predicate 和 Function - .addRule(command -> { - String username = command.getUsername(); - return Objects.nonNull(username) - && StringUtils.isNotEmpty(username) - && StringUtils.isNotBlank(username) - && RegexUtil.matches(username, USERNAME); - }, command -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", command.getUsername()))) - // 传入 predicate 和 error message - .addRule(command -> Predicates - .of(Objects::nonNull) - .and(account -> RegexUtil.matchesOne(account, new Pattern[] { EMAIL, MOBILE_PHONE })) - .test(command.getAccount()), - "请输入邮箱地址或手机号") - // 传入 rule - .addRule(command -> { - String code = command.getCode(); - Preconditions.checkArgument(Objects.nonNull(code), "验证码不能为空"); - Preconditions.checkArgument(RegexUtil.matches(code, CAPTCHA), "验证码不符合规范"); - }) - // 传入 rule - .addRule(command -> { - String password = command.getPassword(); - Preconditions.checkArgument(StringUtils.isNotEmpty(password), "密码不能为空"); - Preconditions.checkArgument(RegexUtil.matches(password, PASSWORD), "密码不符合规范"); - }) - // 传入 predicate 和 Supplier - .addRule(command -> MoreCollections.isNotEmpty(command.getRoles()), - () -> new RuntimeException("角色列表不能为空")) - // 传入 predicate 和 error message - .addRule(command -> Objects.equals(command.getPassword(), command.getPassword2()), - "两次输入的密码不一致"); - registerCommandValidator.validate(registerCommand); - System.out.println(registerCommand); - } -} - -/** - * RegisterCommand - */ -class RegisterCommand { - - private String username; - private String account; - private String code; - private String password; - private String password2; - private List roles; - - public RegisterCommand() { - } - - public RegisterCommand(String username, String account, String code, String password, String password2, - List roles) { - this.username = username; - this.account = account; - this.code = code; - this.password = password; - this.password2 = password2; - this.roles = roles; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getAccount() { - return account; - } - - public void setAccount(String account) { - this.account = account; - } - - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = code; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getPassword2() { - return password2; - } - - public void setPassword2(String password2) { - this.password2 = password2; - } - - public List getRoles() { - return roles; - } - - public void setRoles(List roles) { - this.roles = roles; - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - builder.append("RegisterCommand [username=").append(username).append(", account=").append(account) - .append(", code=").append(code).append(", password=").append(password).append(", password2=") - .append(password2).append(", roles=").append(roles).append("]"); - return builder.toString(); - } -}