From 245c2e962cf1ff487f6219a7af4486660bee60be Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 27 May 2025 23:55:10 +0800 Subject: [PATCH] temp --- plusone-validator/pom.xml | 2 +- .../BaseComparablePropertyValidator.java | 12 ++- .../validator/BasePropertyValidator.java | 58 ++++++++++---- .../validator/BoolPropertyValidator.java | 26 +++--- .../CollectionPropertyValidator.java | 34 +++++--- .../ComparablePropertyValidator.java | 8 +- .../validator/DoublePropertyValidator.java | 48 +++++------ .../validator/IntPropertyValidator.java | 48 +++++------ .../validator/LongPropertyValidator.java | 48 +++++------ .../validator/ObjectPropertyValidator.java | 7 +- .../validator/StringPropertyValidator.java | 80 +++++++++---------- .../ObjectPropertyValidatorTests.java | 1 - pom.xml | 9 ++- 13 files changed, 210 insertions(+), 171 deletions(-) diff --git a/plusone-validator/pom.xml b/plusone-validator/pom.xml index 6fcfa08..8ce1f6a 100644 --- a/plusone-validator/pom.xml +++ b/plusone-validator/pom.xml @@ -16,7 +16,7 @@ http://zhouxy.xyz - Plusone Validator 是一个参数校验框架,可针对 DTO 创建对应的校验器,并复用该校验器实例,对 DTO 进行校验。 + Plusone Validator 是一个校验库,用于构建校验规则对对象(尤其是入参)进行校验。API 参考自 .NET 的 FluentValidation。 diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseComparablePropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseComparablePropertyValidator.java index a6c90ba..52ba86f 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseComparablePropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BaseComparablePropertyValidator.java @@ -21,19 +21,17 @@ import java.util.function.Supplier; import com.google.common.collect.Range; -public abstract -class BaseComparablePropertyValidator, - TPropertyValidator extends BaseComparablePropertyValidator> - extends BasePropertyValidator { +public abstract class BaseComparablePropertyValidator, TPropertyValidator extends BaseComparablePropertyValidator> + extends BasePropertyValidator { - BaseComparablePropertyValidator(Function getter) { + BaseComparablePropertyValidator(Function getter) { super(getter); } public TPropertyValidator inRange(Range range) { withRule(value -> value != null && range.contains(value), - convertExceptionCreator("The value is not in the interval " + range.toString())); + value -> new IllegalArgumentException( + String.format("The input must in the interval %s. You entered %s.", range, value))); return thisObject(); } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java index 47e8621..8350810 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BasePropertyValidator.java @@ -25,16 +25,13 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; -public abstract class BasePropertyValidator< // - TObj, // - TProperty, // - TPropertyValidator extends BasePropertyValidator> { +public abstract class BasePropertyValidator> { - private final Function getter; + private final Function getter; private final List> consumers = new LinkedList<>(); - protected BasePropertyValidator(Function getter) { + protected BasePropertyValidator(Function getter) { this.getter = getter; } @@ -62,7 +59,7 @@ public abstract class BasePropertyValidator< // return thisObject(); } - public final void validate(T obj) { + public final void validate(T obj) { for (Consumer consumer : consumers) { consumer.accept(getter.apply(obj)); } @@ -74,10 +71,12 @@ public abstract class BasePropertyValidator< // // ====== Object ====== // ==================== - // ====== notNull ===== + // ================================ + // #region - notNull + // ================================ public TPropertyValidator notNull() { - return notNull("Value could not be null."); + return notNull("The input must not be null."); } public TPropertyValidator notNull(String errMsg) { @@ -93,7 +92,17 @@ public abstract class BasePropertyValidator< // return thisObject(); } - // ====== isNull ===== + // ================================ + // #endregion - notNull + // ================================ + + // ================================ + // #region - isNull + // ================================ + + public TPropertyValidator isNull() { + return isNull("The input must be null."); + } public TPropertyValidator isNull(String errMsg) { return isNull(convertExceptionCreator(errMsg)); @@ -108,10 +117,17 @@ public abstract class BasePropertyValidator< // return thisObject(); } - // ===== equals ===== + // ================================ + // #endregion - isNull + // ================================ + + // ================================ + // #region - equals + // ================================ public TPropertyValidator equalsThat(Object that) { - return equalsThat(that, value -> new IllegalArgumentException(String.format("(%s) 必须与 (%s) 相等", value, that))); + return equalsThat(that, + value -> new IllegalArgumentException(String.format("The input must be equal to '%s'.", that))); } public TPropertyValidator equalsThat(Object that, String errMsg) { @@ -129,10 +145,16 @@ public abstract class BasePropertyValidator< // return thisObject(); } - // ===== must ===== + // ================================ + // #endregion - equals + // ================================ + + // ================================ + // #region - must + // ================================ public TPropertyValidator must(Predicate condition) { - return must(condition, "无效的用户输入"); + return must(condition, "The specified condition was not met for the input."); } public TPropertyValidator must(Predicate condition, String errMsg) { @@ -152,10 +174,8 @@ public abstract class BasePropertyValidator< // return thisObject(); } - // ===== must ===== - public TPropertyValidator must(Collection> conditions) { - return must(conditions, "无效的用户输入"); + return must(conditions, "The specified conditions were not met for the input."); } public TPropertyValidator must(Collection> conditions, String errMsg) { @@ -177,6 +197,10 @@ public abstract class BasePropertyValidator< // return thisObject(); } + // ================================ + // #endregion - must + // ================================ + static Function convertExceptionCreator(String errMsg) { return value -> new IllegalArgumentException(errMsg); } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BoolPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BoolPropertyValidator.java index da807e3..2357381 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BoolPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/BoolPropertyValidator.java @@ -19,28 +19,28 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; import java.util.function.Supplier; -public class BoolPropertyValidator extends BasePropertyValidator> { +public class BoolPropertyValidator extends BasePropertyValidator> { - BoolPropertyValidator(Function getter) { + BoolPropertyValidator(Function getter) { super(getter); } // ====== isTrueValue ====== - public BoolPropertyValidator isTrueValue() { - return isTrueValue("The value must be true."); + public BoolPropertyValidator isTrueValue() { + return isTrueValue("The input must be true."); } - public BoolPropertyValidator isTrueValue(String errMsg) { + public BoolPropertyValidator isTrueValue(String errMsg) { return isTrueValue(convertExceptionCreator(errMsg)); } - public BoolPropertyValidator isTrueValue( + public BoolPropertyValidator isTrueValue( Supplier exceptionCreator) { return isTrueValue(convertExceptionCreator(exceptionCreator)); } - public BoolPropertyValidator isTrueValue( + public BoolPropertyValidator isTrueValue( Function exceptionCreator) { withRule(Boolean.TRUE::equals, exceptionCreator); return this; @@ -48,27 +48,27 @@ public class BoolPropertyValidator extends BasePropertyValidator isFalseValue() { - return isFalseValue("The value must be false."); + public BoolPropertyValidator isFalseValue() { + return isFalseValue("The input must be false."); } - public BoolPropertyValidator isFalseValue(String errMsg) { + public BoolPropertyValidator isFalseValue(String errMsg) { return isFalseValue(convertExceptionCreator(errMsg)); } - public BoolPropertyValidator isFalseValue( + public BoolPropertyValidator isFalseValue( Supplier exceptionCreator) { return isFalseValue(convertExceptionCreator(exceptionCreator)); } - public BoolPropertyValidator isFalseValue( + public BoolPropertyValidator isFalseValue( Function exceptionCreator) { withRule(Boolean.FALSE::equals, exceptionCreator); return this; } @Override - protected BoolPropertyValidator thisObject() { + protected BoolPropertyValidator thisObject() { return this; } } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/CollectionPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/CollectionPropertyValidator.java index 1379e32..8b657e4 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/CollectionPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/CollectionPropertyValidator.java @@ -22,47 +22,57 @@ import java.util.function.Supplier; import xyz.zhouxy.plusone.commons.collection.CollectionTools; -public class CollectionPropertyValidator - extends BasePropertyValidator, CollectionPropertyValidator> { +public class CollectionPropertyValidator + extends BasePropertyValidator, CollectionPropertyValidator> { - CollectionPropertyValidator(Function> getter) { + CollectionPropertyValidator(Function> getter) { super(getter); } // ====== notEmpty ===== - public CollectionPropertyValidator notEmpty(String errMsg) { + public CollectionPropertyValidator notEmpty() { + return notEmpty("The input must not be empty."); + } + + public CollectionPropertyValidator notEmpty(String errMsg) { return notEmpty(convertExceptionCreator(errMsg)); } - public CollectionPropertyValidator notEmpty(Supplier exceptionCreator) { + public CollectionPropertyValidator notEmpty( + Supplier exceptionCreator) { return notEmpty(convertExceptionCreator(exceptionCreator)); } - public CollectionPropertyValidator notEmpty( - Function, E> exceptionCreator) { + public CollectionPropertyValidator notEmpty( + Function, E> exceptionCreator) { withRule(CollectionTools::isNotEmpty, exceptionCreator); return this; } // ====== isEmpty ===== - public CollectionPropertyValidator isEmpty(String errMsg) { + public CollectionPropertyValidator isEmpty() { + return isEmpty("The input must be empty."); + } + + public CollectionPropertyValidator isEmpty(String errMsg) { return isEmpty(convertExceptionCreator(errMsg)); } - public CollectionPropertyValidator isEmpty(Supplier exceptionCreator) { + public CollectionPropertyValidator isEmpty( + Supplier exceptionCreator) { return isEmpty(convertExceptionCreator(exceptionCreator)); } - public CollectionPropertyValidator isEmpty( - Function, E> exceptionCreator) { + public CollectionPropertyValidator isEmpty( + Function, E> exceptionCreator) { withRule(CollectionTools::isEmpty, exceptionCreator); return this; } @Override - protected CollectionPropertyValidator thisObject() { + protected CollectionPropertyValidator thisObject() { return this; } } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ComparablePropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ComparablePropertyValidator.java index 22b6e70..3fc8820 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ComparablePropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ComparablePropertyValidator.java @@ -18,15 +18,15 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; -public class ComparablePropertyValidator> - extends BaseComparablePropertyValidator> { +public class ComparablePropertyValidator> + extends BaseComparablePropertyValidator> { - ComparablePropertyValidator(Function getter) { + ComparablePropertyValidator(Function getter) { super(getter); } @Override - protected ComparablePropertyValidator thisObject() { + protected ComparablePropertyValidator thisObject() { return this; } } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/DoublePropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/DoublePropertyValidator.java index d8fd23c..646993d 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/DoublePropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/DoublePropertyValidator.java @@ -19,10 +19,10 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; import java.util.function.Supplier; -public class DoublePropertyValidator - extends BaseComparablePropertyValidator> { +public class DoublePropertyValidator + extends BaseComparablePropertyValidator> { - DoublePropertyValidator(Function getter) { + DoublePropertyValidator(Function getter) { super(getter); } @@ -30,20 +30,20 @@ public class DoublePropertyValidator // #region - greater than // ================================ - public DoublePropertyValidator gt(double min) { - return gt(min, String.format("The value should be greater than %s", min)); + public DoublePropertyValidator gt(double min) { + return gt(min, String.format("The input must be greater than '%s'.", min)); } - public DoublePropertyValidator gt(double min, String errMsg) { + public DoublePropertyValidator gt(double min, String errMsg) { return gt(min, convertExceptionCreator(errMsg)); } - public DoublePropertyValidator gt( + public DoublePropertyValidator gt( double min, Supplier exceptionCreator) { return gt(min, convertExceptionCreator(exceptionCreator)); } - public DoublePropertyValidator gt( + public DoublePropertyValidator gt( double min, Function exceptionCreator) { withRule(value -> (value != null && value > min), exceptionCreator); return this; @@ -57,20 +57,20 @@ public class DoublePropertyValidator // #region - greater than or equal to // ================================ - public DoublePropertyValidator ge(double min) { - return ge(min, String.format("The value should be greater than or equal to %s", min)); + public DoublePropertyValidator ge(double min) { + return ge(min, String.format("The input must be greater than or equal to '%s'.", min)); } - public DoublePropertyValidator ge(double min, String errMsg) { + public DoublePropertyValidator ge(double min, String errMsg) { return ge(min, convertExceptionCreator(errMsg)); } - public DoublePropertyValidator ge( + public DoublePropertyValidator ge( double min, Supplier exceptionCreator) { return ge(min, convertExceptionCreator(exceptionCreator)); } - public DoublePropertyValidator ge( + public DoublePropertyValidator ge( double min, Function exceptionCreator) { withRule(value -> (value != null && value >= min), exceptionCreator); return this; @@ -84,20 +84,20 @@ public class DoublePropertyValidator // #region - less than // ================================ - public DoublePropertyValidator lt(double max) { - return lt(max, String.format("The value should be less than %s", max)); + public DoublePropertyValidator lt(double max) { + return lt(max, String.format("The input must be less than '%s'.", max)); } - public DoublePropertyValidator lt(double max, String errMsg) { + public DoublePropertyValidator lt(double max, String errMsg) { return lt(max, convertExceptionCreator(errMsg)); } - public DoublePropertyValidator lt( + public DoublePropertyValidator lt( double max, Supplier exceptionCreator) { return lt(max, convertExceptionCreator(exceptionCreator)); } - public DoublePropertyValidator lt( + public DoublePropertyValidator lt( double max, Function exceptionCreator) { withRule(value -> (value != null && value < max), exceptionCreator); return this; @@ -111,20 +111,20 @@ public class DoublePropertyValidator // #region - less than or equal to // ================================ - public DoublePropertyValidator le(double max) { - return le(max, String.format("The value should be less than or equal to %s", max)); + public DoublePropertyValidator le(double max) { + return le(max, String.format("The input must be less than or equal to '%s'.", max)); } - public DoublePropertyValidator le(double max, String errMsg) { + public DoublePropertyValidator le(double max, String errMsg) { return le(max, convertExceptionCreator(errMsg)); } - public DoublePropertyValidator le( + public DoublePropertyValidator le( double max, Supplier exceptionCreator) { return le(max, convertExceptionCreator(exceptionCreator)); } - public DoublePropertyValidator le( + public DoublePropertyValidator le( double max, Function exceptionCreator) { withRule(value -> (value != null && value <= max), exceptionCreator); return this; @@ -135,7 +135,7 @@ public class DoublePropertyValidator // ================================ @Override - protected DoublePropertyValidator thisObject() { + protected DoublePropertyValidator thisObject() { return this; } } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/IntPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/IntPropertyValidator.java index 4b0d078..431a77e 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/IntPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/IntPropertyValidator.java @@ -19,10 +19,10 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; import java.util.function.Supplier; -public class IntPropertyValidator - extends BaseComparablePropertyValidator> { +public class IntPropertyValidator + extends BaseComparablePropertyValidator> { - IntPropertyValidator(Function getter) { + IntPropertyValidator(Function getter) { super(getter); } @@ -30,20 +30,20 @@ public class IntPropertyValidator // #region - greater than // ================================ - public IntPropertyValidator gt(int min) { - return gt(min, String.format("The value should be greater than %d", min)); + public IntPropertyValidator gt(int min) { + return gt(min, String.format("The input must be greater than '%d'.", min)); } - public IntPropertyValidator gt(int min, String errMsg) { + public IntPropertyValidator gt(int min, String errMsg) { return gt(min, convertExceptionCreator(errMsg)); } - public IntPropertyValidator gt( + public IntPropertyValidator gt( int min, Supplier exceptionCreator) { return gt(min, convertExceptionCreator(exceptionCreator)); } - public IntPropertyValidator gt( + public IntPropertyValidator gt( int min, Function exceptionCreator) { withRule(value -> (value != null && value > min), exceptionCreator); return this; @@ -57,20 +57,20 @@ public class IntPropertyValidator // #region - greater than or equal to // ================================ - public IntPropertyValidator ge(int min) { - return ge(min, String.format("The value should be greater than or equal to %d", min)); + public IntPropertyValidator ge(int min) { + return ge(min, String.format("The input must be greater than or equal to '%d'.", min)); } - public IntPropertyValidator ge(int min, String errMsg) { + public IntPropertyValidator ge(int min, String errMsg) { return ge(min, convertExceptionCreator(errMsg)); } - public IntPropertyValidator ge( + public IntPropertyValidator ge( int min, Supplier exceptionCreator) { return ge(min, convertExceptionCreator(exceptionCreator)); } - public IntPropertyValidator ge( + public IntPropertyValidator ge( int min, Function exceptionCreator) { withRule(value -> (value != null && value >= min), exceptionCreator); return this; @@ -84,20 +84,20 @@ public class IntPropertyValidator // #region - less than // ================================ - public IntPropertyValidator lt(int max) { - return lt(max, String.format("The value should be less than %d", max)); + public IntPropertyValidator lt(int max) { + return lt(max, String.format("The input must be less than '%d'.", max)); } - public IntPropertyValidator lt(int max, String errMsg) { + public IntPropertyValidator lt(int max, String errMsg) { return lt(max, convertExceptionCreator(errMsg)); } - public IntPropertyValidator lt( + public IntPropertyValidator lt( int max, Supplier exceptionCreator) { return lt(max, convertExceptionCreator(exceptionCreator)); } - public IntPropertyValidator lt( + public IntPropertyValidator lt( int max, Function exceptionCreator) { withRule(value -> (value != null && value < max), exceptionCreator); return this; @@ -111,20 +111,20 @@ public class IntPropertyValidator // #region - less than or equal to // ================================ - public IntPropertyValidator le(int max) { - return le(max, String.format("The value should be less than or equal to %d", max)); + public IntPropertyValidator le(int max) { + return le(max, String.format("The input must be less than or equal to '%d'.", max)); } - public IntPropertyValidator le(int max, String errMsg) { + public IntPropertyValidator le(int max, String errMsg) { return le(max, convertExceptionCreator(errMsg)); } - public IntPropertyValidator le( + public IntPropertyValidator le( int max, Supplier exceptionCreator) { return le(max, convertExceptionCreator(exceptionCreator)); } - public IntPropertyValidator le( + public IntPropertyValidator le( int max, Function exceptionCreator) { withRule(value -> (value != null && value <= max), exceptionCreator); return this; @@ -135,7 +135,7 @@ public class IntPropertyValidator // ================================ @Override - protected IntPropertyValidator thisObject() { + protected IntPropertyValidator thisObject() { return this; } } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/LongPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/LongPropertyValidator.java index 9c77bbd..776093b 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/LongPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/LongPropertyValidator.java @@ -19,10 +19,10 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; import java.util.function.Supplier; -public class LongPropertyValidator - extends BaseComparablePropertyValidator> { +public class LongPropertyValidator + extends BaseComparablePropertyValidator> { - LongPropertyValidator(Function getter) { + LongPropertyValidator(Function getter) { super(getter); } @@ -30,20 +30,20 @@ public class LongPropertyValidator // #region - greater than // ================================ - public LongPropertyValidator gt(long min) { - return gt(min, String.format("The value should be greater than %d", min)); + public LongPropertyValidator gt(long min) { + return gt(min, String.format("The input must be greater than '%d'.", min)); } - public LongPropertyValidator gt(long min, String errMsg) { + public LongPropertyValidator gt(long min, String errMsg) { return gt(min, convertExceptionCreator(errMsg)); } - public LongPropertyValidator gt( + public LongPropertyValidator gt( long min, Supplier exceptionCreator) { return gt(min, convertExceptionCreator(exceptionCreator)); } - public LongPropertyValidator gt( + public LongPropertyValidator gt( long min, Function exceptionCreator) { withRule(value -> (value != null && value > min), exceptionCreator); return this; @@ -57,20 +57,20 @@ public class LongPropertyValidator // #region - greater than or equal to // ================================ - public LongPropertyValidator ge(long min) { - return ge(min, String.format("The value should be greater than or equal to %d", min)); + public LongPropertyValidator ge(long min) { + return ge(min, String.format("The input must be greater than or equal to '%d'.", min)); } - public LongPropertyValidator ge(long min, String errMsg) { + public LongPropertyValidator ge(long min, String errMsg) { return ge(min, convertExceptionCreator(errMsg)); } - public LongPropertyValidator ge( + public LongPropertyValidator ge( long min, Supplier exceptionCreator) { return ge(min, convertExceptionCreator(exceptionCreator)); } - public LongPropertyValidator ge( + public LongPropertyValidator ge( long min, Function exceptionCreator) { withRule(value -> (value != null && value >= min), exceptionCreator); return this; @@ -84,20 +84,20 @@ public class LongPropertyValidator // #region - less than // ================================ - public LongPropertyValidator lt(long max) { - return lt(max, String.format("The value should be less than %d", max)); + public LongPropertyValidator lt(long max) { + return lt(max, String.format("The input must be less than '%d'.", max)); } - public LongPropertyValidator lt(long max, String errMsg) { + public LongPropertyValidator lt(long max, String errMsg) { return lt(max, convertExceptionCreator(errMsg)); } - public LongPropertyValidator lt( + public LongPropertyValidator lt( long max, Supplier exceptionCreator) { return lt(max, convertExceptionCreator(exceptionCreator)); } - public LongPropertyValidator lt( + public LongPropertyValidator lt( long max, Function exceptionCreator) { withRule(value -> (value != null && value < max), exceptionCreator); return this; @@ -111,20 +111,20 @@ public class LongPropertyValidator // #region - less than or equal to // ================================ - public LongPropertyValidator le(long max) { - return le(max, String.format("The value should be less than or equal to %d", max)); + public LongPropertyValidator le(long max) { + return le(max, String.format("The input must be less than or equal to '%d'.", max)); } - public LongPropertyValidator le(long max, String errMsg) { + public LongPropertyValidator le(long max, String errMsg) { return le(max, convertExceptionCreator(errMsg)); } - public LongPropertyValidator le( + public LongPropertyValidator le( long max, Supplier exceptionCreator) { return le(max, convertExceptionCreator(exceptionCreator)); } - public LongPropertyValidator le( + public LongPropertyValidator le( long max, Function exceptionCreator) { withRule(value -> (value != null && value <= max), exceptionCreator); return this; @@ -135,7 +135,7 @@ public class LongPropertyValidator // ================================ @Override - protected LongPropertyValidator thisObject() { + protected LongPropertyValidator thisObject() { return this; } } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ObjectPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ObjectPropertyValidator.java index 24318b3..29b36c4 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ObjectPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ObjectPropertyValidator.java @@ -18,14 +18,15 @@ package xyz.zhouxy.plusone.validator; import java.util.function.Function; -public class ObjectPropertyValidator extends BasePropertyValidator> { +public class ObjectPropertyValidator + extends BasePropertyValidator> { - ObjectPropertyValidator(Function getter) { + ObjectPropertyValidator(Function getter) { super(getter); } @Override - protected ObjectPropertyValidator thisObject() { + protected ObjectPropertyValidator thisObject() { return this; } } diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/StringPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/StringPropertyValidator.java index 6867dec..eaf35b5 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/StringPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/StringPropertyValidator.java @@ -36,9 +36,9 @@ import xyz.zhouxy.plusone.commons.util.StringTools; * * @author ZhouXY */ -public class StringPropertyValidator extends BaseComparablePropertyValidator> { +public class StringPropertyValidator extends BaseComparablePropertyValidator> { - StringPropertyValidator(Function getter) { + StringPropertyValidator(Function getter) { super(getter); } @@ -46,17 +46,17 @@ public class StringPropertyValidator extends BaseComparablePropertyValidato // #region - matches // ================================ - public StringPropertyValidator matches(Pattern regex, String errMsg) { + public StringPropertyValidator matches(Pattern regex, String errMsg) { return matches(regex, convertExceptionCreator(errMsg)); } - public StringPropertyValidator matches( + public StringPropertyValidator matches( Pattern regex, Supplier exceptionCreator) { return matches(regex, convertExceptionCreator(exceptionCreator)); } - public StringPropertyValidator matches( + public StringPropertyValidator matches( Pattern regex, Function exceptionCreator) { withRule(input -> (input == null || RegexTools.matches(input, regex)), exceptionCreator); @@ -71,34 +71,34 @@ public class StringPropertyValidator extends BaseComparablePropertyValidato // #region - matchesOne // ================================ - public StringPropertyValidator matchesOne(Pattern[] regexs, String errMsg) { + public StringPropertyValidator matchesOne(Pattern[] regexs, String errMsg) { return matchesOne(regexs, convertExceptionCreator(errMsg)); } - public StringPropertyValidator matchesOne( + public StringPropertyValidator matchesOne( Pattern[] regexs, Supplier exceptionCreator) { return matchesOne(regexs, convertExceptionCreator(exceptionCreator)); } - public StringPropertyValidator matchesOne( + public StringPropertyValidator matchesOne( Pattern[] regexs, Function exceptionCreator) { withRule(input -> RegexTools.matchesOne(input, regexs), exceptionCreator); return this; } - public StringPropertyValidator matchesOne(List regexs, String errMsg) { + public StringPropertyValidator matchesOne(List regexs, String errMsg) { return matchesOne(regexs, convertExceptionCreator(errMsg)); } - public StringPropertyValidator matchesOne( + public StringPropertyValidator matchesOne( List regexs, Supplier exceptionCreator) { return matchesOne(regexs, convertExceptionCreator(exceptionCreator)); } - public StringPropertyValidator matchesOne( + public StringPropertyValidator matchesOne( List regexs, Function exceptionCreator) { withRule(input -> input == null || RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); @@ -113,34 +113,34 @@ public class StringPropertyValidator extends BaseComparablePropertyValidato // #region - matchesAll // ================================ - public StringPropertyValidator matchesAll(Pattern[] regexs, String errMsg) { + public StringPropertyValidator matchesAll(Pattern[] regexs, String errMsg) { return matchesAll(regexs, convertExceptionCreator(errMsg)); } - public StringPropertyValidator matchesAll( + public StringPropertyValidator matchesAll( Pattern[] regexs, Supplier exceptionCreator) { return matchesAll(regexs, convertExceptionCreator(exceptionCreator)); } - public StringPropertyValidator matchesAll( + public StringPropertyValidator matchesAll( Pattern[] regexs, Function exceptionCreator) { withRule(input -> RegexTools.matchesAll(input, regexs), exceptionCreator); return this; } - public StringPropertyValidator matchesAll(Collection regexs, String errMsg) { + public StringPropertyValidator matchesAll(Collection regexs, String errMsg) { return matchesAll(regexs, convertExceptionCreator(errMsg)); } - public StringPropertyValidator matchesAll( + public StringPropertyValidator matchesAll( Collection regexs, Supplier exceptionCreator) { return matchesAll(regexs, convertExceptionCreator(exceptionCreator)); } - public StringPropertyValidator matchesAll( + public StringPropertyValidator matchesAll( Collection regexs, Function exceptionCreator) { withRule(input -> input == null || RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); @@ -155,19 +155,19 @@ public class StringPropertyValidator extends BaseComparablePropertyValidato // #region - notBlank // ================================ - public StringPropertyValidator notBlank() { - return notBlank("The value must have text; it must not be null, empty, or blank."); + public StringPropertyValidator notBlank() { + return notBlank("The input must not be blank."); } - public StringPropertyValidator notBlank(String errMsg) { + public StringPropertyValidator notBlank(String errMsg) { return notBlank(convertExceptionCreator(errMsg)); } - public StringPropertyValidator notBlank(Supplier exceptionCreator) { + public StringPropertyValidator notBlank(Supplier exceptionCreator) { return notBlank(convertExceptionCreator(exceptionCreator)); } - public StringPropertyValidator notBlank( + public StringPropertyValidator notBlank( Function exceptionCreator) { withRule(StringTools::isNotBlank, exceptionCreator); return this; @@ -181,20 +181,20 @@ public class StringPropertyValidator extends BaseComparablePropertyValidato // #region - emailAddress // ================================ - public StringPropertyValidator emailAddress() { - return emailAddress("The value is not an email address."); + public StringPropertyValidator emailAddress() { + return emailAddress("The input is not a valid email address."); } - public StringPropertyValidator emailAddress(String errMsg) { + public StringPropertyValidator emailAddress(String errMsg) { return emailAddress(convertExceptionCreator(errMsg)); } - public StringPropertyValidator emailAddress( + public StringPropertyValidator emailAddress( Supplier exceptionCreator) { return emailAddress(convertExceptionCreator(exceptionCreator)); } - public StringPropertyValidator emailAddress( + public StringPropertyValidator emailAddress( Function exceptionCreator) { return matches(PatternConsts.EMAIL, exceptionCreator); } @@ -207,19 +207,19 @@ public class StringPropertyValidator extends BaseComparablePropertyValidato // #region - notEmpty // ================================ - public StringPropertyValidator notEmpty() { - return notEmpty("The value must not be empty."); + public StringPropertyValidator notEmpty() { + return notEmpty("The input must not be empty."); } - public StringPropertyValidator notEmpty(String errMsg) { + public StringPropertyValidator notEmpty(String errMsg) { return notEmpty(convertExceptionCreator(errMsg)); } - public StringPropertyValidator notEmpty(Supplier exceptionCreator) { + public StringPropertyValidator notEmpty(Supplier exceptionCreator) { return notEmpty(convertExceptionCreator(exceptionCreator)); } - public StringPropertyValidator notEmpty( + public StringPropertyValidator notEmpty( Function exceptionCreator) { withRule(s -> s != null && !s.isEmpty(), exceptionCreator); return this; @@ -233,19 +233,19 @@ public class StringPropertyValidator extends BaseComparablePropertyValidato // #region - length // ================================ - public StringPropertyValidator length(int length, String errMsg) { + public StringPropertyValidator length(int length, String errMsg) { return length(length, convertExceptionCreator(errMsg)); } - public StringPropertyValidator length(int length, + public StringPropertyValidator length(int length, Supplier exceptionCreator) { return length(length, convertExceptionCreator(exceptionCreator)); } - public StringPropertyValidator length(int length, + public StringPropertyValidator length(int length, Function exceptionCreator) { AssertTools.checkArgument(length >= 0, - "The required length must be greater than or equal to 0."); + "The expected length must be greater than or equal to 0."); withRule(s -> s == null || s.length() == length, exceptionCreator); return this; } @@ -258,16 +258,16 @@ public class StringPropertyValidator extends BaseComparablePropertyValidato return len >= min && len <= max; } - public StringPropertyValidator length(int min, int max, String errMsg) { + public StringPropertyValidator length(int min, int max, String errMsg) { return length(min, max, convertExceptionCreator(errMsg)); } - public StringPropertyValidator length(int min, int max, + public StringPropertyValidator length(int min, int max, Supplier exceptionCreator) { return length(min, max, convertExceptionCreator(exceptionCreator)); } - public StringPropertyValidator length(int min, int max, + public StringPropertyValidator length(int min, int max, Function exceptionCreator) { AssertTools.checkArgument(min >= 0, "The minimum value must be greater than equal to 0."); AssertTools.checkArgument(min < max, "The minimum value must be less than the maximum value."); @@ -280,7 +280,7 @@ public class StringPropertyValidator extends BaseComparablePropertyValidato // ================================ @Override - protected StringPropertyValidator thisObject() { + protected StringPropertyValidator thisObject() { return this; } } diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java index 5405bb9..e7358a3 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ObjectPropertyValidatorTests.java @@ -30,7 +30,6 @@ import com.google.common.collect.Lists; import xyz.zhouxy.plusone.commons.collection.CollectionTools; import xyz.zhouxy.plusone.commons.util.DateTimeTools; -import xyz.zhouxy.plusone.commons.util.StringTools; import xyz.zhouxy.plusone.example.ExampleCommand; import xyz.zhouxy.plusone.example.Foo; import xyz.zhouxy.plusone.validator.BaseValidator; diff --git a/pom.xml b/pom.xml index 84cf435..eea56a2 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ - Plusone Validator 是一个参数校验框架,可针对 DTO 创建对应的校验器,并复用该校验器实例,对 DTO 进行校验。 + Plusone Validator 是一个校验库,用于构建校验规则对对象(尤其是入参)进行校验。API 参考自 .NET 的 FluentValidation。 @@ -27,6 +27,13 @@ + + org.junit + junit-bom + 5.12.1 + pom + import + xyz.zhouxy.plusone plusone-dependencies