forked from plusone/plusone-validator
refactor!: BasePropertyValidator
的 between
方法重命名为 inRange
1. `BasePropertyValidator` 的 `between` 方法重命名为 `inRange`,更符合语义; 2. 将判空作为规则的一部分。
This commit is contained in:
parent
ecdd0a616a
commit
1b8a5634b3
@ -25,33 +25,34 @@ public abstract
|
||||
class BaseComparablePropertyValidator<TObj,
|
||||
TProperty extends Comparable<TProperty>,
|
||||
TPropertyValidator extends BaseComparablePropertyValidator<TObj, TProperty, TPropertyValidator>>
|
||||
extends BasePropertyValidator<TObj, TProperty, TPropertyValidator> {
|
||||
extends BasePropertyValidator<TObj, TProperty, TPropertyValidator> {
|
||||
|
||||
BaseComparablePropertyValidator(Function<TObj, ? extends TProperty> getter) {
|
||||
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();
|
||||
}
|
||||
|
||||
|
@ -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()),
|
||||
"两次输入的密码不一致");
|
||||
|
Loading…
x
Reference in New Issue
Block a user