完善 BaseValidator2。

pull/1/head
ZhouXY108 2022-12-10 03:42:44 +08:00
parent 52d248c35e
commit cc6e3a41c3
2 changed files with 128 additions and 58 deletions

View File

@ -6,16 +6,17 @@ import java.util.function.Function;
public abstract class BaseValidator2<T> { 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) { protected final <R> ValueValidator<T, R> ruleFor(Function<T, R> getter) {
ValueValidator<T, R> validValueHolder = new ValueValidator<>(getter); ValueValidator<T, R> validValueHolder = new ValueValidator<>(getter);
hs.add(validValueHolder); valueValidators.add(validValueHolder);
return validValueHolder; return validValueHolder;
} }
public void validate(T obj) { public void validate(T obj) {
this.hs.forEach(valueValidator -> valueValidator.validateProperty(obj)); for (ValueValidator<T, ?> valueValidator : this.valueValidators) {
valueValidator.validateProperty(obj);
}
} }
} }

View File

@ -4,31 +4,35 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
import java.util.function.Supplier; import java.util.function.Supplier;
import lombok.AccessLevel; import cn.hutool.core.util.StrUtil;
import lombok.AllArgsConstructor;
import xyz.zhouxy.plusone.constant.RegexConsts; import xyz.zhouxy.plusone.constant.RegexConsts;
import xyz.zhouxy.plusone.exception.InvalidInputException; import xyz.zhouxy.plusone.exception.InvalidInputException;
import xyz.zhouxy.plusone.util.RegexUtil; import xyz.zhouxy.plusone.util.RegexUtil;
public class ValueValidator<DTO, PROPERTY> { public class ValueValidator<DTO, PROPERTY> {
Function<DTO, PROPERTY> getter; Function<DTO, PROPERTY> getter;
List<RuleInfo<PROPERTY, ?>> rules = new ArrayList<>(); List<Consumer<PROPERTY>> rules = new ArrayList<>();
public ValueValidator(Function<DTO, PROPERTY> getter) { public ValueValidator(Function<DTO, PROPERTY> getter) {
this.getter = 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, private <E extends RuntimeException> void withRule(Predicate<PROPERTY> condition,
Function<PROPERTY, E> exceptionCreator) { 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 ===== // ====== notNull =====
public ValueValidator<DTO, PROPERTY> notNull() {
return notNull("Value could not be null.");
}
public ValueValidator<DTO, PROPERTY> notNull(String errMsg) { 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) { 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) { public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> notNull(Function<PROPERTY, E> exceptionCreator) {
@ -53,11 +61,11 @@ public class ValueValidator<DTO, PROPERTY> {
// ====== isNull ===== // ====== isNull =====
public ValueValidator<DTO, PROPERTY> isNull(String errMsg) { 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) { 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) { 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) { 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( public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> equalsThat(
Object that, Supplier<E> exceptionCreator) { Object that, Supplier<E> exceptionCreator) {
return equalsThat(that, value -> exceptionCreator.get()); return equalsThat(that, convertExceptionCreator(exceptionCreator));
} }
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> equalsThat( public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> equalsThat(
@ -89,17 +97,17 @@ public class ValueValidator<DTO, PROPERTY> {
// ===== state ===== // ===== state =====
public ValueValidator<DTO, PROPERTY> state(Predicate<PROPERTY> condition) { 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) { 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( public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> state(
Predicate<PROPERTY> condition, Predicate<PROPERTY> condition,
Supplier<E> exceptionCreator) { Supplier<E> exceptionCreator) {
return state(condition, value -> exceptionCreator.get()); return state(condition, convertExceptionCreator(exceptionCreator));
} }
public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> state( 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) { 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) { 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, public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> between(int min, int max,
Supplier<E> exceptionCreator) { 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, public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> between(int min, int max,
@ -132,6 +140,29 @@ public class ValueValidator<DTO, PROPERTY> {
return this; 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 ====== // ====== Collection, String ======
// ================================ // ================================
@ -139,11 +170,11 @@ public class ValueValidator<DTO, PROPERTY> {
// ====== notEmpty ===== // ====== notEmpty =====
public ValueValidator<DTO, PROPERTY> notEmpty(String errMsg) { 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) { 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) { public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> notEmpty(Function<PROPERTY, E> exceptionCreator) {
@ -165,11 +196,11 @@ public class ValueValidator<DTO, PROPERTY> {
// ====== isEmpty ===== // ====== isEmpty =====
public ValueValidator<DTO, PROPERTY> isEmpty(String errMsg) { 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) { 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) { public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> isEmpty(Function<PROPERTY, E> exceptionCreator) {
@ -195,15 +226,15 @@ public class ValueValidator<DTO, PROPERTY> {
// ====== isTrue ====== // ====== isTrue ======
public 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) { 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) { 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) { public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> isTrue(Function<PROPERTY, E> exceptionCreator) {
@ -214,15 +245,15 @@ public class ValueValidator<DTO, PROPERTY> {
// ====== isFalse ====== // ====== isFalse ======
public 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) { 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) { 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) { public <E extends RuntimeException> ValueValidator<DTO, PROPERTY> isFalse(Function<PROPERTY, E> exceptionCreator) {
@ -237,15 +268,17 @@ public class ValueValidator<DTO, PROPERTY> {
// ===== matches ===== // ===== matches =====
public ValueValidator<DTO, PROPERTY> matches(String regex, String errMsg) { 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) { 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) { Function<PROPERTY, E> exceptionCreator) {
withRule(input -> RegexUtil.matches((String) input, regex), exceptionCreator); withRule(input -> RegexUtil.matches((String) input, regex), exceptionCreator);
return this; return this;
@ -254,35 +287,72 @@ public class ValueValidator<DTO, PROPERTY> {
// ===== matchesOr ===== // ===== matchesOr =====
public ValueValidator<DTO, PROPERTY> matchesOr(String[] regexs, String errMsg) { 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) { 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) { Function<PROPERTY, E> exceptionCreator) {
withRule(input -> RegexUtil.matchesOr((String) input, regexs), exceptionCreator); withRule(input -> RegexUtil.matchesOr((String) input, regexs), exceptionCreator);
return this; return this;
} }
// ===== matchesOr ===== // ===== matchesAnd =====
public ValueValidator<DTO, PROPERTY> matchesAnd(String[] regexs, String errMsg) { 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; return this;
} }
// ===== email ===== // ===== email =====
public ValueValidator<DTO, PROPERTY> email() {
return email("The value is not an email address.");
}
public ValueValidator<DTO, PROPERTY> email(String errMsg) { 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) { 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) { 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) { void validateProperty(DTO obj) {
PROPERTY value = this.getter.apply(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 <V> Function<V, InvalidInputException> convertExceptionCreator(String errMsg) {
private static final class RuleInfo<V, E extends RuntimeException> { return convertExceptionCreator(errMsg);
private final Predicate<V> condition; }
private final Function<V, E> exceptionCreator;
private void validate(V obj) { private static <V, E extends RuntimeException> Function<V, E> convertExceptionCreator(
if (!this.condition.test(obj)) { Supplier<E> exceptionSupplier) {
throw exceptionCreator.apply(obj); return value -> exceptionSupplier.get();
}
}
} }
} }