diff --git a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java index 08f13d0..1782fc9 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/BaseValidator.java @@ -1,86 +1,80 @@ package xyz.zhouxy.plusone.validator; import java.util.ArrayList; +import java.util.Collection; import java.util.List; +import java.util.function.Consumer; 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 { +public class BaseValidator { + private final List> rules = new ArrayList<>(); + private final List> propertyValidators = new ArrayList<>(); - private final List> rules = new ArrayList<>(); - - protected BaseValidator() { + protected void withRule(final Predicate rule, final String errorMessage) { + withRule(rule, value -> new InvalidInputException(errorMessage)); } - protected final void withRule(Predicate rule, String errorMessage) { - withRule(rule, () -> new InvalidInputException(errorMessage)); + protected void withRule(Predicate rule, Supplier exceptionBuilder) { + withRule(rule, value -> exceptionBuilder.get()); } - protected final void withRule(Predicate rule, Supplier exceptionCreator) { - withRule(rule, value -> exceptionCreator.get()); + protected void withRule(Predicate condition, + Function exceptionBuilder) { + withRule(value -> { + if (!condition.test(value)) { + throw exceptionBuilder.apply(value); + } + }); } - protected final void withRule(Predicate rule, Function exceptionCreator) { - this.rules.add(new RuleInfo<>(rule, exceptionCreator)); + protected void withRule(Consumer rule) { + this.rules.add(rule); + } + + protected final ObjectValidator ruleFor(Function getter) { + ObjectValidator validValueHolder = new ObjectValidator<>(getter); + propertyValidators.add(validValueHolder); + return validValueHolder; + } + + protected final IntValidator ruleForInt(Function getter) { + IntValidator validValueHolder = new IntValidator<>(getter); + propertyValidators.add(validValueHolder); + return validValueHolder; + } + + protected final DoubleValidator ruleForDouble(Function getter) { + DoubleValidator validValueHolder = new DoubleValidator<>(getter); + propertyValidators.add(validValueHolder); + return validValueHolder; + } + + protected final BoolValidator ruleForBool(Function getter) { + BoolValidator validValueHolder = new BoolValidator<>(getter); + propertyValidators.add(validValueHolder); + return validValueHolder; + } + + protected final StringValidator ruleForString(Function getter) { + StringValidator validValueHolder = new StringValidator<>(getter); + propertyValidators.add(validValueHolder); + return validValueHolder; + } + + protected final CollectionValidator ruleForCollection(Function> getter) { + CollectionValidator validValueHolder = new CollectionValidator<>(getter); + propertyValidators.add(validValueHolder); + return validValueHolder; } 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; + for (Consumer rule : this.rules) { + rule.accept(obj); } - - private void validate(T obj) { - if (!rule.test(obj)) { - throw exceptionCreator.apply(obj); - } + for (PropertyValidator valueValidator : this.propertyValidators) { + valueValidator.validate(obj); } } } diff --git a/src/main/java/xyz/zhouxy/plusone/validator/BoolValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/BoolValidator.java new file mode 100644 index 0000000..9156a49 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/validator/BoolValidator.java @@ -0,0 +1,56 @@ +package xyz.zhouxy.plusone.validator; + +import java.util.function.Function; +import java.util.function.Supplier; + +class BoolValidator extends PropertyValidator> { + + BoolValidator(Function getter) { + super(getter); + } + + // ====== isTrue ====== + + public BoolValidator isTrue() { + return isTrue("The value must be true."); + } + + public BoolValidator isTrue(String errMsg) { + return isTrue(convertExceptionCreator(errMsg)); + } + + public BoolValidator isTrue(Supplier exceptionCreator) { + return isTrue(convertExceptionCreator(exceptionCreator)); + } + + public BoolValidator isTrue( + Function exceptionCreator) { + withRule(Boolean.TRUE::equals, exceptionCreator); + return this; + } + + // ====== isFalse ====== + + public BoolValidator isFalse() { + return isFalse("The value must be false."); + } + + public BoolValidator isFalse(String errMsg) { + return isFalse(convertExceptionCreator(errMsg)); + } + + public BoolValidator isFalse(Supplier exceptionCreator) { + return isFalse(convertExceptionCreator(exceptionCreator)); + } + + public BoolValidator isFalse( + Function exceptionCreator) { + withRule(Boolean.FALSE::equals, exceptionCreator); + return this; + } + + @Override + protected BoolValidator returnThis() { + return this; + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java new file mode 100644 index 0000000..bf2136b --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/validator/CollectionValidator.java @@ -0,0 +1,63 @@ +package xyz.zhouxy.plusone.validator; + +import java.util.Collection; +import java.util.function.Function; +import java.util.function.Supplier; + +class CollectionValidator extends PropertyValidator, CollectionValidator> { + + CollectionValidator(Function> getter) { + super(getter); + } + + // ================================ + // ====== Collection, String ====== + // ================================ + + // ====== notEmpty ===== + + public CollectionValidator notEmpty(String errMsg) { + return notEmpty(convertExceptionCreator(errMsg)); + } + + public CollectionValidator notEmpty(Supplier exceptionCreator) { + return notEmpty(convertExceptionCreator(exceptionCreator)); + } + + public CollectionValidator notEmpty( + Function, E> exceptionCreator) { + withRule(value -> { + if (value == null) { + return false; + } + return !((Collection) value).isEmpty(); + }, exceptionCreator); + return this; + } + + // ====== isEmpty ===== + + public CollectionValidator isEmpty(String errMsg) { + return isEmpty(convertExceptionCreator(errMsg)); + } + + public CollectionValidator isEmpty(Supplier exceptionCreator) { + return isEmpty(convertExceptionCreator(exceptionCreator)); + } + + public CollectionValidator isEmpty( + Function, E> exceptionCreator) { + withRule(value -> { + if (value == null) { + return false; + } + return ((Collection) value).isEmpty(); + }, exceptionCreator); + return this; + } + + @Override + protected CollectionValidator returnThis() { + return this; + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/DoubleValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/DoubleValidator.java new file mode 100644 index 0000000..ac78fac --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/validator/DoubleValidator.java @@ -0,0 +1,35 @@ +package xyz.zhouxy.plusone.validator; + +import java.util.function.Function; +import java.util.function.Supplier; + +class DoubleValidator extends PropertyValidator> { + + DoubleValidator(Function getter) { + super(getter); + } + + public DoubleValidator between(double min, double max) { + return between(min, max, String.format("数值不在 %s 和 %s 之间", String.valueOf(min), String.valueOf(max))); + } + + public DoubleValidator between(double min, double max, String errMsg) { + return between(min, max, convertExceptionCreator(errMsg)); + } + + public DoubleValidator between(double min, double max, + Supplier exceptionCreator) { + return between(min, max, convertExceptionCreator(exceptionCreator)); + } + + public DoubleValidator between(double min, double max, + Function exceptionCreator) { + withRule(value -> (value >= min && value < max), exceptionCreator); + return this; + } + + @Override + protected DoubleValidator returnThis() { + return this; + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/IValidateRequired.java b/src/main/java/xyz/zhouxy/plusone/validator/IValidateRequired.java new file mode 100644 index 0000000..82e98ae --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/validator/IValidateRequired.java @@ -0,0 +1,13 @@ +package xyz.zhouxy.plusone.validator; + +/** + * 自带校验方法,校验不通过时直接抛异常。 + * + * @author ZhouXY + * + * @see ValidateUtil + * @see BaseValidator + */ +public interface IValidateRequired { + void validate(); +} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/IntValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/IntValidator.java new file mode 100644 index 0000000..59f6e1f --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/validator/IntValidator.java @@ -0,0 +1,35 @@ +package xyz.zhouxy.plusone.validator; + +import java.util.function.Function; +import java.util.function.Supplier; + +class IntValidator extends PropertyValidator> { + + IntValidator(Function getter) { + super(getter); + } + + public IntValidator between(int min, int max) { + return between(min, max, String.format("数值不在 %s 和 %s 之间", String.valueOf(min), String.valueOf(max))); + } + + public IntValidator between(int min, int max, String errMsg) { + return between(min, max, convertExceptionCreator(errMsg)); + } + + public IntValidator between(int min, int max, + Supplier exceptionCreator) { + return between(min, max, convertExceptionCreator(exceptionCreator)); + } + + public IntValidator between(int min, int max, + Function exceptionCreator) { + withRule(value -> (value >= min && value < max), exceptionCreator); + return this; + } + + @Override + protected IntValidator returnThis() { + return this; + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/ObjectValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/ObjectValidator.java new file mode 100644 index 0000000..bf4c36d --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/validator/ObjectValidator.java @@ -0,0 +1,15 @@ +package xyz.zhouxy.plusone.validator; + +import java.util.function.Function; + +class ObjectValidator extends PropertyValidator> { + + ObjectValidator(Function getter) { + super(getter); + } + + @Override + protected ObjectValidator returnThis() { + return this; + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java new file mode 100644 index 0000000..d1d8ec1 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/validator/PropertyValidator.java @@ -0,0 +1,129 @@ +package xyz.zhouxy.plusone.validator; + +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 returnThis(); + } + + // ====== 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 returnThis(); + } + + // ===== equals ===== + + public THIS equalsThat(Object that) { + return equalsThat(that, value -> new InvalidInputException(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 returnThis(); + } + + // ===== state ===== + + public THIS state(Predicate condition) { + return state(condition, "无效的用户输入"); + } + + public THIS state(Predicate condition, String errMsg) { + return state(condition, convertExceptionCreator(errMsg)); + } + + public THIS state( + Predicate condition, + Supplier exceptionCreator) { + return state(condition, convertExceptionCreator(exceptionCreator)); + } + + public THIS state( + Predicate condition, + Function exceptionCreator) { + withRule(condition, exceptionCreator); + return returnThis(); + } + + // ======================================================================== + + void validate(DTO obj) { + PROPERTY value = this.getter.apply(obj); + this.validator.validate(value); + } + + static Function convertExceptionCreator(String errMsg) { + return value -> new InvalidInputException(errMsg); + } + + static Function convertExceptionCreator( + Supplier exceptionSupplier) { + return value -> exceptionSupplier.get(); + } + + protected abstract THIS returnThis(); +} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java new file mode 100644 index 0000000..dec88dc --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/validator/StringValidator.java @@ -0,0 +1,179 @@ +package xyz.zhouxy.plusone.validator; + +import java.util.List; +import java.util.function.Function; +import java.util.function.Supplier; + +import cn.hutool.core.util.StrUtil; +import xyz.zhouxy.plusone.constant.RegexConsts; +import xyz.zhouxy.plusone.util.RegexUtil; + +class StringValidator extends PropertyValidator> { + + StringValidator(Function getter) { + super(getter); + } + + // ==================== + // ====== String ====== + // ==================== + + // ===== matches ===== + + public StringValidator matches(String regex, String errMsg) { + return matches(regex, convertExceptionCreator(errMsg)); + } + + public StringValidator matches( + String regex, + Supplier exceptionCreator) { + return matches(regex, convertExceptionCreator(exceptionCreator)); + } + + public StringValidator matches( + String regex, + Function exceptionCreator) { + withRule(input -> RegexUtil.matches(input, regex), exceptionCreator); + return this; + } + + // ===== matchesOr ===== + + public StringValidator matchesOr(String[] regexs, String errMsg) { + return matchesOr(regexs, convertExceptionCreator(errMsg)); + } + + public StringValidator matchesOr( + String[] regexs, + Supplier exceptionCreator) { + return matchesOr(regexs, convertExceptionCreator(exceptionCreator)); + } + + public StringValidator matchesOr( + String[] regexs, + Function exceptionCreator) { + withRule(input -> RegexUtil.matchesOr(input, regexs), exceptionCreator); + return this; + } + + public StringValidator matchesOr(List regexs, String errMsg) { + return matchesOr(regexs, convertExceptionCreator(errMsg)); + } + + public StringValidator matchesOr( + List regexs, + Supplier exceptionCreator) { + return matchesOr(regexs, convertExceptionCreator(exceptionCreator)); + } + + public StringValidator matchesOr( + List regexs, + Function exceptionCreator) { + withRule(input -> RegexUtil.matchesOr(input, regexs.toArray(new String[regexs.size()])), exceptionCreator); + return this; + } + + // ===== matchesAnd ===== + + public StringValidator matchesAnd(String[] regexs, String errMsg) { + return matchesAnd(regexs, convertExceptionCreator(errMsg)); + } + + public StringValidator matchesAnd( + String[] regexs, + Supplier exceptionCreator) { + return matchesAnd(regexs, convertExceptionCreator(exceptionCreator)); + } + + public StringValidator matchesAnd( + String[] regexs, + Function exceptionCreator) { + withRule(input -> RegexUtil.matchesAnd(input, regexs), exceptionCreator); + return this; + } + + // ===== notBlank ===== + + public StringValidator notBlank() { + return notBlank("This String argument must have text; it must not be null, empty, or blank"); + } + + public StringValidator notBlank(String errMsg) { + return notBlank(convertExceptionCreator(errMsg)); + } + + public StringValidator notBlank(Supplier exceptionCreator) { + return notBlank(convertExceptionCreator(exceptionCreator)); + } + + public StringValidator notBlank( + Function exceptionCreator) { + withRule(input -> StrUtil.isNotBlank(input), exceptionCreator); + return this; + } + + // ===== email ===== + + public StringValidator email() { + return email("The value is not an email address."); + } + + public StringValidator email(String errMsg) { + return email(convertExceptionCreator(errMsg)); + } + + public StringValidator email(Supplier exceptionCreator) { + return email(convertExceptionCreator(exceptionCreator)); + } + + public StringValidator email(Function exceptionCreator) { + return matches(RegexConsts.EMAIL, exceptionCreator); + } + + // ====== notEmpty ===== + + public StringValidator notEmpty(String errMsg) { + return notEmpty(convertExceptionCreator(errMsg)); + } + + public StringValidator notEmpty(Supplier exceptionCreator) { + return notEmpty(convertExceptionCreator(exceptionCreator)); + } + + public StringValidator notEmpty( + Function exceptionCreator) { + withRule(value -> { + if (value == null) { + return false; + } + return !(value.isEmpty()); + }, exceptionCreator); + return this; + } + + // ====== isEmpty ===== + + public StringValidator isEmpty(String errMsg) { + return isEmpty(convertExceptionCreator(errMsg)); + } + + public StringValidator isEmpty(Supplier exceptionCreator) { + return isEmpty(convertExceptionCreator(exceptionCreator)); + } + + public StringValidator isEmpty( + Function exceptionCreator) { + withRule(value -> { + if (value == null) { + return false; + } + return value.isEmpty(); + }, exceptionCreator); + return this; + } + + @Override + protected StringValidator returnThis() { + return this; + } +} diff --git a/src/main/java/xyz/zhouxy/plusone/validator/ValidateUtil.java b/src/main/java/xyz/zhouxy/plusone/validator/ValidateUtil.java index 438b197..1a8438d 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/ValidateUtil.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/ValidateUtil.java @@ -17,6 +17,12 @@ public class ValidateUtil { throw new IllegalStateException("Utility class"); } + public static void validate(Object obj) { + if (obj instanceof IValidateRequired) { + ((IValidateRequired) obj).validate(); + } + } + 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 index b56ff50..bbe12ad 100644 --- a/src/main/java/xyz/zhouxy/plusone/validator/Validator.java +++ b/src/main/java/xyz/zhouxy/plusone/validator/Validator.java @@ -1,6 +1,9 @@ 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; /** * 校验器 @@ -39,4 +42,19 @@ public final class Validator extends BaseValidator { 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/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 4f48b5b..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/BaseValidatorTest.java b/src/test/java/xyz/zhouxy/plusone/validator/BaseValidatorTest.java new file mode 100644 index 0000000..d3c95d1 --- /dev/null +++ b/src/test/java/xyz/zhouxy/plusone/validator/BaseValidatorTest.java @@ -0,0 +1,134 @@ +package xyz.zhouxy.plusone.validator; + +import java.util.Arrays; +import java.util.List; +import java.util.Objects; + +import org.junit.Test; + +import xyz.zhouxy.plusone.constant.RegexConsts; + +public class BaseValidatorTest { + @Test + public void testRuleFor() { + RegisterCommand registerCommand = new RegisterCommand("me", "luquanlion@outlook.com", "22336", "A1b2C3d4", + "A1b2C3d4", + Arrays.asList(new String[] { "admin", "editor" })); + registerCommand.validate(); + System.out.println(registerCommand); + } +} + +class RegisterCommandValidator extends BaseValidator { + + static final RegisterCommandValidator INSTANCE = new RegisterCommandValidator(); + + private RegisterCommandValidator() { + ruleForString(RegisterCommand::getUsername) + .notNull("用户名不能为空") + .matches(RegexConsts.USERNAME, + username -> new IllegalArgumentException(String.format("用户名\"%s\"不符合规范", username))); + ruleForString(RegisterCommand::getAccount) + .notNull("请输入邮箱地址或手机号") + .matchesOr(new String[] { RegexConsts.EMAIL, RegexConsts.MOBILE_PHONE }, "请输入邮箱地址或手机号"); + ruleForString(RegisterCommand::getCode) + .notNull("验证码不能为空") + .matches(RegexConsts.CAPTCHA, "验证码不符合规范"); + ruleForString(RegisterCommand::getPassword) + .notEmpty("密码不能为空") + .matches(RegexConsts.PASSWORD, "密码不符合规范"); + ruleForCollection(RegisterCommand::getRoles) + .notEmpty(() -> new RuntimeException("角色列表不能为空")); + + withRule(registerCommand -> Objects.equals(registerCommand.getPassword(), registerCommand.getPassword2()), + "两次输入的密码不一致"); + } +} + +/** + * RegisterCommand + */ +class RegisterCommand implements IValidateRequired { + + private String username; + private String account; + private String code; + private String password; + private String password2; + private List roles; + + @Override + public void validate() { + RegisterCommandValidator.INSTANCE.validate(this); + } + + 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(); + } +}