refactor: 修改项目结构,创建 plusone-validator-parent,将 plusone-validator 作为其 module

创建 `plusone-validator-parent`,将 `plusone-validator` 作为其 module,
方便后续在 `plusone-validator-parent` 下创建测试等相关子模块。

删除 `commons-lang3`,用 `plusone-commons` 的 `StringTools` 替代 `commons-lang3` 的 `StringUtils`。

补充测试依赖 `junit-jupiter-engine`。
This commit is contained in:
2025-05-08 23:25:48 +08:00
parent 6d5cad48e2
commit 818c8fb26f
21 changed files with 64 additions and 33 deletions

45
plusone-validator/pom.xml Normal file
View File

@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-validator-parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>plusone-validator</artifactId>
<name>plusone-validator</name>
<url>http://zhouxy.xyz</url>
<description>
Plusone Validator 是一个参数校验框架,可针对 DTO 创建对应的校验器,并复用该校验器实例,对 DTO 进行校验。
</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>xyz.zhouxy.plusone</groupId>
<artifactId>plusone-commons</artifactId>
<version>1.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,171 @@
package xyz.zhouxy.plusone.validator;
import java.util.Collection;
import java.util.LinkedList;
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;
public abstract class BasePropertyValidator< //
TObj, //
TProperty, //
TPropertyValidator extends BasePropertyValidator<TObj, TProperty, TPropertyValidator>> {
private final Function<TObj, ? extends TProperty> getter;
private final List<Consumer<? super TProperty>> consumers = new LinkedList<>();
protected BasePropertyValidator(Function<TObj, ? extends TProperty> getter) {
this.getter = getter;
}
public final TPropertyValidator withRule(Predicate<? super TProperty> rule) {
return withRule(rule, v -> new IllegalArgumentException());
}
public final TPropertyValidator withRule(
Predicate<? super TProperty> rule, String errMsg) {
return withRule(rule, convertExceptionCreator(errMsg));
}
public final <E extends RuntimeException> TPropertyValidator withRule(
Predicate<? super TProperty> rule, Supplier<E> e) {
return withRule(rule, convertExceptionCreator(e));
}
public final <E extends RuntimeException> TPropertyValidator withRule(
Predicate<? super TProperty> rule, Function<TProperty, E> e) {
this.consumers.add(v -> {
if (!rule.test(v)) {
throw e.apply(v);
}
});
return thisObject();
}
public final <T extends TObj> void validate(T obj) {
for (Consumer<? super TProperty> consumer : consumers) {
consumer.accept(getter.apply(obj));
}
}
protected abstract TPropertyValidator thisObject();
// ====================
// ====== Object ======
// ====================
// ====== notNull =====
public TPropertyValidator notNull() {
return notNull("Value could not be null.");
}
public TPropertyValidator notNull(String errMsg) {
return notNull(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> TPropertyValidator notNull(Supplier<E> exceptionCreator) {
return notNull(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> TPropertyValidator notNull(Function<TProperty, E> exceptionCreator) {
withRule(Objects::nonNull, exceptionCreator);
return thisObject();
}
// ====== isNull =====
public TPropertyValidator isNull(String errMsg) {
return isNull(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> TPropertyValidator isNull(Supplier<E> exceptionCreator) {
return isNull(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> TPropertyValidator isNull(Function<TProperty, E> exceptionCreator) {
withRule(Objects::isNull, exceptionCreator);
return thisObject();
}
// ===== equals =====
public TPropertyValidator equalsThat(Object that) {
return equalsThat(that, value -> new IllegalArgumentException(String.format("(%s) 必须与 (%s) 相等", value, that)));
}
public TPropertyValidator equalsThat(Object that, String errMsg) {
return equalsThat(that, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> TPropertyValidator equalsThat(
Object that, Supplier<E> exceptionCreator) {
return equalsThat(that, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> TPropertyValidator equalsThat(
Object that, Function<TProperty, E> exceptionCreator) {
withRule(value -> Objects.equals(value, that), exceptionCreator);
return thisObject();
}
// ===== isTrue =====
public TPropertyValidator isTrue(Predicate<? super TProperty> condition) {
return isTrue(condition, "无效的用户输入");
}
public TPropertyValidator isTrue(Predicate<? super TProperty> condition, String errMsg) {
return isTrue(condition, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> TPropertyValidator isTrue(
Predicate<? super TProperty> condition,
Supplier<E> exceptionCreator) {
return isTrue(condition, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> TPropertyValidator isTrue(
Predicate<? super TProperty> condition,
Function<TProperty, E> exceptionCreator) {
withRule(condition, exceptionCreator);
return thisObject();
}
// ===== isTrue =====
public TPropertyValidator isTrue(Collection<Predicate<? super TProperty>> conditions) {
return isTrue(conditions, "无效的用户输入");
}
public TPropertyValidator isTrue(Collection<Predicate<? super TProperty>> conditions, String errMsg) {
return isTrue(conditions, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> TPropertyValidator isTrue(
Collection<Predicate<? super TProperty>> conditions,
Supplier<E> exceptionCreator) {
return isTrue(conditions, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> TPropertyValidator isTrue(
Collection<Predicate<? super TProperty>> conditions,
Function<TProperty, E> exceptionCreator) {
for (Predicate<? super TProperty> condition : conditions) {
withRule(condition, exceptionCreator);
}
return thisObject();
}
static <V> Function<V, IllegalArgumentException> convertExceptionCreator(String errMsg) {
return value -> new IllegalArgumentException(errMsg);
}
static <V, E extends RuntimeException> Function<V, E> convertExceptionCreator(Supplier<E> exceptionSupplier) {
return value -> exceptionSupplier.get();
}
}

View File

@@ -0,0 +1,99 @@
package xyz.zhouxy.plusone.validator;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* BaseValidator
*
* <p>
* 校验器的基类
* </p>
*
* <p>
* <b>NOTE: content.</b>
* </p>
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
* @since 0.0.1
*/
public abstract class BaseValidator<T> {
private final List<Consumer<? super T>> rules = new ArrayList<>();
protected void withRule(final Predicate<? super T> rule, final String errorMessage) {
withRule(rule, () -> new IllegalArgumentException(errorMessage));
}
protected <E extends RuntimeException> void withRule(Predicate<? super T> rule, Supplier<E> exceptionBuilder) {
withRule(rule, value -> exceptionBuilder.get());
}
protected <E extends RuntimeException> void withRule(
Predicate<? super T> condition, Function<T, E> exceptionBuilder) {
withRule(value -> {
if (!condition.test(value)) {
throw exceptionBuilder.apply(value);
}
});
}
protected void withRule(Consumer<? super T> rule) {
this.rules.add(rule);
}
protected final <R> ObjectValidator<T, R> ruleFor(Function<T, R> getter) {
ObjectValidator<T, R> validator = new ObjectValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
protected final <R extends Comparable<R>> DefaultValidatorOfComparable<T, R> ruleForComparable(Function<T, R> getter) {
DefaultValidatorOfComparable<T, R> validator = new DefaultValidatorOfComparable<>(getter);
this.rules.add(validator::validate);
return validator;
}
protected final IntValidator<T> ruleForInt(Function<T, Integer> getter) {
IntValidator<T> validator = new IntValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
protected final LongValidator<T> ruleForLong(Function<T, Long> getter) {
LongValidator<T> validator = new LongValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
protected final DoubleValidator<T> ruleForDouble(Function<T, Double> getter) {
DoubleValidator<T> validator = new DoubleValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
protected final BoolValidator<T> ruleForBool(Function<T, Boolean> getter) {
BoolValidator<T> validator = new BoolValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
protected final StringValidator<T> ruleForString(Function<T, String> getter) {
StringValidator<T> validator = new StringValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
protected final <E> CollectionValidator<T, E> ruleForCollection(Function<T, Collection<E>> getter) {
CollectionValidator<T, E> validator = new CollectionValidator<>(getter);
this.rules.add(validator::validate);
return validator;
}
public void validate(T obj) {
this.rules.forEach(rule -> rule.accept(obj));
}
}

View File

@@ -0,0 +1,56 @@
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
public class BoolValidator<DTO> extends BasePropertyValidator<DTO, Boolean, BoolValidator<DTO>> {
BoolValidator(Function<DTO, Boolean> getter) {
super(getter);
}
// ====== isTrue ======
public BoolValidator<DTO> isTrue() {
return isTrue("The value must be true.");
}
public BoolValidator<DTO> isTrue(String errMsg) {
return isTrue(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> BoolValidator<DTO> isTrue(Supplier<E> exceptionCreator) {
return isTrue(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> BoolValidator<DTO> isTrue(
Function<Boolean, E> exceptionCreator) {
withRule(Boolean.TRUE::equals, exceptionCreator);
return this;
}
// ====== isFalse ======
public BoolValidator<DTO> isFalse() {
return isFalse("The value must be false.");
}
public BoolValidator<DTO> isFalse(String errMsg) {
return isFalse(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> BoolValidator<DTO> isFalse(Supplier<E> exceptionCreator) {
return isFalse(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> BoolValidator<DTO> isFalse(
Function<Boolean, E> exceptionCreator) {
withRule(Boolean.FALSE::equals, exceptionCreator);
return this;
}
@Override
protected BoolValidator<DTO> thisObject() {
return this;
}
}

View File

@@ -0,0 +1,52 @@
package xyz.zhouxy.plusone.validator;
import java.util.Collection;
import java.util.function.Function;
import java.util.function.Supplier;
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
public class CollectionValidator<DTO, T>
extends BasePropertyValidator<DTO, Collection<T>, CollectionValidator<DTO, T>> {
CollectionValidator(Function<DTO, Collection<T>> getter) {
super(getter);
}
// ====== notEmpty =====
public CollectionValidator<DTO, T> notEmpty(String errMsg) {
return notEmpty(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> CollectionValidator<DTO, T> notEmpty(Supplier<E> exceptionCreator) {
return notEmpty(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> CollectionValidator<DTO, T> notEmpty(
Function<Collection<T>, E> exceptionCreator) {
withRule(CollectionTools::isNotEmpty, exceptionCreator);
return this;
}
// ====== isEmpty =====
public CollectionValidator<DTO, T> isEmpty(String errMsg) {
return isEmpty(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> CollectionValidator<DTO, T> isEmpty(Supplier<E> exceptionCreator) {
return isEmpty(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> CollectionValidator<DTO, T> isEmpty(
Function<Collection<T>, E> exceptionCreator) {
withRule(CollectionTools::isEmpty, exceptionCreator);
return this;
}
@Override
protected CollectionValidator<DTO, T> thisObject() {
return this;
}
}

View File

@@ -0,0 +1,18 @@
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
public class DefaultValidatorOfComparable<
TObj,
TProperty extends Comparable<TProperty>
> extends ValidatorOfComparable<TObj, TProperty, DefaultValidatorOfComparable<TObj, TProperty>> {
DefaultValidatorOfComparable(Function<TObj, ? extends TProperty> getter) {
super(getter);
}
@Override
protected DefaultValidatorOfComparable<TObj, TProperty> thisObject() {
return this;
}
}

View File

@@ -0,0 +1,35 @@
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
public class DoubleValidator<DTO> extends ValidatorOfComparable<DTO, Double, DoubleValidator<DTO>> {
DoubleValidator(Function<DTO, Double> getter) {
super(getter);
}
public DoubleValidator<DTO> between(double min, double max) {
return between(min, max, String.format("数值不在 %s 和 %s 之间", String.valueOf(min), String.valueOf(max)));
}
public DoubleValidator<DTO> between(double min, double max, String errMsg) {
return between(min, max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> DoubleValidator<DTO> between(double min, double max,
Supplier<E> exceptionCreator) {
return between(min, max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> DoubleValidator<DTO> between(double min, double max,
Function<Double, E> exceptionCreator) {
withRule(value -> (value >= min && value < max), exceptionCreator);
return this;
}
@Override
protected DoubleValidator<DTO> thisObject() {
return this;
}
}

View File

@@ -0,0 +1,13 @@
package xyz.zhouxy.plusone.validator;
/**
* 自带校验方法,校验不通过时直接抛异常。
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*
* @see ValidTools
* @see BaseValidator
*/
public interface IValidateRequired {
void validate();
}

View File

@@ -0,0 +1,35 @@
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
public class IntValidator<DTO> extends ValidatorOfComparable<DTO, Integer, IntValidator<DTO>> {
IntValidator(Function<DTO, Integer> getter) {
super(getter);
}
public IntValidator<DTO> between(int min, int max) {
return between(min, max, String.format("数值不在 %d 和 %d 之间", min, max));
}
public IntValidator<DTO> between(int min, int max, String errMsg) {
return between(min, max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> IntValidator<DTO> between(int min, int max,
Supplier<E> exceptionCreator) {
return between(min, max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> IntValidator<DTO> between(int min, int max,
Function<Integer, E> exceptionCreator) {
withRule(value -> (value >= min && value < max), exceptionCreator);
return this;
}
@Override
protected IntValidator<DTO> thisObject() {
return this;
}
}

View File

@@ -0,0 +1,35 @@
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
public class LongValidator<DTO> extends ValidatorOfComparable<DTO, Long, LongValidator<DTO>> {
LongValidator(Function<DTO, Long> getter) {
super(getter);
}
public LongValidator<DTO> between(long min, long max) {
return between(min, max, String.format("数值不在 %d 和 %d 之间", min, max));
}
public LongValidator<DTO> between(long min, long max, String errMsg) {
return between(min, max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> LongValidator<DTO> between(long min, long max,
Supplier<E> exceptionCreator) {
return between(min, max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> LongValidator<DTO> between(long min, long max,
Function<Long, E> exceptionCreator) {
withRule(value -> (value >= min && value < max), exceptionCreator);
return this;
}
@Override
protected LongValidator<DTO> thisObject() {
return this;
}
}

View File

@@ -0,0 +1,75 @@
package xyz.zhouxy.plusone.validator;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
public abstract class MapValidator<K, V> extends BaseValidator<Map<K, V>> {
private final Set<K> keys;
protected MapValidator(K[] keys) {
this(Arrays.asList(keys));
}
protected MapValidator(Collection<K> keys) {
this.keys = keys.stream().collect(Collectors.toSet());
}
// ========== validate & validateAndCopy ==========
public final Map<K, V> validateAndCopy(Map<K, V> obj) {
return validateAndCopyInternal(obj, this.keys);
}
public final Map<K, V> validateAndCopy(Map<K, V> obj, Collection<K> keys) {
return validateAndCopyInternal(obj, keys);
}
@SafeVarargs
public final Map<K, V> validateAndCopy(Map<K, V> obj, K... keys) {
return validateAndCopyInternal(obj, Arrays.asList(keys));
}
private final Map<K, V> validateAndCopyInternal(Map<K, V> obj, Collection<K> keys) {
validate(obj);
return obj.entrySet().stream()
.filter(kv -> keys.contains(kv.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
// ========== ruleFor ==========
protected final ObjectValidator<Map<K, V>, V> ruleFor(K key) {
return ruleFor(m -> m.get(key));
}
protected final IntValidator<Map<K, V>> ruleForInt(K key) {
return ruleForInt(m -> (Integer) m.get(key));
}
protected final LongValidator<Map<K, V>> ruleForLong(K key) {
return ruleForLong(m -> (Long) m.get(key));
}
protected final DoubleValidator<Map<K, V>> ruleForDouble(K key) {
return ruleForDouble(m -> (Double) m.get(key));
}
protected final BoolValidator<Map<K, V>> ruleForBool(K key) {
return ruleForBool(m -> (Boolean) m.get(key));
}
protected final StringValidator<Map<K, V>> ruleForString(K key) {
return ruleForString(m -> (String) m.get(key));
}
protected final <E> CollectionValidator<Map<K, V>, E> ruleForCollection(K key) {
@SuppressWarnings("unchecked")
Function<Map<K, V>, Collection<E>> getter = m -> (Collection<E>) m.get(key);
return ruleForCollection(getter);
}
}

View File

@@ -0,0 +1,15 @@
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
public class ObjectValidator<DTO, T> extends BasePropertyValidator<DTO, T, ObjectValidator<DTO, T>> {
ObjectValidator(Function<DTO, T> getter) {
super(getter);
}
@Override
protected ObjectValidator<DTO, T> thisObject() {
return this;
}
}

View File

@@ -0,0 +1,286 @@
package xyz.zhouxy.plusone.validator;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.commons.util.AssertTools;
import xyz.zhouxy.plusone.commons.util.RegexTools;
import xyz.zhouxy.plusone.commons.util.StringTools;
/**
* StringValidator
*
* <p>
* 针对文本字段的验证器。
* </p>
*
* @author <a href="http://zhouxy.xyz:3000/ZhouXY108">ZhouXY</a>
*/
public class StringValidator<DTO> extends ValidatorOfComparable<DTO, String, StringValidator<DTO>> {
StringValidator(Function<DTO, String> getter) {
super(getter);
}
// ================================
// #region - matches
// ================================
public StringValidator<DTO> matches(Pattern regex, String errMsg) {
return matches(regex, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringValidator<DTO> matches(
Pattern regex,
Supplier<E> exceptionCreator) {
return matches(regex, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringValidator<DTO> matches(
Pattern regex,
Function<String, E> exceptionCreator) {
withRule(input -> RegexTools.matches(input, regex), exceptionCreator);
return this;
}
// ================================
// #endregion - matches
// ================================
// ================================
// #region - matchesOne
// ================================
public StringValidator<DTO> matchesOne(Pattern[] regexs, String errMsg) {
return matchesOne(regexs, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringValidator<DTO> matchesOne(
Pattern[] regexs,
Supplier<E> exceptionCreator) {
return matchesOne(regexs, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringValidator<DTO> matchesOne(
Pattern[] regexs,
Function<String, E> exceptionCreator) {
withRule(input -> RegexTools.matchesOne(input, regexs), exceptionCreator);
return this;
}
public StringValidator<DTO> matchesOne(List<Pattern> regexs, String errMsg) {
return matchesOne(regexs, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringValidator<DTO> matchesOne(
List<Pattern> regexs,
Supplier<E> exceptionCreator) {
return matchesOne(regexs, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringValidator<DTO> matchesOne(
List<Pattern> regexs,
Function<String, E> exceptionCreator) {
withRule(input -> RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
return this;
}
// ================================
// #endregion - matchesOne
// ================================
// ================================
// #region - matchesAll
// ================================
public StringValidator<DTO> matchesAll(Pattern[] regexs, String errMsg) {
return matchesAll(regexs, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringValidator<DTO> matchesAll(
Pattern[] regexs,
Supplier<E> exceptionCreator) {
return matchesAll(regexs, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringValidator<DTO> matchesAll(
Pattern[] regexs,
Function<String, E> exceptionCreator) {
withRule(input -> RegexTools.matchesAll(input, regexs), exceptionCreator);
return this;
}
public StringValidator<DTO> matchesAll(Collection<Pattern> regexs, String errMsg) {
return matchesAll(regexs, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringValidator<DTO> matchesAll(
Collection<Pattern> regexs,
Supplier<E> exceptionCreator) {
return matchesAll(regexs, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringValidator<DTO> matchesAll(
Collection<Pattern> regexs,
Function<String, E> exceptionCreator) {
withRule(input -> RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
return this;
}
// ================================
// #endregion - matchesAll
// ================================
// ================================
// #region - notBlank
// ================================
public StringValidator<DTO> notBlank() {
return notBlank("This String argument must have text; it must not be null, empty, or blank");
}
public StringValidator<DTO> notBlank(String errMsg) {
return notBlank(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringValidator<DTO> notBlank(Supplier<E> exceptionCreator) {
return notBlank(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringValidator<DTO> notBlank(
Function<String, E> exceptionCreator) {
withRule(StringTools::isNotBlank, exceptionCreator);
return this;
}
// ================================
// #endregion - notBlank
// ================================
// ================================
// #region - email
// ================================
public StringValidator<DTO> email() {
return email("The value is not an email address.");
}
public StringValidator<DTO> email(String errMsg) {
return email(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringValidator<DTO> email(Supplier<E> exceptionCreator) {
return email(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringValidator<DTO> email(Function<String, E> exceptionCreator) {
// TODO [优化] 优化 email 校验
return matches(PatternConsts.EMAIL, exceptionCreator);
}
// ================================
// #endregion - email
// ================================
// ================================
// #region - notEmpty
// ================================
public StringValidator<DTO> notEmpty(String errMsg) {
return notEmpty(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringValidator<DTO> notEmpty(Supplier<E> exceptionCreator) {
return notEmpty(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringValidator<DTO> notEmpty(
Function<String, E> exceptionCreator) {
withRule(s -> s != null && !s.isEmpty(), exceptionCreator);
return this;
}
// ================================
// #endregion - notEmpty
// ================================
// ================================
// #region - isNullOrEmpty
// ================================
public StringValidator<DTO> isNullOrEmpty(String errMsg) {
return isNullOrEmpty(convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringValidator<DTO> isNullOrEmpty(Supplier<E> exceptionCreator) {
return isNullOrEmpty(convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringValidator<DTO> isNullOrEmpty(
Function<String, E> exceptionCreator) {
withRule(s -> s == null || s.isEmpty(), exceptionCreator);
return this;
}
// ================================
// #endregion - isNullOrEmpty
// ================================
// ================================
// #region - length
// ================================
public StringValidator<DTO> length(int length, String errMsg) {
return length(length, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringValidator<DTO> length(int length,
Supplier<E> exceptionCreator) {
return length(length, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringValidator<DTO> length(int length,
Function<String, E> exceptionCreator) {
AssertTools.checkArgument(length >= 0, "The minimum value must be less than the maximum value.");
withRule(s -> s != null && s.length() == length, exceptionCreator);
return this;
}
static boolean length(String str, int min, int max) {
if (str == null) {
return false;
}
final int len = str.length();
return len >= min && len < max;
}
public StringValidator<DTO> length(int min, int max, String errMsg) {
return length(min, max, convertExceptionCreator(errMsg));
}
public <E extends RuntimeException> StringValidator<DTO> length(int min, int max,
Supplier<E> exceptionCreator) {
return length(min, max, convertExceptionCreator(exceptionCreator));
}
public <E extends RuntimeException> StringValidator<DTO> length(int min, int max,
Function<String, E> exceptionCreator) {
AssertTools.checkArgument(min >= 0, "The minimum value must be greater than equal to 0.");
AssertTools.checkArgument(min < max, "The minimum value must be less than the maximum value.");
withRule(s -> length(s, min, max), exceptionCreator);
return this;
}
// ================================
// #endregion - length
// ================================
@Override
protected StringValidator<DTO> thisObject() {
return this;
}
}

View File

@@ -0,0 +1,29 @@
package xyz.zhouxy.plusone.validator;
/**
* 校验工具类
* <p>
* 对 {@link IValidateRequired} 的实现类对象进行校验
* </p>
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
*
* @see BaseValidator
* @see Validator
* @see IValidateRequired
*/
public class ValidTools {
private ValidTools() {
throw new IllegalStateException("Utility class");
}
public static void validate(Object obj) {
if (obj instanceof IValidateRequired) {
((IValidateRequired) obj).validate();
}
}
public static <T> void validate(T obj, BaseValidator<T> validator) {
validator.validate(obj);
}
}

View File

@@ -0,0 +1,60 @@
package xyz.zhouxy.plusone.validator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* 校验器
*
* <p>
* 可以使用以下方式初始化一个校验器:
* </p>
*
* <pre>
* var validator = new Validator&lt;Integer&gt;()
* .addRule(value -> Objects.nonNull(value), "value 不能为空")
* .addRule(value -> (value >= 0 && value <= 500), "value 应在 [0, 500] 内");
* </pre>
*
* <p>
* 然后通过校验器的 {@link #validate} 方法,或
* {@link ValidTools#validate(Object, Validator)} 对指定对象进行校验。
* </p>
*
* <pre>
* validator.validate(666);
* </pre>
*
* <pre>
* ValidTools.validate(255, validator);
* </pre>
* </p>
*
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
* @see IValidateRequired
* @see ValidTools
* @see BaseValidator
*/
public final class Validator<T> extends BaseValidator<T> {
public final Validator<T> addRule(final Predicate<? super T> rule, final String errorMessage) {
withRule(rule, errorMessage);
return this;
}
public final <E extends RuntimeException> Validator<T> addRule(Predicate<? super T> rule, Supplier<E> exceptionCreator) {
withRule(rule, exceptionCreator);
return this;
}
public final <E extends RuntimeException> Validator<T> addRule(Predicate<? super T> rule, Function<T, E> exceptionCreator) {
withRule(rule, exceptionCreator);
return this;
}
public final Validator<T> addRule(Consumer<? super T> rule) {
withRule(rule);
return this;
}
}

View File

@@ -0,0 +1,49 @@
package xyz.zhouxy.plusone.validator;
import java.util.function.Function;
import java.util.function.Supplier;
import com.google.common.collect.Range;
public abstract
class ValidatorOfComparable<TObj,
TProperty extends Comparable<TProperty>,
TPropertyValidator extends ValidatorOfComparable<TObj, TProperty, TPropertyValidator>>
extends BasePropertyValidator<TObj, TProperty, TPropertyValidator> {
ValidatorOfComparable(Function<TObj, ? extends TProperty> getter) {
super(getter);
}
public TPropertyValidator between(Range<TProperty> range) {
withRule(range::contains, convertExceptionCreator("The value is not in " + range.toString()));
return thisObject();
}
public TPropertyValidator between(Range<TProperty> range, String errMsg) {
withRule(range::contains, convertExceptionCreator(errMsg));
return thisObject();
}
public <E extends RuntimeException> TPropertyValidator between(
Range<TProperty> range,
Supplier<E> exceptionCreator) {
withRule(range::contains, exceptionCreator);
return thisObject();
}
public <E extends RuntimeException> TPropertyValidator between(
Range<TProperty> range,
Function<TProperty, E> exceptionCreator) {
withRule(range::contains, exceptionCreator);
return thisObject();
}
static <V> Function<V, IllegalArgumentException> convertExceptionCreator(String errMsg) {
return value -> new IllegalArgumentException(errMsg);
}
static <V, E extends RuntimeException> Function<V, E> convertExceptionCreator(Supplier<E> exceptionSupplier) {
return value -> exceptionSupplier.get();
}
}

View File

@@ -0,0 +1,86 @@
package xyz.zhouxy.plusone.validator.map.test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;
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;
@Test
void testValidateAndCopy() {
Map<String, Object> params = new HashMap<>();
params.put(ParamsValidator.USERNAME, "ZhouXY");
params.put(ParamsValidator.ACCOUNT, "zhouxy@code108.cn");
params.put(ParamsValidator.PASSWORD, "99Code108");
params.put(ParamsValidator.PASSWORD2, "99Code108");
params.put(ParamsValidator.AGE, 18);
params.put(ParamsValidator.BOOLEAN, true);
params.put(ParamsValidator.ROLE_LIST, Arrays.asList("admin", ""));
assertThrows(IllegalArgumentException.class, () -> {
validator.validateAndCopy(params);
});
params.put(ParamsValidator.ROLE_LIST, Arrays.asList("admin", "developer"));
Map<String, Object> validatedParams = validator.validateAndCopy(params);
System.out.println(validatedParams);
}
}
class ParamsValidator extends MapValidator<String, Object> {
public static final String USERNAME = "username";
public static final String ACCOUNT = "account";
public static final String PASSWORD = "password";
public static final String PASSWORD2 = "password2";
public static final String AGE = "age";
public static final String BOOLEAN = "boolean";
public static final String ROLE_LIST = "roleList";
public static final ParamsValidator INSTANCE = new ParamsValidator();
private ParamsValidator() {
super(new String[] { USERNAME, ACCOUNT, PASSWORD, AGE, BOOLEAN, ROLE_LIST });
ruleForString(USERNAME)
.notBlank("用户名不能为空")
.matches(PatternConsts.USERNAME,
username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username)));
ruleForString(ACCOUNT)
.notBlank("账号不能为空")
.matchesOne(new Pattern[] { PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE }, "请输入正确的邮箱地址或手机号");
ruleForString(PASSWORD)
.notEmpty("密码不能为空")
.matches(PatternConsts.PASSWORD, "密码不符合规范");
// 校验到多个属性,只能针对 map 本身进行校验
withRule(m -> Objects.equals(m.get(PASSWORD), m.get(PASSWORD2)),
"两次输入的密码不一样!");
ruleForInt(AGE)
.withRule(Objects::nonNull)
.between(18, 61);
ruleForBool(BOOLEAN)
.notNull("Boolean property could not be null.")
.isTrue("Boolean property must be true.");
this.<String>ruleForCollection(ROLE_LIST)
.notEmpty("角色列表不能为空!")
.withRule(l -> l.stream().allMatch(StringTools::isNotBlank),
() -> new IllegalArgumentException("角色标识不能为空!"));
}
}

View File

@@ -0,0 +1,169 @@
package xyz.zhouxy.plusone.validator.test;
import java.time.Year;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.junit.jupiter.api.Test;
import com.google.common.collect.Range;
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
import xyz.zhouxy.plusone.commons.function.PredicateTools;
import xyz.zhouxy.plusone.commons.util.RegexTools;
import xyz.zhouxy.plusone.commons.util.StringTools;
import xyz.zhouxy.plusone.validator.BaseValidator;
import xyz.zhouxy.plusone.validator.ValidTools;
class BaseValidatorTest {
@Test
void testValidate() {
RegisterCommand registerCommand = new RegisterCommand("zhouxy108", "luquanlion@outlook.com", "22336",
"A1b2C3d4",
"A1b2C3d4",
Arrays.asList(new String[] { "admin", "editor" }),
2000);
RegisterCommandValidator.INSTANCE.validate(registerCommand);
ValidTools.validate(registerCommand, RegisterCommandValidator.INSTANCE);
System.out.println(registerCommand);
}
static class RegisterCommandValidator extends BaseValidator<RegisterCommand> {
static final RegisterCommandValidator INSTANCE = new RegisterCommandValidator();
private RegisterCommandValidator() {
int thisYear = Year.now().getValue();
ruleForString(RegisterCommand::getUsername)
.isTrue(PredicateTools.<String>from(Objects::nonNull)
.and(StringTools::isNotEmpty)
.and(StringTools::isNotBlank)
.and(username -> RegexTools.matches(username, PatternConsts.USERNAME)),
username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username)));
ruleForString(RegisterCommand::getAccount)
.notNull("请输入邮箱地址或手机号")
.matchesOne(Arrays.asList(PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE), "请输入邮箱地址或手机号");
ruleForString(RegisterCommand::getCode)
.notNull("验证码不能为空")
.matches(PatternConsts.CAPTCHA, "验证码不符合规范");
ruleForString(RegisterCommand::getPassword)
.notEmpty("密码不能为空")
.matches(PatternConsts.PASSWORD, "密码不符合规范");
ruleForCollection(RegisterCommand::getRoles)
.notEmpty(() -> new RuntimeException("角色列表不能为空"));
ruleForComparable(RegisterCommand::getYearOfBirth)
.notNull()
.between(Range.closed(thisYear - 60, thisYear - 18));
ruleForInt(RegisterCommand::getYearOfBirth)
.notNull()
.between(Range.closed(thisYear - 60, thisYear - 18));
withRule(registerCommand -> Objects.equals(registerCommand.getPassword(), registerCommand.getPassword2()),
"两次输入的密码不一致");
}
}
/**
* RegisterCommand
*/
static class RegisterCommand {
private String username;
private String account;
private String code;
private String password;
private String password2;
private List<String> roles;
private Integer yearOfBirth;
public RegisterCommand() {
}
public RegisterCommand(String username, String account, String code, String password, String password2,
List<String> roles, Integer yearOfBirth) {
this.username = username;
this.account = account;
this.code = code;
this.password = password;
this.password2 = password2;
this.roles = roles;
this.yearOfBirth = yearOfBirth;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword2() {
return password2;
}
public void setPassword2(String password2) {
this.password2 = password2;
}
public List<String> getRoles() {
return roles;
}
public void setRoles(List<String> roles) {
this.roles = roles;
}
public Integer getYearOfBirth() {
return yearOfBirth;
}
public void setYearOfBirth(Integer yearOfBirth) {
this.yearOfBirth = yearOfBirth;
}
@Override
public String toString() {
return new StringBuilder()
.append("RegisterCommand [")
.append("username=").append(username)
.append(", account=").append(account)
.append(", code=").append(code)
.append(", password=").append(password)
.append(", password2=").append(password2)
.append(", roles=").append(roles)
.append(", yearOfBirth=").append(yearOfBirth)
.append("]")
.toString();
}
}
}

View File

@@ -0,0 +1,146 @@
package xyz.zhouxy.plusone.validator.test;
import static xyz.zhouxy.plusone.commons.constant.PatternConsts.*;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
import xyz.zhouxy.plusone.commons.function.PredicateTools;
import xyz.zhouxy.plusone.commons.util.AssertTools;
import xyz.zhouxy.plusone.commons.util.RegexTools;
import xyz.zhouxy.plusone.commons.util.StringTools;
import xyz.zhouxy.plusone.validator.Validator;
class ValidatorTests {
@Test
void testValidate() {
RegisterCommand registerCommand = new RegisterCommand(
"null", "luquanlion@outlook.com", "22336",
"A1b2C3d4", "A1b2C3d4",
Arrays.asList(new String[] { "admin", "editor" }));
Validator<RegisterCommand> registerCommandValidator = new Validator<RegisterCommand>()
// 传入 predicate 和 Function<T, E extends RuntimeException>
.addRule(command -> {
String username = command.getUsername();
return Objects.nonNull(username)
&& StringTools.isNotEmpty(username)
&& StringTools.isNotBlank(username)
&& RegexTools.matches(username, USERNAME);
}, command -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", command.getUsername())))
// 传入 predicate 和 error message
.addRule(command -> PredicateTools
.<String>from(Objects::nonNull)
.and(account -> RegexTools.matchesOne(account, new Pattern[] { EMAIL, MOBILE_PHONE }))
.test(command.getAccount()),
"请输入邮箱地址或手机号")
// 传入 rule
.addRule(command -> {
String code = command.getCode();
AssertTools.checkArgument(Objects.nonNull(code), "验证码不能为空");
AssertTools.checkArgument(RegexTools.matches(code, CAPTCHA), "验证码不符合规范");
})
// 传入 rule
.addRule(command -> {
String password = command.getPassword();
AssertTools.checkArgument(StringTools.isNotEmpty(password), "密码不能为空");
AssertTools.checkArgument(RegexTools.matches(password, PASSWORD), "密码不符合规范");
})
// 传入 predicate 和 Supplier<E extends RuntimeException>
.addRule(command -> CollectionTools.isNotEmpty(command.getRoles()),
() -> new RuntimeException("角色列表不能为空"))
// 传入 predicate 和 error message
.addRule(command -> Objects.equals(command.getPassword(), command.getPassword2()),
"两次输入的密码不一致");
registerCommandValidator.validate(registerCommand);
System.out.println(registerCommand);
}
/**
* RegisterCommand
*/
static class RegisterCommand {
private String username;
private String account;
private String code;
private String password;
private String password2;
private List<String> roles;
public RegisterCommand() {
}
public RegisterCommand(String username, String account, String code, String password, String password2,
List<String> roles) {
this.username = username;
this.account = account;
this.code = code;
this.password = password;
this.password2 = password2;
this.roles = roles;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword2() {
return password2;
}
public void setPassword2(String password2) {
this.password2 = password2;
}
public List<String> getRoles() {
return roles;
}
public void setRoles(List<String> roles) {
this.roles = roles;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("RegisterCommand [username=").append(username).append(", account=").append(account)
.append(", code=").append(code).append(", password=").append(password).append(", password2=")
.append(password2).append(", roles=").append(roles).append("]");
return builder.toString();
}
}
}