Compare commits

..

No commits in common. "45dc111eb6ed67bba776c6a3eee70d3f12133dff" and "a9df6f8c0ed7c7994c1156a834613cf3ce874169" have entirely different histories.

6 changed files with 37 additions and 309 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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