refactor!: BasePropertyValidatorbetween 方法重命名为 inRange

1. `BasePropertyValidator` 的 `between` 方法重命名为 `inRange`,更符合语义;

2. 将判空作为规则的一部分。
This commit is contained in:
zhouxy108 2025-05-19 17:28:45 +08:00
parent ecdd0a616a
commit 1b8a5634b3
2 changed files with 12 additions and 11 deletions

View File

@ -31,27 +31,28 @@ class BaseComparablePropertyValidator<TObj,
super(getter);
}
public TPropertyValidator between(Range<TProperty> range) {
withRule(range::contains, convertExceptionCreator("The value is not in " + range.toString()));
public TPropertyValidator inRange(Range<TProperty> range) {
withRule(value -> value != null && range.contains(value),
convertExceptionCreator("The value is not in " + range.toString()));
return thisObject();
}
public TPropertyValidator between(Range<TProperty> range, String errMsg) {
withRule(range::contains, convertExceptionCreator(errMsg));
public TPropertyValidator inRange(Range<TProperty> range, String errMsg) {
withRule(value -> value != null && range.contains(value), convertExceptionCreator(errMsg));
return thisObject();
}
public <E extends RuntimeException> TPropertyValidator between(
public <E extends RuntimeException> TPropertyValidator inRange(
Range<TProperty> range,
Supplier<E> exceptionCreator) {
withRule(range::contains, exceptionCreator);
withRule(value -> value != null && range.contains(value), exceptionCreator);
return thisObject();
}
public <E extends RuntimeException> TPropertyValidator between(
public <E extends RuntimeException> TPropertyValidator inRange(
Range<TProperty> range,
Function<TProperty, E> exceptionCreator) {
withRule(range::contains, exceptionCreator);
withRule(value -> value != null && range.contains(value), exceptionCreator);
return thisObject();
}

View File

@ -56,11 +56,11 @@ class BaseValidatorTest {
ruleForComparable(RegisterCommand::getYearOfBirth)
.notNull()
.between(Range.closed(thisYear - 60, thisYear - 18));
.inRange(Range.closed(thisYear - 60, thisYear - 18));
ruleForInt(RegisterCommand::getYearOfBirth)
.notNull()
.between(Range.closed(thisYear - 60, thisYear - 18));
.inRange(Range.closed(thisYear - 60, thisYear - 18));
withRule(registerCommand -> Objects.equals(registerCommand.getPassword(), registerCommand.getPassword2()),
"两次输入的密码不一致");