From 3cac2705748a109b568c4e82e2150a3af29f8986 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 7 Aug 2024 17:42:35 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=EF=BC=8CMa?= =?UTF-8?q?pValidator=20=E5=8F=AF=E4=BD=BF=E7=94=A8=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E7=9A=84=20PropertyValidator=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../validator/{map => }/MapValidator.java | 46 ++++++++++++++++--- .../plusone/validator/map/EntryValidator.java | 22 --------- .../validator/map/test/MapValidatorTests.java | 43 ++++++++--------- 3 files changed, 57 insertions(+), 54 deletions(-) rename src/main/java/xyz/zhouxy/plusone/validator/{map => }/MapValidator.java (59%) delete mode 100644 src/main/java/xyz/zhouxy/plusone/validator/map/EntryValidator.java diff --git a/src/main/java/xyz/zhouxy/plusone/validator/map/MapValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java similarity index 59% rename from src/main/java/xyz/zhouxy/plusone/validator/map/MapValidator.java rename to src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java index e7432dc..151133e 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/map/MapValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java @@ -1,4 +1,4 @@ -package xyz.zhouxy.plusone.validator.map; +package xyz.zhouxy.plusone.validator; import java.util.Arrays; import java.util.Collection; @@ -26,6 +26,8 @@ public abstract class MapValidator { this.keys = keys.stream().collect(Collectors.toSet()); } + // ========== validate & validateAndCopy ========== + public final Map validateAndCopy(Map obj) { return validateAndCopyInternal(obj, this.keys); } @@ -50,17 +52,47 @@ public abstract class MapValidator { this.consumers.forEach(consumer -> consumer.accept(obj)); } - @SuppressWarnings("unused") - protected final EntryValidator checkValue(K key, Class clazz) { - return checkValue(key); - } + // ========== ruleFor ========== - protected final EntryValidator checkValue(K key) { - EntryValidator validator = new EntryValidator<>(key); + protected final ObjectValidator, V> ruleFor(K key) { + ObjectValidator, V> validator = new ObjectValidator<>(m -> m.get(key)); this.consumers.add(validator::validate); return validator; } + protected final IntValidator> ruleForInt(K key) { + IntValidator> validator = new IntValidator<>(m -> (Integer) m.get(key)); + this.consumers.add(validator::validate); + return validator; + } + + protected final DoubleValidator> ruleForDouble(K key) { + DoubleValidator> validator = new DoubleValidator<>(m -> (Double) m.get(key)); + this.consumers.add(validator::validate); + return validator; + } + + protected final BoolValidator> ruleForBool(K key) { + BoolValidator> validator = new BoolValidator<>(m -> (Boolean) m.get(key)); + this.consumers.add(validator::validate); + return validator; + } + + protected final StringValidator> ruleForString(K key) { + StringValidator> validator = new StringValidator<>(m -> (String) m.get(key)); + this.consumers.add(validator::validate); + return validator; + } + + protected final CollectionValidator, E> ruleForCollection(K key) { + @SuppressWarnings("unchecked") + CollectionValidator, E> validator = new CollectionValidator<>(m -> (Collection) m.get(key)); + this.consumers.add(validator::validate); + return validator; + } + + // ========== withRule ========== + protected final void withRule(Predicate> rule, String errMsg) { withRule(rule, map -> new IllegalArgumentException(errMsg)); } diff --git a/src/main/java/xyz/zhouxy/plusone/validator/map/EntryValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/map/EntryValidator.java deleted file mode 100644 index fd42bb0..0000000 --- a/src/main/java/xyz/zhouxy/plusone/validator/map/EntryValidator.java +++ /dev/null @@ -1,22 +0,0 @@ -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/test/java/xyz/zhouxy/plusone/validator/map/test/MapValidatorTests.java b/src/test/java/xyz/zhouxy/plusone/validator/map/test/MapValidatorTests.java index 23996f8..fb9e156 100644 --- a/src/test/java/xyz/zhouxy/plusone/validator/map/test/MapValidatorTests.java +++ b/src/test/java/xyz/zhouxy/plusone/validator/map/test/MapValidatorTests.java @@ -3,7 +3,6 @@ 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; @@ -12,11 +11,8 @@ 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; +import xyz.zhouxy.plusone.validator.MapValidator; public // class MapValidatorTests { @@ -52,36 +48,33 @@ class ParamsValidator extends MapValidator { private ParamsValidator() { super(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))); + ruleForString(USERNAME) + .notBlank("用户名不能为空") + .matches(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 })), - "请输入正确的邮箱地址或手机号"); + ruleForString(ACCOUNT) + .notBlank("账号不能为空") + .matchesOne(new Pattern[] { PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE }, "请输入正确的邮箱地址或手机号"); - checkValue(PASSWORD, String.class) - .withRule(StringUtils::isNotEmpty, "密码不能为空") - .withRule(pwd -> RegexTools.matches(pwd, PatternConsts.PASSWORD), "密码不符合规范"); + ruleForString(PASSWORD) + .notEmpty("密码不能为空") + .matches(PatternConsts.PASSWORD, "密码不符合规范"); // 校验到多个属性,只能针对 map 本身进行校验 withRule(m -> Objects.equals(m.get(PASSWORD), m.get(PASSWORD2)), "两次输入的密码不一样!"); - // 通过泛型方式调用方法,指定数据类型 - this.checkValue(AGE) + ruleForInt(AGE) .withRule(Objects::nonNull) - .withRule(age -> (18 <= age && 60 >= age)); + .between(18, 61); - checkValue(BOOLEAN, Boolean.class) - .withRule(Objects::nonNull, "Boolean property could not be null.") - .withRule(b -> b, "Boolean property must be true."); + ruleForBool(BOOLEAN) + .notNull("Boolean property could not be null.") + .isTrue("Boolean property must be true."); - this.>checkValue(ROLE_LIST) - .withRule(CollectionTools::isNotEmpty, "角色列表不能为空!") + this.ruleForCollection(ROLE_LIST) + .notEmpty("角色列表不能为空!") .withRule(l -> l.stream().allMatch(StringUtils::isNotBlank), () -> new IllegalArgumentException("角色标识不能为空!")); }