完善 BaseValidator2。
parent
52d248c35e
commit
cc6e3a41c3
|
@ -6,16 +6,17 @@ import java.util.function.Function;
|
|||
|
||||
public abstract class BaseValidator2<T> {
|
||||
|
||||
private List<ValueValidator<T, ?>> hs = new ArrayList<>();
|
||||
private List<ValueValidator<T, ?>> valueValidators = new ArrayList<>();
|
||||
|
||||
protected final <R> ValueValidator<T, R> ruleFor(Function<T, R> getter) {
|
||||
ValueValidator<T, R> validValueHolder = new ValueValidator<>(getter);
|
||||
hs.add(validValueHolder);
|
||||
valueValidators.add(validValueHolder);
|
||||
return validValueHolder;
|
||||
}
|
||||
|
||||
public void validate(T obj) {
|
||||
this.hs.forEach(valueValidator -> valueValidator.validateProperty(obj));
|
||||
for (ValueValidator<T, ?> valueValidator : this.valueValidators) {
|
||||
valueValidator.validateProperty(obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,31 +4,35 @@ import java.util.ArrayList;
|
|||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import xyz.zhouxy.plusone.constant.RegexConsts;
|
||||
import xyz.zhouxy.plusone.exception.InvalidInputException;
|
||||
import xyz.zhouxy.plusone.util.RegexUtil;
|
||||
|
||||
public class ValueValidator<DTO, PROPERTY> {
|
||||
Function<DTO, PROPERTY> getter;
|
||||
List<RuleInfo<PROPERTY, ?>> rules = new ArrayList<>();
|
||||
List<Consumer<PROPERTY>> rules = new ArrayList<>();
|
||||
|
||||
public ValueValidator(Function<DTO, PROPERTY> getter) {
|
||||
this.getter = getter;
|
||||
}
|
||||
|
||||
private void withRule(Predicate<PROPERTY> condition, String errMsg) {
|
||||
withRule(condition, value -> new InvalidInputException(errMsg));
|
||||
}
|
||||
|
||||
private <E extends RuntimeException> void withRule(Predicate<PROPERTY> condition,
|
||||
Function<PROPERTY, E> exceptionCreator) {
|
||||
this.rules.add(new RuleInfo<>(condition, exceptionCreator));
|
||||
withRule(value -> {
|
||||
if (!condition.test(value)) {
|
||||
throw exceptionCreator.apply(value);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private <E extends RuntimeException> void withRule(Consumer<PROPERTY> rule) {
|
||||
this.rules.add(rule);
|
||||
}
|
||||
|
||||
// ====================
|
||||
|
@ -37,12 +41,16 @@ public class ValueValidator<DTO, PROPERTY> {
|
|||
|
||||
// ====== notNull =====
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> notNull() {
|
||||
return notNull("Value could not be null.");
|
||||
}
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> notNull(String errMsg) {
|
||||
return notNull(value -> new InvalidInputException(errMsg));
|
||||
return notNull(convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> notNull(Supplier<E> exceptionCreator) {
|
||||
return notNull(value -> exceptionCreator.get());
|
||||
return notNull(convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> notNull(Function<PROPERTY, E> exceptionCreator) {
|
||||
|
@ -53,11 +61,11 @@ public class ValueValidator<DTO, PROPERTY> {
|
|||
// ====== isNull =====
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> isNull(String errMsg) {
|
||||
return isNull(value -> new InvalidInputException(errMsg));
|
||||
return isNull(convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> isNull(Supplier<E> exceptionCreator) {
|
||||
return isNull(value -> exceptionCreator.get());
|
||||
return isNull(convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> isNull(Function<PROPERTY, E> exceptionCreator) {
|
||||
|
@ -72,12 +80,12 @@ public class ValueValidator<DTO, PROPERTY> {
|
|||
}
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> equalsThat(Object that, String errMsg) {
|
||||
return equalsThat(that, value -> new InvalidInputException(errMsg));
|
||||
return equalsThat(that, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> equalsThat(
|
||||
Object that, Supplier<E> exceptionCreator) {
|
||||
return equalsThat(that, value -> exceptionCreator.get());
|
||||
return equalsThat(that, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> equalsThat(
|
||||
|
@ -89,17 +97,17 @@ public class ValueValidator<DTO, PROPERTY> {
|
|||
// ===== state =====
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> state(Predicate<PROPERTY> condition) {
|
||||
return state(condition, value -> new InvalidInputException("无效的用户输入"));
|
||||
return state(condition, "无效的用户输入");
|
||||
}
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> state(Predicate<PROPERTY> condition, String errMsg) {
|
||||
return state(condition, value -> new InvalidInputException(errMsg));
|
||||
return state(condition, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> state(
|
||||
Predicate<PROPERTY> condition,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return state(condition, value -> exceptionCreator.get());
|
||||
return state(condition, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> state(
|
||||
|
@ -114,16 +122,16 @@ public class ValueValidator<DTO, PROPERTY> {
|
|||
// =================
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> between(int min, int max) {
|
||||
return between(min, max, String.format("数值不在 %d 和 %d 之间", min, max));
|
||||
return between(min, max, String.format("数值不在 %s 和 %s 之间", String.valueOf(min), String.valueOf(max)));
|
||||
}
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> between(int min, int max, String errMsg) {
|
||||
return between(min, max, value -> new InvalidInputException(errMsg));
|
||||
return between(min, max, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> between(int min, int max,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return between(min, max, value -> exceptionCreator.get());
|
||||
return between(min, max, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> between(int min, int max,
|
||||
|
@ -132,6 +140,29 @@ public class ValueValidator<DTO, PROPERTY> {
|
|||
return this;
|
||||
}
|
||||
|
||||
// ====================
|
||||
// ====== double ======
|
||||
// ====================
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> between(double min, double max) {
|
||||
return between(min, max, String.format("数值不在 %s 和 %s 之间", String.valueOf(min), String.valueOf(max)));
|
||||
}
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> between(double min, double max, String errMsg) {
|
||||
return between(min, max, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> between(double min, double max,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return between(min, max, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> between(double min, double max,
|
||||
Function<PROPERTY, E> exceptionCreator) {
|
||||
withRule(value -> ((double) value >= min && (double) value < max), exceptionCreator);
|
||||
return this;
|
||||
}
|
||||
|
||||
// ================================
|
||||
// ====== Collection, String ======
|
||||
// ================================
|
||||
|
@ -139,11 +170,11 @@ public class ValueValidator<DTO, PROPERTY> {
|
|||
// ====== notEmpty =====
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> notEmpty(String errMsg) {
|
||||
return notEmpty(value -> new InvalidInputException(errMsg));
|
||||
return notEmpty(convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> notEmpty(Supplier<E> exceptionCreator) {
|
||||
return notEmpty(value -> exceptionCreator.get());
|
||||
return notEmpty(convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> notEmpty(Function<PROPERTY, E> exceptionCreator) {
|
||||
|
@ -165,11 +196,11 @@ public class ValueValidator<DTO, PROPERTY> {
|
|||
// ====== isEmpty =====
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> isEmpty(String errMsg) {
|
||||
return isEmpty(value -> new InvalidInputException(errMsg));
|
||||
return isEmpty(convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> isEmpty(Supplier<E> exceptionCreator) {
|
||||
return isEmpty(value -> exceptionCreator.get());
|
||||
return isEmpty(convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> isEmpty(Function<PROPERTY, E> exceptionCreator) {
|
||||
|
@ -195,15 +226,15 @@ public class ValueValidator<DTO, PROPERTY> {
|
|||
// ====== isTrue ======
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> isTrue() {
|
||||
return isTrue(value -> new InvalidInputException("Value must be true."));
|
||||
return isTrue("The value must be true.");
|
||||
}
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> isTrue(String errMsg) {
|
||||
return isTrue(value -> new InvalidInputException(errMsg));
|
||||
return isTrue(convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> isTrue(Supplier<E> exceptionCreator) {
|
||||
return isTrue(value -> exceptionCreator.get());
|
||||
return isTrue(convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> isTrue(Function<PROPERTY, E> exceptionCreator) {
|
||||
|
@ -214,15 +245,15 @@ public class ValueValidator<DTO, PROPERTY> {
|
|||
// ====== isFalse ======
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> isFalse() {
|
||||
return isFalse(value -> new InvalidInputException("Value must be true."));
|
||||
return isFalse("The value must be false.");
|
||||
}
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> isFalse(String errMsg) {
|
||||
return isFalse(value -> new InvalidInputException(errMsg));
|
||||
return isFalse(convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> isFalse(Supplier<E> exceptionCreator) {
|
||||
return isFalse(value -> exceptionCreator.get());
|
||||
return isFalse(convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> isFalse(Function<PROPERTY, E> exceptionCreator) {
|
||||
|
@ -237,15 +268,17 @@ public class ValueValidator<DTO, PROPERTY> {
|
|||
// ===== matches =====
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> matches(String regex, String errMsg) {
|
||||
return matches(regex, value -> new InvalidInputException(errMsg));
|
||||
return matches(regex, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> matches(String regex,
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> matches(
|
||||
String regex,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return matches(regex, value -> exceptionCreator.get());
|
||||
return matches(regex, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> matches(String regex,
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> matches(
|
||||
String regex,
|
||||
Function<PROPERTY, E> exceptionCreator) {
|
||||
withRule(input -> RegexUtil.matches((String) input, regex), exceptionCreator);
|
||||
return this;
|
||||
|
@ -254,35 +287,72 @@ public class ValueValidator<DTO, PROPERTY> {
|
|||
// ===== matchesOr =====
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> matchesOr(String[] regexs, String errMsg) {
|
||||
return matchesOr(regexs, value -> new InvalidInputException(errMsg));
|
||||
return matchesOr(regexs, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> matchesOr(String[] regexs,
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> matchesOr(
|
||||
String[] regexs,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return matchesOr(regexs, value -> exceptionCreator.get());
|
||||
return matchesOr(regexs, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> matchesOr(String[] regexs,
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> matchesOr(
|
||||
String[] regexs,
|
||||
Function<PROPERTY, E> exceptionCreator) {
|
||||
withRule(input -> RegexUtil.matchesOr((String) input, regexs), exceptionCreator);
|
||||
return this;
|
||||
}
|
||||
|
||||
// ===== matchesOr =====
|
||||
// ===== matchesAnd =====
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> matchesAnd(String[] regexs, String errMsg) {
|
||||
withRule(input -> RegexUtil.matchesAnd((String) input, regexs), errMsg);
|
||||
return matchesAnd(regexs, convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> matchesAnd(
|
||||
String[] regexs,
|
||||
Supplier<E> exceptionCreator) {
|
||||
return matchesAnd(regexs, convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> matchesAnd(
|
||||
String[] regexs,
|
||||
Function<PROPERTY, E> exceptionCreator) {
|
||||
withRule(input -> RegexUtil.matchesAnd((String) input, regexs), exceptionCreator);
|
||||
return this;
|
||||
}
|
||||
|
||||
// ===== notBlank =====
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> notBlank() {
|
||||
return notBlank("This String argument must have text; it must not be null, empty, or blank");
|
||||
}
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> notBlank(String errMsg) {
|
||||
return notBlank(convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> notBlank(Supplier<E> exceptionCreator) {
|
||||
return notBlank(convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> notBlank(Function<PROPERTY, E> exceptionCreator) {
|
||||
withRule(input -> StrUtil.isNotBlank((String) input), exceptionCreator);
|
||||
return this;
|
||||
}
|
||||
|
||||
// ===== email =====
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> email() {
|
||||
return email("The value is not an email address.");
|
||||
}
|
||||
|
||||
public ValueValidator<DTO, PROPERTY> email(String errMsg) {
|
||||
return email(value -> new InvalidInputException(errMsg));
|
||||
return email(convertExceptionCreator(errMsg));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> email(Supplier<E> exceptionCreator) {
|
||||
return email(value -> exceptionCreator.get());
|
||||
return email(convertExceptionCreator(exceptionCreator));
|
||||
}
|
||||
|
||||
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> email(Function<PROPERTY, E> exceptionCreator) {
|
||||
|
@ -293,18 +363,17 @@ public class ValueValidator<DTO, PROPERTY> {
|
|||
|
||||
void validateProperty(DTO obj) {
|
||||
PROPERTY value = this.getter.apply(obj);
|
||||
this.rules.forEach(rule -> rule.validate(value));
|
||||
for (var rule : this.rules) {
|
||||
rule.accept(value);
|
||||
}
|
||||
}
|
||||
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
private static final class RuleInfo<V, E extends RuntimeException> {
|
||||
private final Predicate<V> condition;
|
||||
private final Function<V, E> exceptionCreator;
|
||||
private static <V> Function<V, InvalidInputException> convertExceptionCreator(String errMsg) {
|
||||
return convertExceptionCreator(errMsg);
|
||||
}
|
||||
|
||||
private void validate(V obj) {
|
||||
if (!this.condition.test(obj)) {
|
||||
throw exceptionCreator.apply(obj);
|
||||
}
|
||||
}
|
||||
private static <V, E extends RuntimeException> Function<V, E> convertExceptionCreator(
|
||||
Supplier<E> exceptionSupplier) {
|
||||
return value -> exceptionSupplier.get();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue