From 0a5dbd9f535bd4e447f4f6fa8c0d702707af2484 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 28 May 2025 23:06:48 +0800 Subject: [PATCH] =?UTF-8?q?refactor!:=20=E5=88=A0=E9=99=A4=E6=84=8F?= =?UTF-8?q?=E4=B9=89=E4=B8=8D=E5=A4=A7=E7=9A=84=20`Validator`=20=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zhouxy/plusone/validator/Validator.java | 70 -------- .../example/validator/ValidatorTests.java | 162 ------------------ 2 files changed, 232 deletions(-) delete mode 100644 plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/Validator.java delete mode 100644 plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ValidatorTests.java diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/Validator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/Validator.java deleted file mode 100644 index 011b4b5..0000000 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/Validator.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2022-2025 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package xyz.zhouxy.plusone.validator; - -import java.util.function.Consumer; -import java.util.function.Function; -import java.util.function.Predicate; -import java.util.function.Supplier; - -/** - * 校验器 - * - *

- * 可以使用以下方式初始化一个校验器: - *

- * - *
- * var validator = new Validator<Integer>()
- *         .addRule(value -> Objects.nonNull(value), "value 不能为空")
- *         .addRule(value -> (value >= 0 && value <= 500), "value 应在 [0, 500] 内");
- * 
- * - *

- * 然后通过校验器的 {@link #validate} 方法对指定对象进行校验。 - *

- * - *
- * validator.validate(666);
- * 
- *

- * - * @author ZhouXY - * @see IValidateRequired - * @see BaseValidator - */ -public final class Validator extends BaseValidator { - public final Validator addRule(final Predicate rule, final String errorMessage) { - withRule(rule, errorMessage); - return this; - } - - public final Validator addRule(Predicate rule, Supplier exceptionCreator) { - withRule(rule, exceptionCreator); - return this; - } - - public final Validator addRule(Predicate rule, Function exceptionCreator) { - withRule(rule, exceptionCreator); - return this; - } - - public final Validator addRule(Consumer rule) { - withRule(rule); - return this; - } -} diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ValidatorTests.java deleted file mode 100644 index 1ad07b5..0000000 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ValidatorTests.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright 2024-2025 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package xyz.zhouxy.plusone.example.validator; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -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.junit.jupiter.api.Test; - -import xyz.zhouxy.plusone.commons.collection.CollectionTools; -import xyz.zhouxy.plusone.commons.function.PredicateTools; -import xyz.zhouxy.plusone.commons.util.AssertTools; -import xyz.zhouxy.plusone.commons.util.RegexTools; -import xyz.zhouxy.plusone.commons.util.StringTools; -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) - && StringTools.isNotEmpty(username) - && StringTools.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(); - AssertTools.checkArgument(Objects.nonNull(code), "验证码不能为空"); - AssertTools.checkArgument(RegexTools.matches(code, CAPTCHA), "验证码不符合规范"); - }) - // 传入 rule - .addRule(command -> { - String password = command.getPassword(); - AssertTools.checkArgument(StringTools.isNotEmpty(password), "密码不能为空"); - AssertTools.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()), - "两次输入的密码不一致"); - assertDoesNotThrow(() -> registerCommandValidator.validate(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(); - } - } -}