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); super(getter);
} }
public TPropertyValidator between(Range<TProperty> range) { public TPropertyValidator inRange(Range<TProperty> range) {
withRule(range::contains, convertExceptionCreator("The value is not in " + range.toString())); withRule(value -> value != null && range.contains(value),
convertExceptionCreator("The value is not in " + range.toString()));
return thisObject(); return thisObject();
} }
public TPropertyValidator between(Range<TProperty> range, String errMsg) { public TPropertyValidator inRange(Range<TProperty> range, String errMsg) {
withRule(range::contains, convertExceptionCreator(errMsg)); withRule(value -> value != null && range.contains(value), convertExceptionCreator(errMsg));
return thisObject(); return thisObject();
} }
public <E extends RuntimeException> TPropertyValidator between( public <E extends RuntimeException> TPropertyValidator inRange(
Range<TProperty> range, Range<TProperty> range,
Supplier<E> exceptionCreator) { Supplier<E> exceptionCreator) {
withRule(range::contains, exceptionCreator); withRule(value -> value != null && range.contains(value), exceptionCreator);
return thisObject(); return thisObject();
} }
public <E extends RuntimeException> TPropertyValidator between( public <E extends RuntimeException> TPropertyValidator inRange(
Range<TProperty> range, Range<TProperty> range,
Function<TProperty, E> exceptionCreator) { Function<TProperty, E> exceptionCreator) {
withRule(range::contains, exceptionCreator); withRule(value -> value != null && range.contains(value), exceptionCreator);
return thisObject(); return thisObject();
} }

View File

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