diff --git a/pom.xml b/pom.xml index cdab09f..2a5a8ad 100644 --- a/pom.xml +++ b/pom.xml @@ -1,6 +1,7 @@ - 4.0.0 @@ -9,8 +10,7 @@ 0.0.1-SNAPSHOT plusone-validator - - http://www.example.com + http://www.zhouxy.xyz UTF-8 @@ -20,27 +20,37 @@ - xyz.zhouxy.plusone - plusone-commons - 0.0.1-SNAPSHOT + com.google.guava + guava + 32.0.1-jre - junit - junit - 4.11 + org.apache.commons + commons-lang3 + 3.13.0 + test + + + xyz.zhouxy.plusone + plusone-commons + 0.1.0-SNAPSHOT + test + + + org.junit.jupiter + junit-jupiter-api + 5.9.2 test - + - maven-clean-plugin 3.1.0 - maven-resources-plugin 3.0.2 @@ -65,7 +75,6 @@ maven-deploy-plugin 2.8.2 - maven-site-plugin 3.7.1 diff --git a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java deleted file mode 100644 index 08f13d0..0000000 --- a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java +++ /dev/null @@ -1,86 +0,0 @@ -package xyz.zhouxy.plusone.validator; - -import java.util.ArrayList; -import java.util.List; -import java.util.function.Function; -import java.util.function.Predicate; -import java.util.function.Supplier; - -/** - * 校验器 - * - *

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

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

- * 也可以通过继承本类,定义一个校验器(可使用单例模式)。 - *

- * - *

