From a71d75029ee07837efb3457636da5157baa1d90c Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 7 Aug 2024 16:54:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=B5=8B=E8=AF=95=E7=B1=BB?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../validator/map/test/MapValidatorTests.java | 110 +++++++++--------- 1 file changed, 56 insertions(+), 54 deletions(-) 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 f8da6dc..23996f8 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 @@ -21,66 +21,68 @@ 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("角色标识不能为空!")); - - } - }; + private static final MapValidator validator = ParamsValidator.INSTANCE; @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", "")); + params.put(ParamsValidator.USERNAME, "ZhouXY"); + params.put(ParamsValidator.ACCOUNT, "zhouxy@code108.cn"); + params.put(ParamsValidator.PASSWORD, "99Code108"); + params.put(ParamsValidator.PASSWORD2, "99Code108"); + params.put(ParamsValidator.AGE, 18); + params.put(ParamsValidator.BOOLEAN, true); + params.put(ParamsValidator.ROLE_LIST, Arrays.asList("admin", "")); Map validedParams = validator.validateAndCopy(params); System.out.println(validedParams); } } + +class ParamsValidator extends MapValidator { + public static final String USERNAME = "username"; + public static final String ACCOUNT = "account"; + public static final String PASSWORD = "password"; + public static final String PASSWORD2 = "password2"; + public static final String AGE = "age"; + public static final String BOOLEAN = "boolean"; + public static final String ROLE_LIST = "roleList"; + + public static final ParamsValidator INSTANCE = new ParamsValidator(); + + 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))); + + 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("角色标识不能为空!")); + } +}