refactor: 优化错误提示

This commit is contained in:
zhouxy108 2025-05-27 23:46:09 +08:00
parent 9f2ade6252
commit a28a6135a8
7 changed files with 65 additions and 29 deletions

View File

@ -30,7 +30,8 @@ public abstract class BaseComparablePropertyValidator<T, TProperty extends Compa
public TPropertyValidator inRange(Range<TProperty> range) { public TPropertyValidator inRange(Range<TProperty> range) {
withRule(value -> value != null && range.contains(value), 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(); return thisObject();
} }

View File

@ -71,10 +71,12 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
// ====== Object ====== // ====== Object ======
// ==================== // ====================
// ====== notNull ===== // ================================
// #region - notNull
// ================================
public TPropertyValidator notNull() { public TPropertyValidator notNull() {
return notNull("Value could not be null."); return notNull("The input must not be null.");
} }
public TPropertyValidator notNull(String errMsg) { public TPropertyValidator notNull(String errMsg) {
@ -90,7 +92,13 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
return thisObject(); return thisObject();
} }
// ====== isNull ===== // ================================
// #endregion - notNull
// ================================
// ================================
// #region - isNull
// ================================
public TPropertyValidator isNull(String errMsg) { public TPropertyValidator isNull(String errMsg) {
return isNull(convertExceptionCreator(errMsg)); return isNull(convertExceptionCreator(errMsg));
@ -105,10 +113,17 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
return thisObject(); return thisObject();
} }
// ===== equals ===== // ================================
// #endregion - isNull
// ================================
// ================================
// #region - equals
// ================================
public TPropertyValidator equalsThat(Object that) { 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) { public TPropertyValidator equalsThat(Object that, String errMsg) {
@ -126,10 +141,16 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
return thisObject(); return thisObject();
} }
// ===== must ===== // ================================
// #endregion - equals
// ================================
// ================================
// #region - must
// ================================
public TPropertyValidator must(Predicate<? super TProperty> condition) { 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) { public TPropertyValidator must(Predicate<? super TProperty> condition, String errMsg) {
@ -149,10 +170,8 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
return thisObject(); return thisObject();
} }
// ===== must =====
public TPropertyValidator must(Collection<Predicate<? super TProperty>> conditions) { 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) { public TPropertyValidator must(Collection<Predicate<? super TProperty>> conditions, String errMsg) {
@ -174,6 +193,10 @@ public abstract class BasePropertyValidator<T, TProperty, TPropertyValidator ext
return thisObject(); return thisObject();
} }
// ================================
// #endregion - must
// ================================
static <V> Function<V, IllegalArgumentException> convertExceptionCreator(String errMsg) { static <V> Function<V, IllegalArgumentException> convertExceptionCreator(String errMsg) {
return value -> new IllegalArgumentException(errMsg); return value -> new IllegalArgumentException(errMsg);
} }

View File

@ -28,7 +28,7 @@ public class BoolPropertyValidator<T> extends BasePropertyValidator<T, Boolean,
// ====== isTrueValue ====== // ====== isTrueValue ======
public BoolPropertyValidator<T> 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) { public BoolPropertyValidator<T> isTrueValue(String errMsg) {
@ -49,7 +49,7 @@ public class BoolPropertyValidator<T> extends BasePropertyValidator<T, Boolean,
// ====== isFalseValue ====== // ====== isFalseValue ======
public BoolPropertyValidator<T> 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) { public BoolPropertyValidator<T> isFalseValue(String errMsg) {

View File

@ -31,7 +31,8 @@ public class DoublePropertyValidator<T>
// ================================ // ================================
public DoublePropertyValidator<T> gt(double min) { 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) { public DoublePropertyValidator<T> gt(double min, String errMsg) {
@ -58,7 +59,8 @@ public class DoublePropertyValidator<T>
// ================================ // ================================
public DoublePropertyValidator<T> ge(double min) { 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) { public DoublePropertyValidator<T> ge(double min, String errMsg) {
@ -85,7 +87,8 @@ public class DoublePropertyValidator<T>
// ================================ // ================================
public DoublePropertyValidator<T> lt(double max) { 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) { public DoublePropertyValidator<T> lt(double max, String errMsg) {
@ -112,7 +115,8 @@ public class DoublePropertyValidator<T>
// ================================ // ================================
public DoublePropertyValidator<T> le(double max) { 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) { public DoublePropertyValidator<T> le(double max, String errMsg) {

View File

@ -31,7 +31,8 @@ public class IntPropertyValidator<T>
// ================================ // ================================
public IntPropertyValidator<T> gt(int min) { 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) { public IntPropertyValidator<T> gt(int min, String errMsg) {
@ -58,7 +59,8 @@ public class IntPropertyValidator<T>
// ================================ // ================================
public IntPropertyValidator<T> ge(int min) { 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) { public IntPropertyValidator<T> ge(int min, String errMsg) {
@ -85,7 +87,8 @@ public class IntPropertyValidator<T>
// ================================ // ================================
public IntPropertyValidator<T> lt(int max) { 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) { public IntPropertyValidator<T> lt(int max, String errMsg) {
@ -112,7 +115,8 @@ public class IntPropertyValidator<T>
// ================================ // ================================
public IntPropertyValidator<T> le(int max) { 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) { public IntPropertyValidator<T> le(int max, String errMsg) {

View File

@ -31,7 +31,8 @@ public class LongPropertyValidator<T>
// ================================ // ================================
public LongPropertyValidator<T> gt(long min) { 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) { public LongPropertyValidator<T> gt(long min, String errMsg) {
@ -58,7 +59,8 @@ public class LongPropertyValidator<T>
// ================================ // ================================
public LongPropertyValidator<T> ge(long min) { 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) { public LongPropertyValidator<T> ge(long min, String errMsg) {
@ -85,7 +87,8 @@ public class LongPropertyValidator<T>
// ================================ // ================================
public LongPropertyValidator<T> lt(long max) { 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) { public LongPropertyValidator<T> lt(long max, String errMsg) {
@ -112,7 +115,8 @@ public class LongPropertyValidator<T>
// ================================ // ================================
public LongPropertyValidator<T> le(long max) { 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) { public LongPropertyValidator<T> le(long max, String errMsg) {

View File

@ -156,7 +156,7 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
// ================================ // ================================
public StringPropertyValidator<T> notBlank() { 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) { public StringPropertyValidator<T> notBlank(String errMsg) {
@ -182,7 +182,7 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
// ================================ // ================================
public StringPropertyValidator<T> emailAddress() { 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) { public StringPropertyValidator<T> emailAddress(String errMsg) {
@ -208,7 +208,7 @@ public class StringPropertyValidator<T> extends BaseComparablePropertyValidator<
// ================================ // ================================
public StringPropertyValidator<T> notEmpty() { 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) { 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, public <E extends RuntimeException> StringPropertyValidator<T> length(int length,
Function<String, E> exceptionCreator) { Function<String, E> exceptionCreator) {
AssertTools.checkArgument(length >= 0, 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); withRule(s -> s == null || s.length() == length, exceptionCreator);
return this; return this;
} }