- * 然后通过校验器的 {@link #validate} 方法,或 - * {@link ValidateUtil#validate(Object, Validator)} 对指定对象进行校验。 - *

- * - *
- * ValidateUtil.validate(255, validator);
- * 
- * - *
- * validator.validate(666);
- * 
- *

- * - * @author ZhouXY - * @see IValidateRequired - * @see ValidateUtil - * @see Validator - */ -public abstract class BaseValidator { - - private final List> rules = new ArrayList<>(); - - protected BaseValidator() { - } - - protected final void withRule(Predicate rule, String errorMessage) { - withRule(rule, () -> new InvalidInputException(errorMessage)); - } - - protected final void withRule(Predicate rule, Supplier exceptionCreator) { - withRule(rule, value -> exceptionCreator.get()); - } - - protected final void withRule(Predicate rule, Function exceptionCreator) { - this.rules.add(new RuleInfo<>(rule, exceptionCreator)); - } - - public void validate(T obj) { - this.rules.forEach(ruleInfo -> ruleInfo.validate(obj)); - } - - protected static class RuleInfo { - private final Predicate rule; - private final Function exceptionCreator; - - private RuleInfo(Predicate rule, Function exceptionCreator) { - this.rule = rule; - this.exceptionCreator = exceptionCreator; - } - - private void validate(T obj) { - if (!rule.test(obj)) { - throw exceptionCreator.apply(obj); - } - } - } -} 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 5a23431..0000000 --- a/src/main/java/xyz/zhouxy/plusone/validator/InvalidInputException.java +++ /dev/null @@ -1,53 +0,0 @@ -package xyz.zhouxy.plusone.validator; - -import xyz.zhouxy.plusone.exception.PlusoneException; - -/** - * 4040200 - 无效的用户输入 - * - * @author ZhouXY - */ -public class InvalidInputException extends PlusoneException { - - private static final long serialVersionUID = 7956661913360059670L; - - public static final int ERROR_CODE = 4040200; - - private InvalidInputException(int code, String msg) { - super(code, msg); - } - - private InvalidInputException(int code, Throwable cause) { - super(code, cause); - } - - private InvalidInputException(int code, String msg, Throwable cause) { - super(code, msg, cause); - } - - public InvalidInputException(String msg) { - this(ERROR_CODE, msg); - } - - public InvalidInputException(Throwable cause) { - this(ERROR_CODE, cause); - } - - 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/MapValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java new file mode 100644 index 0000000..bc7f769 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/validator/MapValidator.java @@ -0,0 +1,125 @@ +package xyz.zhouxy.plusone.validator; + +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 PropertyValidator checkProperty(K key, Class clazz) { + return checkProperty(key); + } + + protected final PropertyValidator checkProperty(K key) { + PropertyValidator validator = new PropertyValidator<>(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); + } + }); + } +} + +class PropertyValidator { + + private final K key; + + public PropertyValidator(K key) { + this.key = key; + } + + private final List> consumers = new LinkedList<>(); + + public final PropertyValidator withRules(Predicate rule) { + return withRules(rule, v -> new IllegalArgumentException(key.toString())); + } + + public final PropertyValidator withRules(Predicate rule, String errorMsg) { + return withRules(rule, v -> new IllegalArgumentException(errorMsg)); + } + + public final PropertyValidator withRules(Predicate rule, + Supplier e) { + return withRules(rule, v -> e.get()); + } + + public final PropertyValidator withRules(Predicate rule, + Function e) { + this.consumers.add(v -> { + if (!rule.test(v)) { + throw e.apply(v); + } + }); + return this; + } + + public final void validate(Map map) { + for (Consumer consumer : consumers) { + @SuppressWarnings("unchecked") + V v = (V) map.get(this.key); + consumer.accept(v); + } + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/PredicateTools.java b/src/main/java/xyz/zhouxy/plusone/validator/PredicateTools.java new file mode 100644 index 0000000..2a06afc --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/validator/PredicateTools.java @@ -0,0 +1,86 @@ +package xyz.zhouxy.plusone.validator; + +import java.util.Collection; +import java.util.function.Predicate; +import java.util.regex.Pattern; + +public class PredicateTools { + + private PredicateTools() { + throw new IllegalStateException("Utility class"); + } + + public static Predicate asPredicate(Predicate predicate) { + return predicate; + } + + public static Predicate notNull() { + return obj -> obj != null; + } + + public static int length(CharSequence cs) { + return cs == null ? 0 : cs.length(); + } + + public static boolean isNotBlank(CharSequence cs) { + return !isBlank(cs); + } + + public static Predicate isNotBlank() { + return PredicateTools::isNotBlank; + } + + public static boolean isBlank(CharSequence cs) { + final int strLen = length(cs); + if (strLen == 0) { + return true; + } + for (int i = 0; i < strLen; i++) { + if (!Character.isWhitespace(cs.charAt(i))) { + return false; + } + } + return true; + } + + public static Predicate isBlank() { + return PredicateTools::isBlank; + } + + public static Predicate matches(Pattern pattern) { + return str -> pattern.matcher(str).matches(); + } + + public static Predicate matchesAll(Pattern... patterns) { + return str -> { + for (Pattern pattern : patterns) { + if (!pattern.matcher(str).matches()) { + return false; + } + } + return true; + }; + } + + public static Predicate matchesOne(Pattern... patterns) { + return str -> { + for (Pattern pattern : patterns) { + if (pattern.matcher(str).matches()) { + return true; + } + } + return false; + }; + } + + public static Predicate> forAllItems(Predicate predicate) { + return c -> { + for (T t : c) { + if (!predicate.test(t)) { + return false; + } + } + return true; + }; + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/ValidateUtil.java b/src/main/java/xyz/zhouxy/plusone/validator/ValidateUtil.java deleted file mode 100644 index 438b197..0000000 --- a/src/main/java/xyz/zhouxy/plusone/validator/ValidateUtil.java +++ /dev/null @@ -1,23 +0,0 @@ -package xyz.zhouxy.plusone.validator; - -/** - * 校验工具类 - *

- * 对 {@link IValidateRequired} 的实现类对象进行校验 - *

- * - * @author ZhouXY - * - * @see BaseValidator - * @see Validator - * @see IValidateRequired - */ -public class ValidateUtil { - private ValidateUtil() { - throw new IllegalStateException("Utility class"); - } - - public static void validate(T obj, BaseValidator validator) { - validator.validate(obj); - } -} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/Validator.java b/src/main/java/xyz/zhouxy/plusone/validator/Validator.java deleted file mode 100644 index b56ff50..0000000 --- a/src/main/java/xyz/zhouxy/plusone/validator/Validator.java +++ /dev/null @@ -1,42 +0,0 @@ -package xyz.zhouxy.plusone.validator; - -import java.util.function.Predicate; - -/** - * 校验器 - * - *

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

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

- * 然后通过校验器的 {@link #validate} 方法,或 - * {@link ValidateUtil#validate(Object, Validator)} 对指定对象进行校验。 - *

- * - *
- * validator.validate(666);
- * 
- * - *
- * ValidateUtil.validate(255, validator);
- * 
- *

- * - * @author ZhouXY - * @see IValidateRequired - * @see ValidateUtil - * @see BaseValidator - */ -public final class Validator extends BaseValidator { - public final Validator addRule(final Predicate rule, final String errorMessage) { - withRule(rule, errorMessage); - return this; - } -} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/validator2/BaseValidator2.java b/src/main/java/xyz/zhouxy/plusone/validator/validator2/BaseValidator2.java deleted file mode 100644 index 257af16..0000000 --- a/src/main/java/xyz/zhouxy/plusone/validator/validator2/BaseValidator2.java +++ /dev/null @@ -1,22 +0,0 @@ -package xyz.zhouxy.plusone.validator.validator2; - -import java.util.ArrayList; -import java.util.List; -import java.util.function.Function; - -public abstract class BaseValidator2 { - - private List> valueValidators = new ArrayList<>(); - - protected final ValueValidator ruleFor(Function getter) { - ValueValidator validValueHolder = new ValueValidator<>(getter); - valueValidators.add(validValueHolder); - return validValueHolder; - } - - public void validate(T obj) { - for (ValueValidator valueValidator : this.valueValidators) { - valueValidator.validateProperty(obj); - } - } -} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/validator2/ValueValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/validator2/ValueValidator.java deleted file mode 100644 index e80b136..0000000 --- a/src/main/java/xyz/zhouxy/plusone/validator/validator2/ValueValidator.java +++ /dev/null @@ -1,379 +0,0 @@ -package xyz.zhouxy.plusone.validator.validator2; - -import java.util.ArrayList; -import java.util.Collection; -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; - -import cn.hutool.core.util.StrUtil; -import xyz.zhouxy.plusone.constant.RegexConsts; -import xyz.zhouxy.plusone.util.RegexUtil; -import xyz.zhouxy.plusone.validator.InvalidInputException; - -public class ValueValidator { - Function getter; - List> rules = new ArrayList<>(); - - public ValueValidator(Function getter) { - this.getter = getter; - } - - private void withRule(Predicate condition, - Function exceptionCreator) { - withRule(value -> { - if (!condition.test(value)) { - throw exceptionCreator.apply(value); - } - }); - } - - private void withRule(Consumer rule) { - this.rules.add(rule); - } - - // ==================== - // ====== Object ====== - // ==================== - - // ====== notNull ===== - - public ValueValidator notNull() { - return notNull("Value could not be null."); - } - - public ValueValidator notNull(String errMsg) { - return notNull(convertExceptionCreator(errMsg)); - } - - public ValueValidator notNull(Supplier exceptionCreator) { - return notNull(convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator notNull(Function exceptionCreator) { - withRule(Objects::nonNull, exceptionCreator); - return this; - } - - // ====== isNull ===== - - public ValueValidator isNull(String errMsg) { - return isNull(convertExceptionCreator(errMsg)); - } - - public ValueValidator isNull(Supplier exceptionCreator) { - return isNull(convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator isNull(Function exceptionCreator) { - withRule(Objects::isNull, exceptionCreator); - return this; - } - - // ===== equals ===== - - public ValueValidator equalsThat(Object that) { - return equalsThat(that, value -> new InvalidInputException(String.format("(%s) 必须与 (%s) 相等", value, that))); - } - - public ValueValidator equalsThat(Object that, String errMsg) { - return equalsThat(that, convertExceptionCreator(errMsg)); - } - - public ValueValidator equalsThat( - Object that, Supplier exceptionCreator) { - return equalsThat(that, convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator equalsThat( - Object that, Function exceptionCreator) { - withRule(value -> Objects.equals(value, that), exceptionCreator); - return this; - } - - // ===== state ===== - - public ValueValidator state(Predicate condition) { - return state(condition, "无效的用户输入"); - } - - public ValueValidator state(Predicate condition, String errMsg) { - return state(condition, convertExceptionCreator(errMsg)); - } - - public ValueValidator state( - Predicate condition, - Supplier exceptionCreator) { - return state(condition, convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator state( - Predicate condition, - Function exceptionCreator) { - withRule(condition, exceptionCreator); - return this; - } - - // ================= - // ====== int ====== - // ================= - - public ValueValidator between(int min, int max) { - return between(min, max, String.format("数值不在 %s 和 %s 之间", String.valueOf(min), String.valueOf(max))); - } - - public ValueValidator between(int min, int max, String errMsg) { - return between(min, max, convertExceptionCreator(errMsg)); - } - - public ValueValidator between(int min, int max, - Supplier exceptionCreator) { - return between(min, max, convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator between(int min, int max, - Function exceptionCreator) { - withRule(value -> ((int) value >= min && (int) value < max), exceptionCreator); - return this; - } - - // ==================== - // ====== double ====== - // ==================== - - public ValueValidator between(double min, double max) { - return between(min, max, String.format("数值不在 %s 和 %s 之间", String.valueOf(min), String.valueOf(max))); - } - - public ValueValidator between(double min, double max, String errMsg) { - return between(min, max, convertExceptionCreator(errMsg)); - } - - public ValueValidator between(double min, double max, - Supplier exceptionCreator) { - return between(min, max, convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator between(double min, double max, - Function exceptionCreator) { - withRule(value -> ((double) value >= min && (double) value < max), exceptionCreator); - return this; - } - - // ================================ - // ====== Collection, String ====== - // ================================ - - // ====== notEmpty ===== - - public ValueValidator notEmpty(String errMsg) { - return notEmpty(convertExceptionCreator(errMsg)); - } - - public ValueValidator notEmpty(Supplier exceptionCreator) { - return notEmpty(convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator notEmpty(Function exceptionCreator) { - withRule(value -> { - if (value == null) { - return false; - } - if (value instanceof Collection) { - return !((Collection) value).isEmpty(); - } - if (value instanceof String) { - return !((String) value).isEmpty(); - } - return false; - }, exceptionCreator); - return this; - } - - // ====== isEmpty ===== - - public ValueValidator isEmpty(String errMsg) { - return isEmpty(convertExceptionCreator(errMsg)); - } - - public ValueValidator isEmpty(Supplier exceptionCreator) { - return isEmpty(convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator isEmpty(Function exceptionCreator) { - withRule(value -> { - if (value == null) { - return false; - } - if (value instanceof Collection) { - return ((Collection) value).isEmpty(); - } - if (value instanceof String) { - return ((String) value).isEmpty(); - } - return false; - }, exceptionCreator); - return this; - } - - // ===================== - // ====== boolean ====== - // ===================== - - // ====== isTrue ====== - - public ValueValidator isTrue() { - return isTrue("The value must be true."); - } - - public ValueValidator isTrue(String errMsg) { - return isTrue(convertExceptionCreator(errMsg)); - } - - public ValueValidator isTrue(Supplier exceptionCreator) { - return isTrue(convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator isTrue(Function exceptionCreator) { - withRule(Boolean.TRUE::equals, exceptionCreator); - return this; - } - - // ====== isFalse ====== - - public ValueValidator isFalse() { - return isFalse("The value must be false."); - } - - public ValueValidator isFalse(String errMsg) { - return isFalse(convertExceptionCreator(errMsg)); - } - - public ValueValidator isFalse(Supplier exceptionCreator) { - return isFalse(convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator isFalse(Function exceptionCreator) { - withRule(Boolean.FALSE::equals, exceptionCreator); - return this; - } - - // ==================== - // ====== String ====== - // ==================== - - // ===== matches ===== - - public ValueValidator matches(String regex, String errMsg) { - return matches(regex, convertExceptionCreator(errMsg)); - } - - public ValueValidator matches( - String regex, - Supplier exceptionCreator) { - return matches(regex, convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator matches( - String regex, - Function exceptionCreator) { - withRule(input -> RegexUtil.matches((String) input, regex), exceptionCreator); - return this; - } - - // ===== matchesOr ===== - - public ValueValidator matchesOr(String[] regexs, String errMsg) { - return matchesOr(regexs, convertExceptionCreator(errMsg)); - } - - public ValueValidator matchesOr( - String[] regexs, - Supplier exceptionCreator) { - return matchesOr(regexs, convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator matchesOr( - String[] regexs, - Function exceptionCreator) { - withRule(input -> RegexUtil.matchesOr((String) input, regexs), exceptionCreator); - return this; - } - - // ===== matchesAnd ===== - - public ValueValidator matchesAnd(String[] regexs, String errMsg) { - return matchesAnd(regexs, convertExceptionCreator(errMsg)); - } - - public ValueValidator matchesAnd( - String[] regexs, - Supplier exceptionCreator) { - return matchesAnd(regexs, convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator matchesAnd( - String[] regexs, - Function exceptionCreator) { - withRule(input -> RegexUtil.matchesAnd((String) input, regexs), exceptionCreator); - return this; - } - - // ===== notBlank ===== - - public ValueValidator notBlank() { - return notBlank("This String argument must have text; it must not be null, empty, or blank"); - } - - public ValueValidator notBlank(String errMsg) { - return notBlank(convertExceptionCreator(errMsg)); - } - - public ValueValidator notBlank(Supplier exceptionCreator) { - return notBlank(convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator notBlank(Function exceptionCreator) { - withRule(input -> StrUtil.isNotBlank((String) input), exceptionCreator); - return this; - } - - // ===== email ===== - - public ValueValidator email() { - return email("The value is not an email address."); - } - - public ValueValidator email(String errMsg) { - return email(convertExceptionCreator(errMsg)); - } - - public ValueValidator email(Supplier exceptionCreator) { - return email(convertExceptionCreator(exceptionCreator)); - } - - public ValueValidator email(Function exceptionCreator) { - return matches(RegexConsts.EMAIL, exceptionCreator); - } - - // ======================================================================== - - void validateProperty(DTO obj) { - PROPERTY value = this.getter.apply(obj); - for (Consumer rule : this.rules) { - rule.accept(value); - } - } - - private static Function convertExceptionCreator(String errMsg) { - return convertExceptionCreator(errMsg); - } - - private static Function convertExceptionCreator( - Supplier exceptionSupplier) { - return value -> exceptionSupplier.get(); - } -} diff --git a/src/test/java/xyz/zhouxy/plusone/validator/MapValidatorTests.java b/src/test/java/xyz/zhouxy/plusone/validator/MapValidatorTests.java new file mode 100644 index 0000000..2efc21a --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/validator/MapValidatorTests.java @@ -0,0 +1,75 @@ +package xyz.zhouxy.plusone.validator; + +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 org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; + +import xyz.zhouxy.plusone.commons.constant.PatternConsts; + +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 }) { + { + checkProperty(USERNAME, String.class) + .withRules(StringUtils::isNotEmpty) + .withRules(PredicateTools.matches(PatternConsts.USERNAME), + username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username))); + + checkProperty(ACCOUNT, String.class) + .withRules(StringUtils::isNotEmpty) + .withRules(PredicateTools.matchesOne(PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE), + "请输入正确的邮箱地址或手机号"); + + checkProperty(PASSWORD, String.class) + .withRules(StringUtils::isNotEmpty) + .withRules(PredicateTools.matches(PatternConsts.PASSWORD), "密码不符合规范"); + + withRule(m -> Objects.equals(m.get(PASSWORD), m.get(PASSWORD2)), + "两次输入的密码不一样!"); + + checkProperty(AGE, Integer.class) + .withRules(Objects::nonNull) + .withRules(age -> (18 <= age && 60 >= age)); + + checkProperty(BOOLEAN, Boolean.class) + .withRules(PredicateTools.notNull(), "Boolean property could not be null.") + .withRules(b -> b, "Boolean property must be true."); + + this.>checkProperty(ROLE_LIST) + .withRules(c -> c != null && !c.isEmpty(), "角色列表不能为空!") + .withRules(PredicateTools.forAllItems(Objects::nonNull), () -> new NullPointerException(ROLE_LIST)); + } + }; + + @Test + void test() { + 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); + } + +}