forked from plusone/plusone-validator
refactor: 优化错误提示
This commit is contained in:
parent
9f2ade6252
commit
a28a6135a8
@ -30,7 +30,8 @@ public abstract class BaseComparablePropertyValidator<T, TProperty extends Compa
|
||||
|
||||
public TPropertyValidator inRange(Range<TProperty> 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();
|
||||
}
|
||||
|
||||
|
@ -71,10 +71,12 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
|
||||
// ====== 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) {
|
||||
@ -90,7 +92,13 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
|
||||
return thisObject();
|
||||
}
|
||||
|
||||
// ====== isNull =====
|
||||
// ================================
|
||||
// #endregion - notNull
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - isNull
|
||||
// ================================
|
||||
|
||||
public TPropertyValidator isNull(String errMsg) {
|
||||
return isNull(convertExceptionCreator(errMsg));
|
||||
@ -105,10 +113,17 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
|
||||
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) {
|
||||
@ -126,10 +141,16 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
|
||||
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) {
|
||||
@ -149,10 +170,8 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
|
||||
return thisObject();
|
||||
}
|
||||
|
||||
// ===== must =====
|
||||
|
||||
public TPropertyValidator must(Collection<Predicate<? super TProperty>> conditions) {
|
||||
return must(conditions, "无效的用户输入");
|
||||
return must(conditions, "The specified conditions were not met for the input.");
|
||||
}
|
||||
|
||||
public TPropertyValidator must(Collection<Predicate<? super TProperty>> conditions, String errMsg) {
|
||||
@ -174,6 +193,10 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
|
||||
return thisObject();
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - must
|
||||
// ================================
|
||||
|
||||
static <V> Function<V, IllegalArgumentException> convertExceptionCreator(String errMsg) {
|
||||
return value -> new IllegalArgumentException(errMsg);
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ public class BoolPropertyValidator<T> extends BasePropertyValidator<T, Boolean,
|
||||
// ====== isTrueValue ======
|
||||
|
||||
public BoolPropertyValidator<T> isTrueValue() {
|
||||
return isTrueValue("The value must be true.");
|
||||
return isTrueValue("The input must be true.");
|
||||
}
|
||||
|
||||
public BoolPropertyValidator<T> isTrueValue(String errMsg) {
|
||||
@ -49,7 +49,7 @@ public class BoolPropertyValidator<T> extends BasePropertyValidator<T, Boolean,
|
||||
// ====== isFalseValue ======
|
||||
|
||||
public BoolPropertyValidator<T> isFalseValue() {
|
||||
return isFalseValue("The value must be false.");
|
||||
return isFalseValue("The input must be false.");
|
||||
}
|
||||
|
||||
public BoolPropertyValidator<T> isFalseValue(String errMsg) {
|
||||
|
@ -31,7 +31,8 @@ public class DoublePropertyValidator<T>
|
||||
// ================================
|
||||
|
||||
public DoublePropertyValidator<T> gt(double min) {
|
||||
return gt(min, String.format("The value should be greater than %s", min));
|
||||
return gt(min, () -> new IllegalArgumentException(
|
||||
String.format("The input must be greater than '%s'.", min)));
|
||||
}
|
||||
|
||||
public DoublePropertyValidator<T> gt(double min, String errMsg) {
|
||||
@ -58,7 +59,8 @@ public class DoublePropertyValidator<T>
|
||||
// ================================
|
||||
|
||||
public DoublePropertyValidator<T> ge(double min) {
|
||||
return ge(min, String.format("The value should be greater than or equal to %s", min));
|
||||
return ge(min, () -> new IllegalArgumentException(
|
||||
String.format("The input must be greater than or equal to '%s'.", min)));
|
||||
}
|
||||
|
||||
public DoublePropertyValidator<T> ge(double min, String errMsg) {
|
||||
@ -85,7 +87,8 @@ public class DoublePropertyValidator<T>
|
||||
// ================================
|
||||
|
||||
public DoublePropertyValidator<T> lt(double max) {
|
||||
return lt(max, String.format("The value should be less than %s", max));
|
||||
return lt(max, () -> new IllegalArgumentException(
|
||||
String.format("The input must be less than '%s'.", max)));
|
||||
}
|
||||
|
||||
public DoublePropertyValidator<T> lt(double max, String errMsg) {
|
||||
@ -112,7 +115,8 @@ public class DoublePropertyValidator<T>
|
||||
// ================================
|
||||
|
||||
public DoublePropertyValidator<T> le(double max) {
|
||||
return le(max, String.format("The value should be less than or equal to %s", max));
|
||||
return le(max, () -> new IllegalArgumentException(
|
||||
String.format("The input must be less than or equal to '%s'.", max)));
|
||||
}
|
||||
|
||||
public DoublePropertyValidator<T> le(double max, String errMsg) {
|
||||
|
@ -31,7 +31,8 @@ public class IntPropertyValidator<T>
|
||||
// ================================
|
||||
|
||||
public IntPropertyValidator<T> gt(int min) {
|
||||
return gt(min, String.format("The value should be greater than %d", min));
|
||||
return gt(min, () -> new IllegalArgumentException(
|
||||
String.format("The input must be greater than '%d'.", min)));
|
||||
}
|
||||
|
||||
public IntPropertyValidator<T> gt(int min, String errMsg) {
|
||||
@ -58,7 +59,8 @@ public class IntPropertyValidator<T>
|
||||
// ================================
|
||||
|
||||
public IntPropertyValidator<T> ge(int min) {
|
||||
return ge(min, String.format("The value should be greater than or equal to %d", min));
|
||||
return ge(min, () -> new IllegalArgumentException(
|
||||
String.format("The input must be greater than or equal to '%d'.", min)));
|
||||
}
|
||||
|
||||
public IntPropertyValidator<T> ge(int min, String errMsg) {
|
||||
@ -85,7 +87,8 @@ public class IntPropertyValidator<T>
|
||||
// ================================
|
||||
|
||||
public IntPropertyValidator<T> lt(int max) {
|
||||
return lt(max, String.format("The value should be less than %d", max));
|
||||
return lt(max, () -> new IllegalArgumentException(
|
||||
String.format("The input must be less than '%d'.", max)));
|
||||
}
|
||||
|
||||
public IntPropertyValidator<T> lt(int max, String errMsg) {
|
||||
@ -112,7 +115,8 @@ public class IntPropertyValidator<T>
|
||||
// ================================
|
||||
|
||||
public IntPropertyValidator<T> le(int max) {
|
||||
return le(max, String.format("The value should be less than or equal to %d", max));
|
||||
return le(max, () -> new IllegalArgumentException(
|
||||
String.format("The input must be less than or equal to '%d'.", max)));
|
||||
}
|
||||
|
||||
public IntPropertyValidator<T> le(int max, String errMsg) {
|
||||
|
@ -31,7 +31,8 @@ public class LongPropertyValidator<T>
|
||||
// ================================
|
||||
|
||||
public LongPropertyValidator<T> gt(long min) {
|
||||
return gt(min, String.format("The value should be greater than %d", min));
|
||||
return gt(min, () -> new IllegalArgumentException(
|
||||
String.format("The input must be greater than '%d'.", min)));
|
||||
}
|
||||
|
||||
public LongPropertyValidator<T> gt(long min, String errMsg) {
|
||||
@ -58,7 +59,8 @@ public class LongPropertyValidator<T>
|
||||
// ================================
|
||||
|
||||
public LongPropertyValidator<T> ge(long min) {
|
||||
return ge(min, String.format("The value should be greater than or equal to %d", min));
|
||||
return ge(min, () -> new IllegalArgumentException(
|
||||
String.format("The input must be greater than or equal to '%d'.", min)));
|
||||
}
|
||||
|
||||
public LongPropertyValidator<T> ge(long min, String errMsg) {
|
||||
@ -85,7 +87,8 @@ public class LongPropertyValidator<T>
|
||||
// ================================
|
||||
|
||||
public LongPropertyValidator<T> lt(long max) {
|
||||
return lt(max, String.format("The value should be less than %d", max));
|
||||
return lt(max, () -> new IllegalArgumentException(
|
||||
String.format("The input must be less than '%d'.", max)));
|
||||
}
|
||||
|
||||
public LongPropertyValidator<T> lt(long max, String errMsg) {
|
||||
@ -112,7 +115,8 @@ public class LongPropertyValidator<T>
|
||||
// ================================
|
||||
|
||||
public LongPropertyValidator<T> le(long max) {
|
||||
return le(max, String.format("The value should be less than or equal to %d", max));
|
||||
return le(max, () -> new IllegalArgumentException(
|
||||
String.format("The input must be less than or equal to '%d'.", max)));
|
||||
}
|
||||
|
||||
public LongPropertyValidator<T> le(long max, String errMsg) {
|
||||
|
@ -156,7 +156,7 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
|
||||
// ================================
|
||||
|
||||
public StringPropertyValidator<T> notBlank() {
|
||||
return notBlank("The value must have text; it must not be null, empty, or blank.");
|
||||
return notBlank("The input must not be blank.");
|
||||
}
|
||||
|
||||
public StringPropertyValidator<T> notBlank(String errMsg) {
|
||||
@ -182,7 +182,7 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
|
||||
// ================================
|
||||
|
||||
public StringPropertyValidator<T> emailAddress() {
|
||||
return emailAddress("The value is not an email address.");
|
||||
return emailAddress("The input is not a valid email address.");
|
||||
}
|
||||
|
||||
public StringPropertyValidator<T> emailAddress(String errMsg) {
|
||||
@ -208,7 +208,7 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
|
||||
// ================================
|
||||
|
||||
public StringPropertyValidator<T> notEmpty() {
|
||||
return notEmpty("The value must not be empty.");
|
||||
return notEmpty("The input must not be empty.");
|
||||
}
|
||||
|
||||
public StringPropertyValidator<T> notEmpty(String errMsg) {
|
||||
@ -245,7 +245,7 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
|
||||
public <E extends RuntimeException> StringPropertyValidator<T> length(int length,
|
||||
Function<String, E> 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;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user