Compare commits

..

3 Commits

Author SHA1 Message Date
45dc111eb6 更新项目描述 2025-05-20 00:41:25 +08:00
19b7926f0d refactor!: 修改数值校验器的 API
原 `IntPropertyValidator`、`LongPropertyValidator` 和 `DoublePropertyValidator` 的
`between` 方法,使用“左闭右开”区间,但方法签名上并不明确,容易产生歧义。

现改为 `gt`(GreaterThan)、`ge`(GreaterThanOrEqualTo)、`lt`(LessThan)、`le`(LessThanOrEqualTo)。
2025-05-19 21:16:27 +08:00
297649c63a refactor: StringPropertyValidatorisNullOrEmpty 使用 Strings#isNullOrEmpty 进行逻辑判断 2025-05-19 20:41:47 +08:00
6 changed files with 309 additions and 37 deletions

View File

@ -19,31 +19,121 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
public class DoublePropertyValidator<DTO> extends BaseComparablePropertyValidator<DTO, Double, DoublePropertyValidator<DTO>> {
public class DoublePropertyValidator<DTO>
extends BaseComparablePropertyValidator<DTO, Double, DoublePropertyValidator<DTO>> {
DoublePropertyValidator(Function<DTO, Double> getter) {
super(getter);
}
public DoublePropertyValidator<DTO> between(double min, double max) {
return between(min, max, String.format("数值不在 %s 和 %s 之间", String.valueOf(min), String.valueOf(max)));
// ================================
// #region - greater than
// ================================
public DoublePropertyValidator<DTO> gt(double min) {
return gt(min, String.format("The value should be greater than %s", min));
}
public DoublePropertyValidator<DTO> between(double min, double max, String errMsg) {
return between(min, max, convertExceptionCreator(errMsg));
public DoublePropertyValidator<DTO> gt(double min, String errMsg) {
return gt(min, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> DoublePropertyValidator<DTO> between(double min, double max,
Supplier<E> exceptionCreator) {
return between(min, max, convertExceptionCreator(exceptionCreator));
public <E extends RuntimeException> DoublePropertyValidator<DTO> gt(
double min, Supplier<E> exceptionCreator) {
return gt(min, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> DoublePropertyValidator<DTO> between(double min, double max,
Function<Double, E> exceptionCreator) {
withRule(value -> (value >= min && value < max), exceptionCreator);
public <E extends RuntimeException> DoublePropertyValidator<DTO> gt(
double min, Function<Double, E> exceptionCreator) {
withRule(value -> (value != null && value > min), exceptionCreator);
return this;
}
// ================================
// #endregion - greater than
// ================================
// ================================
// #region - greater than or equal to
// ================================
public DoublePropertyValidator<DTO> ge(double min) {
return ge(min, String.format("The value should be greater than or equal to %s", min));
}
public DoublePropertyValidator<DTO> ge(double min, String errMsg) {
return ge(min, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> DoublePropertyValidator<DTO> ge(
double min, Supplier<E> exceptionCreator) {
return ge(min, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> DoublePropertyValidator<DTO> ge(
double min, Function<Double, E> exceptionCreator) {
withRule(value -> (value != null && value >= min), exceptionCreator);
return this;
}
// ================================
// #endregion - greater than or equal to
// ================================
// ================================
// #region - less than
// ================================
public DoublePropertyValidator<DTO> lt(double max) {
return lt(max, String.format("The value should be less than %s", max));
}
public DoublePropertyValidator<DTO> lt(double max, String errMsg) {
return lt(max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> DoublePropertyValidator<DTO> lt(
double max, Supplier<E> exceptionCreator) {
return lt(max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> DoublePropertyValidator<DTO> lt(
double max, Function<Double, E> exceptionCreator) {
withRule(value -> (value != null && value < max), exceptionCreator);
return this;
}
// ================================
// #endregion - less than
// ================================
// ================================
// #region - less than or equal to
// ================================
public DoublePropertyValidator<DTO> le(double max) {
return le(max, String.format("The value should be less than or equal to %s", max));
}
public DoublePropertyValidator<DTO> le(double max, String errMsg) {
return le(max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> DoublePropertyValidator<DTO> le(
double max, Supplier<E> exceptionCreator) {
return le(max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> DoublePropertyValidator<DTO> le(
double max, Function<Double, E> exceptionCreator) {
withRule(value -> (value != null && value <= max), exceptionCreator);
return this;
}
// ================================
// #endregion - less than or equal to
// ================================
@Override
protected DoublePropertyValidator<DTO> thisObject() {
return this;

View File

@ -19,31 +19,121 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
public class IntPropertyValidator<DTO> extends BaseComparablePropertyValidator<DTO, Integer, IntPropertyValidator<DTO>> {
public class IntPropertyValidator<DTO>
extends BaseComparablePropertyValidator<DTO, Integer, IntPropertyValidator<DTO>> {
IntPropertyValidator(Function<DTO, Integer> getter) {
super(getter);
}
public IntPropertyValidator<DTO> between(int min, int max) {
return between(min, max, String.format("数值不在 %d 和 %d 之间", min, max));
// ================================
// #region - greater than
// ================================
public IntPropertyValidator<DTO> gt(int min) {
return gt(min, String.format("The value should be greater than %d", min));
}
public IntPropertyValidator<DTO> between(int min, int max, String errMsg) {
return between(min, max, convertExceptionCreator(errMsg));
public IntPropertyValidator<DTO> gt(int min, String errMsg) {
return gt(min, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> IntPropertyValidator<DTO> between(int min, int max,
Supplier<E> exceptionCreator) {
return between(min, max, convertExceptionCreator(exceptionCreator));
public <E extends RuntimeException> IntPropertyValidator<DTO> gt(
int min, Supplier<E> exceptionCreator) {
return gt(min, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> IntPropertyValidator<DTO> between(int min, int max,
Function<Integer, E> exceptionCreator) {
withRule(value -> (value >= min && value < max), exceptionCreator);
public <E extends RuntimeException> IntPropertyValidator<DTO> gt(
int min, Function<Integer, E> exceptionCreator) {
withRule(value -> (value != null && value > min), exceptionCreator);
return this;
}
// ================================
// #endregion - greater than
// ================================
// ================================
// #region - greater than or equal to
// ================================
public IntPropertyValidator<DTO> ge(int min) {
return ge(min, String.format("The value should be greater than or equal to %d", min));
}
public IntPropertyValidator<DTO> ge(int min, String errMsg) {
return ge(min, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> IntPropertyValidator<DTO> ge(
int min, Supplier<E> exceptionCreator) {
return ge(min, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> IntPropertyValidator<DTO> ge(
int min, Function<Integer, E> exceptionCreator) {
withRule(value -> (value != null && value >= min), exceptionCreator);
return this;
}
// ================================
// #endregion - greater than or equal to
// ================================
// ================================
// #region - less than
// ================================
public IntPropertyValidator<DTO> lt(int max) {
return lt(max, String.format("The value should be less than %d", max));
}
public IntPropertyValidator<DTO> lt(int max, String errMsg) {
return lt(max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> IntPropertyValidator<DTO> lt(
int max, Supplier<E> exceptionCreator) {
return lt(max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> IntPropertyValidator<DTO> lt(
int max, Function<Integer, E> exceptionCreator) {
withRule(value -> (value != null && value < max), exceptionCreator);
return this;
}
// ================================
// #endregion - less than
// ================================
// ================================
// #region - less than or equal to
// ================================
public IntPropertyValidator<DTO> le(int max) {
return le(max, String.format("The value should be less than or equal to %d", max));
}
public IntPropertyValidator<DTO> le(int max, String errMsg) {
return le(max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> IntPropertyValidator<DTO> le(
int max, Supplier<E> exceptionCreator) {
return le(max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> IntPropertyValidator<DTO> le(
int max, Function<Integer, E> exceptionCreator) {
withRule(value -> (value != null && value <= max), exceptionCreator);
return this;
}
// ================================
// #endregion - less than or equal to
// ================================
@Override
protected IntPropertyValidator<DTO> thisObject() {
return this;

View File

@ -19,31 +19,121 @@ package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
public class LongPropertyValidator<DTO> extends BaseComparablePropertyValidator<DTO, Long, LongPropertyValidator<DTO>> {
public class LongPropertyValidator<DTO>
extends BaseComparablePropertyValidator<DTO, Long, LongPropertyValidator<DTO>> {
LongPropertyValidator(Function<DTO, Long> getter) {
super(getter);
}
public LongPropertyValidator<DTO> between(long min, long max) {
return between(min, max, String.format("数值不在 %d 和 %d 之间", min, max));
// ================================
// #region - greater than
// ================================
public LongPropertyValidator<DTO> gt(long min) {
return gt(min, String.format("The value should be greater than %d", min));
}
public LongPropertyValidator<DTO> between(long min, long max, String errMsg) {
return between(min, max, convertExceptionCreator(errMsg));
public LongPropertyValidator<DTO> gt(long min, String errMsg) {
return gt(min, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> LongPropertyValidator<DTO> between(long min, long max,
Supplier<E> exceptionCreator) {
return between(min, max, convertExceptionCreator(exceptionCreator));
public <E extends RuntimeException> LongPropertyValidator<DTO> gt(
long min, Supplier<E> exceptionCreator) {
return gt(min, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> LongPropertyValidator<DTO> between(long min, long max,
Function<Long, E> exceptionCreator) {
withRule(value -> (value >= min && value < max), exceptionCreator);
public <E extends RuntimeException> LongPropertyValidator<DTO> gt(
long min, Function<Long, E> exceptionCreator) {
withRule(value -> (value != null && value > min), exceptionCreator);
return this;
}
// ================================
// #endregion - greater than
// ================================
// ================================
// #region - greater than or equal to
// ================================
public LongPropertyValidator<DTO> ge(long min) {
return ge(min, String.format("The value should be greater than or equal to %d", min));
}
public LongPropertyValidator<DTO> ge(long min, String errMsg) {
return ge(min, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> LongPropertyValidator<DTO> ge(
long min, Supplier<E> exceptionCreator) {
return ge(min, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> LongPropertyValidator<DTO> ge(
long min, Function<Long, E> exceptionCreator) {
withRule(value -> (value != null && value >= min), exceptionCreator);
return this;
}
// ================================
// #endregion - greater than or equal to
// ================================
// ================================
// #region - less than
// ================================
public LongPropertyValidator<DTO> lt(long max) {
return lt(max, String.format("The value should be less than %d", max));
}
public LongPropertyValidator<DTO> lt(long max, String errMsg) {
return lt(max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> LongPropertyValidator<DTO> lt(
long max, Supplier<E> exceptionCreator) {
return lt(max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> LongPropertyValidator<DTO> lt(
long max, Function<Long, E> exceptionCreator) {
withRule(value -> (value != null && value < max), exceptionCreator);
return this;
}
// ================================
// #endregion - less than
// ================================
// ================================
// #region - less than or equal to
// ================================
public LongPropertyValidator<DTO> le(long max) {
return le(max, String.format("The value should be less than or equal to %d", max));
}
public LongPropertyValidator<DTO> le(long max, String errMsg) {
return le(max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> LongPropertyValidator<DTO> le(
long max, Supplier<E> exceptionCreator) {
return le(max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> LongPropertyValidator<DTO> le(
long max, Function<Long, E> exceptionCreator) {
withRule(value -> (value != null && value <= max), exceptionCreator);
return this;
}
// ================================
// #endregion - less than or equal to
// ================================
@Override
protected LongPropertyValidator<DTO> thisObject() {
return this;

View File

@ -22,6 +22,8 @@ import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import com.google.common.base.Strings;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.commons.util.AssertTools;
import xyz.zhouxy.plusone.commons.util.RegexTools;
@ -239,7 +241,7 @@ public class StringPropertyValidator<DTO> extends BaseComparablePropertyValidato
public <E extends RuntimeException> StringPropertyValidator<DTO> isNullOrEmpty(
Function<String, E> exceptionCreator) {
withRule(s -> s == null || s.isEmpty(), exceptionCreator);
withRule(Strings::isNullOrEmpty, exceptionCreator);
return this;
}

View File

@ -14,7 +14,6 @@ import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.commons.util.StringTools;
import xyz.zhouxy.plusone.validator.MapValidator;
public //
class MapValidatorTests {
private static final MapValidator<String, Object> validator = ParamsValidator.INSTANCE;
@ -72,7 +71,8 @@ class ParamsValidator extends MapValidator<String, Object> {
ruleForInt(AGE)
.withRule(Objects::nonNull)
.between(18, 61);
.ge(18)
.le(60);
ruleForBool(BOOLEAN)
.notNull("Boolean property could not be null.")

View File

@ -18,7 +18,7 @@
</modules>
<description>
Plusone Validator 是一个参数校验框架,可针对 DTO 创建对应的校验器,并复用该校验器实例,对 DTO 进行校验
Plusone Validator 是一个校验库用于构建校验规则对对象尤其是入参进行校验。API 参考自 .NET 的 FluentValidation
</description>
<properties>