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 super TProperty> 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 super TProperty> condition) {
- return must(condition, "无效的用户输入");
+ return must(condition, "The specified condition was not met for the input.");
}
public TPropertyValidator must(Predicate super TProperty> 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