0.1.4-SNAPSHOT #3
|
@ -0,0 +1,12 @@
|
||||||
|
# EditorConfig is awesome: https://EditorConfig.org
|
||||||
|
|
||||||
|
# top-most EditorConfig file
|
||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
||||||
|
end_of_line = lf
|
||||||
|
charset = utf-8
|
||||||
|
trim_trailing_whitespace = false
|
||||||
|
insert_final_newline = true
|
|
@ -1,4 +1,5 @@
|
||||||
{
|
{
|
||||||
"java.configuration.updateBuildConfiguration": "automatic",
|
"java.configuration.updateBuildConfiguration": "automatic",
|
||||||
"java.dependency.packagePresentation": "hierarchical"
|
"java.dependency.packagePresentation": "hierarchical",
|
||||||
|
"java.compile.nullAnalysis.mode": "automatic"
|
||||||
}
|
}
|
22
pom.xml
22
pom.xml
|
@ -1,12 +1,12 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?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"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
<groupId>xyz.zhouxy.plusone</groupId>
|
<groupId>xyz.zhouxy.plusone</groupId>
|
||||||
<artifactId>plusone-validator</artifactId>
|
<artifactId>plusone-validator</artifactId>
|
||||||
<version>0.1.2-SNAPSHOT</version>
|
<version>0.1.4-SNAPSHOT</version>
|
||||||
|
|
||||||
<name>plusone-validator</name>
|
<name>plusone-validator</name>
|
||||||
<url>http://zhouxy.xyz</url>
|
<url>http://zhouxy.xyz</url>
|
||||||
|
@ -15,6 +15,7 @@
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<commons-lang3.version>3.12.0</commons-lang3.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
@ -24,15 +25,22 @@
|
||||||
<version>0.1.0-SNAPSHOT</version>
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>commons-lang3</artifactId>
|
||||||
<version>4.11</version>
|
<version>${commons-lang3.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>5.9.2</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
|
<!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
|
||||||
|
<pluginManagement>
|
||||||
<plugins>
|
<plugins>
|
||||||
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
|
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
|
||||||
<plugin>
|
<plugin>
|
||||||
|
|
|
@ -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<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<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<TProperty> condition) {
|
||||||
|
return isTrue(condition, "无效的用户输入");
|
||||||
|
}
|
||||||
|
|
||||||
|
public TPropertyValidator isTrue(Predicate<TProperty> condition, String errMsg) {
|
||||||
|
return isTrue(condition, convertExceptionCreator(errMsg));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> TPropertyValidator isTrue(
|
||||||
|
Predicate<TProperty> condition,
|
||||||
|
Supplier<E> exceptionCreator) {
|
||||||
|
return isTrue(condition, convertExceptionCreator(exceptionCreator));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> TPropertyValidator isTrue(
|
||||||
|
Predicate<TProperty> condition,
|
||||||
|
Function<TProperty, E> exceptionCreator) {
|
||||||
|
withRule(condition, exceptionCreator);
|
||||||
|
return thisObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===== isTrue =====
|
||||||
|
|
||||||
|
public TPropertyValidator isTrue(Collection<Predicate<TProperty>> conditions) {
|
||||||
|
return isTrue(conditions, "无效的用户输入");
|
||||||
|
}
|
||||||
|
|
||||||
|
public TPropertyValidator isTrue(Collection<Predicate<TProperty>> conditions, String errMsg) {
|
||||||
|
return isTrue(conditions, convertExceptionCreator(errMsg));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> TPropertyValidator isTrue(
|
||||||
|
Collection<Predicate<TProperty>> conditions,
|
||||||
|
Supplier<E> exceptionCreator) {
|
||||||
|
return isTrue(conditions, convertExceptionCreator(exceptionCreator));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <E extends RuntimeException> TPropertyValidator isTrue(
|
||||||
|
Collection<Predicate<TProperty>> conditions,
|
||||||
|
Function<TProperty, E> exceptionCreator) {
|
||||||
|
for (Predicate<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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,18 +10,17 @@ import java.util.function.Supplier;
|
||||||
|
|
||||||
public class BaseValidator<T> {
|
public class BaseValidator<T> {
|
||||||
private final List<Consumer<T>> rules = new ArrayList<>();
|
private final List<Consumer<T>> rules = new ArrayList<>();
|
||||||
private final List<PropertyValidator<T, ?, ?>> propertyValidators = new ArrayList<>();
|
|
||||||
|
|
||||||
protected void withRule(final Predicate<T> rule, final String errorMessage) {
|
protected void withRule(final Predicate<T> rule, final String errorMessage) {
|
||||||
withRule(rule, value -> new InvalidInputException(errorMessage));
|
withRule(rule, () -> new IllegalArgumentException(errorMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <E extends RuntimeException> void withRule(Predicate<T> rule, Supplier<E> exceptionBuilder) {
|
protected <E extends RuntimeException> void withRule(Predicate<T> rule, Supplier<E> exceptionBuilder) {
|
||||||
withRule(rule, value -> exceptionBuilder.get());
|
withRule(rule, value -> exceptionBuilder.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected <E extends RuntimeException> void withRule(Predicate<T> condition,
|
protected <E extends RuntimeException> void withRule(
|
||||||
Function<T, E> exceptionBuilder) {
|
Predicate<T> condition, Function<T, E> exceptionBuilder) {
|
||||||
withRule(value -> {
|
withRule(value -> {
|
||||||
if (!condition.test(value)) {
|
if (!condition.test(value)) {
|
||||||
throw exceptionBuilder.apply(value);
|
throw exceptionBuilder.apply(value);
|
||||||
|
@ -34,47 +33,42 @@ public class BaseValidator<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final <R> ObjectValidator<T, R> ruleFor(Function<T, R> getter) {
|
protected final <R> ObjectValidator<T, R> ruleFor(Function<T, R> getter) {
|
||||||
ObjectValidator<T, R> validValueHolder = new ObjectValidator<>(getter);
|
ObjectValidator<T, R> validator = new ObjectValidator<>(getter);
|
||||||
propertyValidators.add(validValueHolder);
|
this.rules.add(validator::validate);
|
||||||
return validValueHolder;
|
return validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final IntValidator<T> ruleForInt(Function<T, Integer> getter) {
|
protected final IntValidator<T> ruleForInt(Function<T, Integer> getter) {
|
||||||
IntValidator<T> validValueHolder = new IntValidator<>(getter);
|
IntValidator<T> validator = new IntValidator<>(getter);
|
||||||
propertyValidators.add(validValueHolder);
|
this.rules.add(validator::validate);
|
||||||
return validValueHolder;
|
return validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final DoubleValidator<T> ruleForDouble(Function<T, Double> getter) {
|
protected final DoubleValidator<T> ruleForDouble(Function<T, Double> getter) {
|
||||||
DoubleValidator<T> validValueHolder = new DoubleValidator<>(getter);
|
DoubleValidator<T> validator = new DoubleValidator<>(getter);
|
||||||
propertyValidators.add(validValueHolder);
|
this.rules.add(validator::validate);
|
||||||
return validValueHolder;
|
return validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final BoolValidator<T> ruleForBool(Function<T, Boolean> getter) {
|
protected final BoolValidator<T> ruleForBool(Function<T, Boolean> getter) {
|
||||||
BoolValidator<T> validValueHolder = new BoolValidator<>(getter);
|
BoolValidator<T> validator = new BoolValidator<>(getter);
|
||||||
propertyValidators.add(validValueHolder);
|
this.rules.add(validator::validate);
|
||||||
return validValueHolder;
|
return validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final StringValidator<T> ruleForString(Function<T, String> getter) {
|
protected final StringValidator<T> ruleForString(Function<T, String> getter) {
|
||||||
StringValidator<T> validValueHolder = new StringValidator<>(getter);
|
StringValidator<T> validator = new StringValidator<>(getter);
|
||||||
propertyValidators.add(validValueHolder);
|
this.rules.add(validator::validate);
|
||||||
return validValueHolder;
|
return validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected final <E> CollectionValidator<T, E> ruleForCollection(Function<T, Collection<E>> getter) {
|
protected final <E> CollectionValidator<T, E> ruleForCollection(Function<T, Collection<E>> getter) {
|
||||||
CollectionValidator<T, E> validValueHolder = new CollectionValidator<>(getter);
|
CollectionValidator<T, E> validator = new CollectionValidator<>(getter);
|
||||||
propertyValidators.add(validValueHolder);
|
this.rules.add(validator::validate);
|
||||||
return validValueHolder;
|
return validator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void validate(T obj) {
|
public void validate(T obj) {
|
||||||
for (Consumer<T> rule : this.rules) {
|
this.rules.forEach(rule -> rule.accept(obj));
|
||||||
rule.accept(obj);
|
|
||||||
}
|
|
||||||
for (PropertyValidator<T, ?, ?> valueValidator : this.propertyValidators) {
|
|
||||||
valueValidator.validate(obj);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,7 @@ 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 BoolValidator<DTO> extends PropertyValidator<DTO, Boolean, BoolValidator<DTO>> {
|
public class BoolValidator<DTO> extends BasePropertyValidator<DTO, Boolean, BoolValidator<DTO>> {
|
||||||
|
|
||||||
BoolValidator(Function<DTO, Boolean> getter) {
|
BoolValidator(Function<DTO, Boolean> getter) {
|
||||||
super(getter);
|
super(getter);
|
||||||
|
|
|
@ -4,7 +4,10 @@ import java.util.Collection;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public class CollectionValidator<DTO, T> extends PropertyValidator<DTO, Collection<T>, CollectionValidator<DTO, T>> {
|
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) {
|
CollectionValidator(Function<DTO, Collection<T>> getter) {
|
||||||
super(getter);
|
super(getter);
|
||||||
|
@ -22,12 +25,7 @@ public class CollectionValidator<DTO, T> extends PropertyValidator<DTO, Collecti
|
||||||
|
|
||||||
public <E extends RuntimeException> CollectionValidator<DTO, T> notEmpty(
|
public <E extends RuntimeException> CollectionValidator<DTO, T> notEmpty(
|
||||||
Function<Collection<T>, E> exceptionCreator) {
|
Function<Collection<T>, E> exceptionCreator) {
|
||||||
withRule(value -> {
|
withRule(CollectionTools::isNotEmpty, exceptionCreator);
|
||||||
if (value == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return !((Collection<?>) value).isEmpty();
|
|
||||||
}, exceptionCreator);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,12 +41,7 @@ public class CollectionValidator<DTO, T> extends PropertyValidator<DTO, Collecti
|
||||||
|
|
||||||
public <E extends RuntimeException> CollectionValidator<DTO, T> isEmpty(
|
public <E extends RuntimeException> CollectionValidator<DTO, T> isEmpty(
|
||||||
Function<Collection<T>, E> exceptionCreator) {
|
Function<Collection<T>, E> exceptionCreator) {
|
||||||
withRule(value -> {
|
withRule(CollectionTools::isEmpty, exceptionCreator);
|
||||||
if (value == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return ((Collection<?>) value).isEmpty();
|
|
||||||
}, exceptionCreator);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ 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 DoubleValidator<DTO> extends PropertyValidator<DTO, Double, DoubleValidator<DTO>> {
|
public class DoubleValidator<DTO> extends BasePropertyValidator<DTO, Double, DoubleValidator<DTO>> {
|
||||||
|
|
||||||
DoubleValidator(Function<DTO, Double> getter) {
|
DoubleValidator(Function<DTO, Double> getter) {
|
||||||
super(getter);
|
super(getter);
|
||||||
|
|
|
@ -3,14 +3,14 @@ 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 IntValidator<DTO> extends PropertyValidator<DTO, Integer, IntValidator<DTO>> {
|
public class IntValidator<DTO> extends BasePropertyValidator<DTO, Integer, IntValidator<DTO>> {
|
||||||
|
|
||||||
IntValidator(Function<DTO, Integer> getter) {
|
IntValidator(Function<DTO, Integer> getter) {
|
||||||
super(getter);
|
super(getter);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IntValidator<DTO> between(int min, int max) {
|
public IntValidator<DTO> between(int min, int max) {
|
||||||
return between(min, max, String.format("数值不在 %s 和 %s 之间", String.valueOf(min), String.valueOf(max)));
|
return between(min, max, String.format("数值不在 %d 和 %d 之间", min, max));
|
||||||
}
|
}
|
||||||
|
|
||||||
public IntValidator<DTO> between(int min, int max, String errMsg) {
|
public IntValidator<DTO> between(int min, int max, String errMsg) {
|
||||||
|
|
|
@ -1,53 +0,0 @@
|
||||||
package xyz.zhouxy.plusone.validator;
|
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.exception.BaseException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 4040200 - 无效的用户输入
|
|
||||||
*
|
|
||||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
|
||||||
*/
|
|
||||||
public class InvalidInputException extends BaseException {
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 7956661913360059670L;
|
|
||||||
|
|
||||||
public static final int ERROR_CODE = 4040200;
|
|
||||||
|
|
||||||
private InvalidInputException(int code, String msg) {
|
|
||||||
super(code, msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
private InvalidInputException(int code, Throwable cause) {
|
|
||||||
super(code, cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
private InvalidInputException(int code, String msg, Throwable cause) {
|
|
||||||
super(code, msg, cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
public InvalidInputException(String msg) {
|
|
||||||
this(ERROR_CODE, msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
public InvalidInputException(Throwable cause) {
|
|
||||||
this(ERROR_CODE, cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
public InvalidInputException(String msg, Throwable cause) {
|
|
||||||
this(ERROR_CODE, msg, cause);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 不支持的 Principal 类型出现时抛出的异常
|
|
||||||
*/
|
|
||||||
public static InvalidInputException unsupportedPrincipalTypeException() {
|
|
||||||
return unsupportedPrincipalTypeException("不支持的 PrincipalType");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 不支持的 Principal 类型出现时抛出的异常
|
|
||||||
*/
|
|
||||||
public static InvalidInputException unsupportedPrincipalTypeException(String message) {
|
|
||||||
return new InvalidInputException(4040201, message);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2,7 +2,7 @@ package xyz.zhouxy.plusone.validator;
|
||||||
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
public class ObjectValidator<DTO, T> extends PropertyValidator<DTO, T, ObjectValidator<DTO, T>> {
|
public class ObjectValidator<DTO, T> extends BasePropertyValidator<DTO, T, ObjectValidator<DTO, T>> {
|
||||||
|
|
||||||
ObjectValidator(Function<DTO, T> getter) {
|
ObjectValidator(Function<DTO, T> getter) {
|
||||||
super(getter);
|
super(getter);
|
||||||
|
|
|
@ -1,155 +0,0 @@
|
||||||
package xyz.zhouxy.plusone.validator;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
import java.util.function.Function;
|
|
||||||
import java.util.function.Predicate;
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
abstract class PropertyValidator<DTO, PROPERTY, THIS> {
|
|
||||||
Function<DTO, PROPERTY> getter;
|
|
||||||
Validator<PROPERTY> validator = new Validator<>();
|
|
||||||
|
|
||||||
PropertyValidator(Function<DTO, PROPERTY> getter) {
|
|
||||||
this.getter = getter;
|
|
||||||
}
|
|
||||||
|
|
||||||
<E extends RuntimeException> void withRule(Predicate<PROPERTY> condition,
|
|
||||||
Function<PROPERTY, E> exceptionCreator) {
|
|
||||||
withRule(value -> {
|
|
||||||
if (!condition.test(value)) {
|
|
||||||
throw exceptionCreator.apply(value);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void withRule(Consumer<PROPERTY> rule) {
|
|
||||||
this.validator.addRule(rule);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ====================
|
|
||||||
// ====== Object ======
|
|
||||||
// ====================
|
|
||||||
|
|
||||||
// ====== notNull =====
|
|
||||||
|
|
||||||
public THIS notNull() {
|
|
||||||
return notNull("Value could not be null.");
|
|
||||||
}
|
|
||||||
|
|
||||||
public THIS notNull(String errMsg) {
|
|
||||||
return notNull(convertExceptionCreator(errMsg));
|
|
||||||
}
|
|
||||||
|
|
||||||
public <E extends RuntimeException> THIS notNull(Supplier<E> exceptionCreator) {
|
|
||||||
return notNull(convertExceptionCreator(exceptionCreator));
|
|
||||||
}
|
|
||||||
|
|
||||||
public <E extends RuntimeException> THIS notNull(Function<PROPERTY, E> exceptionCreator) {
|
|
||||||
withRule(Objects::nonNull, exceptionCreator);
|
|
||||||
return thisObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
// ====== isNull =====
|
|
||||||
|
|
||||||
public THIS isNull(String errMsg) {
|
|
||||||
return isNull(convertExceptionCreator(errMsg));
|
|
||||||
}
|
|
||||||
|
|
||||||
public <E extends RuntimeException> THIS isNull(Supplier<E> exceptionCreator) {
|
|
||||||
return isNull(convertExceptionCreator(exceptionCreator));
|
|
||||||
}
|
|
||||||
|
|
||||||
public <E extends RuntimeException> THIS isNull(Function<PROPERTY, E> exceptionCreator) {
|
|
||||||
withRule(Objects::isNull, exceptionCreator);
|
|
||||||
return thisObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
// ===== equals =====
|
|
||||||
|
|
||||||
public THIS equalsThat(Object that) {
|
|
||||||
return equalsThat(that, value -> new InvalidInputException(String.format("(%s) 必须与 (%s) 相等", value, that)));
|
|
||||||
}
|
|
||||||
|
|
||||||
public THIS equalsThat(Object that, String errMsg) {
|
|
||||||
return equalsThat(that, convertExceptionCreator(errMsg));
|
|
||||||
}
|
|
||||||
|
|
||||||
public <E extends RuntimeException> THIS equalsThat(
|
|
||||||
Object that, Supplier<E> exceptionCreator) {
|
|
||||||
return equalsThat(that, convertExceptionCreator(exceptionCreator));
|
|
||||||
}
|
|
||||||
|
|
||||||
public <E extends RuntimeException> THIS equalsThat(
|
|
||||||
Object that, Function<PROPERTY, E> exceptionCreator) {
|
|
||||||
withRule(value -> Objects.equals(value, that), exceptionCreator);
|
|
||||||
return thisObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
// ===== state =====
|
|
||||||
|
|
||||||
public THIS state(Predicate<PROPERTY> condition) {
|
|
||||||
return state(condition, "无效的用户输入");
|
|
||||||
}
|
|
||||||
|
|
||||||
public THIS state(Predicate<PROPERTY> condition, String errMsg) {
|
|
||||||
return state(condition, convertExceptionCreator(errMsg));
|
|
||||||
}
|
|
||||||
|
|
||||||
public <E extends RuntimeException> THIS state(
|
|
||||||
Predicate<PROPERTY> condition,
|
|
||||||
Supplier<E> exceptionCreator) {
|
|
||||||
return state(condition, convertExceptionCreator(exceptionCreator));
|
|
||||||
}
|
|
||||||
|
|
||||||
public <E extends RuntimeException> THIS state(
|
|
||||||
Predicate<PROPERTY> condition,
|
|
||||||
Function<PROPERTY, E> exceptionCreator) {
|
|
||||||
withRule(condition, exceptionCreator);
|
|
||||||
return thisObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
// ===== state =====
|
|
||||||
|
|
||||||
public THIS state(Collection<Predicate<PROPERTY>> conditions) {
|
|
||||||
return state(conditions, "无效的用户输入");
|
|
||||||
}
|
|
||||||
|
|
||||||
public THIS state(Collection<Predicate<PROPERTY>> conditions, String errMsg) {
|
|
||||||
return state(conditions, convertExceptionCreator(errMsg));
|
|
||||||
}
|
|
||||||
|
|
||||||
public <E extends RuntimeException> THIS state(
|
|
||||||
Collection<Predicate<PROPERTY>> conditions,
|
|
||||||
Supplier<E> exceptionCreator) {
|
|
||||||
return state(conditions, convertExceptionCreator(exceptionCreator));
|
|
||||||
}
|
|
||||||
|
|
||||||
public <E extends RuntimeException> THIS state(
|
|
||||||
Collection<Predicate<PROPERTY>> conditions,
|
|
||||||
Function<PROPERTY, E> exceptionCreator) {
|
|
||||||
for (Predicate<PROPERTY> condition : conditions) {
|
|
||||||
withRule(condition, exceptionCreator);
|
|
||||||
}
|
|
||||||
return thisObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
// ========================================================================
|
|
||||||
|
|
||||||
void validate(DTO obj) {
|
|
||||||
PROPERTY value = this.getter.apply(obj);
|
|
||||||
this.validator.validate(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
static <V> Function<V, InvalidInputException> convertExceptionCreator(String errMsg) {
|
|
||||||
return value -> new InvalidInputException(errMsg);
|
|
||||||
}
|
|
||||||
|
|
||||||
static <V, E extends RuntimeException> Function<V, E> convertExceptionCreator(
|
|
||||||
Supplier<E> exceptionSupplier) {
|
|
||||||
return value -> exceptionSupplier.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected abstract THIS thisObject();
|
|
||||||
}
|
|
|
@ -1,14 +1,17 @@
|
||||||
package xyz.zhouxy.plusone.validator;
|
package xyz.zhouxy.plusone.validator;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import com.google.common.base.Preconditions;
|
||||||
import xyz.zhouxy.plusone.constant.RegexConsts;
|
|
||||||
import xyz.zhouxy.plusone.util.RegexUtil;
|
|
||||||
|
|
||||||
public class StringValidator<DTO> extends PropertyValidator<DTO, String, StringValidator<DTO>> {
|
import xyz.zhouxy.plusone.commons.constant.PatternConsts;
|
||||||
|
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
||||||
|
|
||||||
|
public class StringValidator<DTO> extends BasePropertyValidator<DTO, String, StringValidator<DTO>> {
|
||||||
|
|
||||||
StringValidator(Function<DTO, String> getter) {
|
StringValidator(Function<DTO, String> getter) {
|
||||||
super(getter);
|
super(getter);
|
||||||
|
@ -20,80 +23,109 @@ public class StringValidator<DTO> extends PropertyValidator<DTO, String, StringV
|
||||||
|
|
||||||
// ===== matches =====
|
// ===== matches =====
|
||||||
|
|
||||||
public StringValidator<DTO> matches(String regex, String errMsg) {
|
public StringValidator<DTO> matches(Pattern regex, String errMsg) {
|
||||||
return matches(regex, convertExceptionCreator(errMsg));
|
return matches(regex, convertExceptionCreator(errMsg));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> matches(
|
public <E extends RuntimeException> StringValidator<DTO> matches(
|
||||||
String regex,
|
Pattern regex,
|
||||||
Supplier<E> exceptionCreator) {
|
Supplier<E> exceptionCreator) {
|
||||||
return matches(regex, convertExceptionCreator(exceptionCreator));
|
return matches(regex, convertExceptionCreator(exceptionCreator));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> matches(
|
public <E extends RuntimeException> StringValidator<DTO> matches(
|
||||||
String regex,
|
Pattern regex,
|
||||||
Function<String, E> exceptionCreator) {
|
Function<String, E> exceptionCreator) {
|
||||||
withRule(input -> RegexUtil.matches(input, regex), exceptionCreator);
|
withRule(input -> RegexTools.matches(input, regex), exceptionCreator);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== matchesOr =====
|
// ===== matchesOne =====
|
||||||
|
|
||||||
public StringValidator<DTO> matchesOr(String[] regexs, String errMsg) {
|
public StringValidator<DTO> matchesOne(Pattern[] regexs, String errMsg) {
|
||||||
return matchesOr(regexs, convertExceptionCreator(errMsg));
|
return matchesOne(regexs, convertExceptionCreator(errMsg));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> matchesOr(
|
public <E extends RuntimeException> StringValidator<DTO> matchesOne(
|
||||||
String[] regexs,
|
Pattern[] regexs,
|
||||||
Supplier<E> exceptionCreator) {
|
Supplier<E> exceptionCreator) {
|
||||||
return matchesOr(regexs, convertExceptionCreator(exceptionCreator));
|
return matchesOne(regexs, convertExceptionCreator(exceptionCreator));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> matchesOr(
|
public <E extends RuntimeException> StringValidator<DTO> matchesOne(
|
||||||
String[] regexs,
|
Pattern[] regexs,
|
||||||
Function<String, E> exceptionCreator) {
|
Function<String, E> exceptionCreator) {
|
||||||
withRule(input -> RegexUtil.matchesOr(input, regexs), exceptionCreator);
|
withRule(input -> RegexTools.matchesOne(input, regexs), exceptionCreator);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public StringValidator<DTO> matchesOr(List<String> regexs, String errMsg) {
|
public StringValidator<DTO> matchesOne(List<Pattern> regexs, String errMsg) {
|
||||||
return matchesOr(regexs, convertExceptionCreator(errMsg));
|
return matchesOne(regexs, convertExceptionCreator(errMsg));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> matchesOr(
|
public <E extends RuntimeException> StringValidator<DTO> matchesOne(
|
||||||
List<String> regexs,
|
List<Pattern> regexs,
|
||||||
Supplier<E> exceptionCreator) {
|
Supplier<E> exceptionCreator) {
|
||||||
return matchesOr(regexs, convertExceptionCreator(exceptionCreator));
|
return matchesOne(regexs, convertExceptionCreator(exceptionCreator));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> matchesOr(
|
public <E extends RuntimeException> StringValidator<DTO> matchesOne(
|
||||||
List<String> regexs,
|
List<Pattern> regexs,
|
||||||
Function<String, E> exceptionCreator) {
|
Function<String, E> exceptionCreator) {
|
||||||
withRule(input -> RegexUtil.matchesOr(input, regexs.toArray(new String[regexs.size()])), exceptionCreator);
|
withRule(input -> RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== matchesAnd =====
|
// ===== matchesAll =====
|
||||||
|
|
||||||
public StringValidator<DTO> matchesAnd(String[] regexs, String errMsg) {
|
public StringValidator<DTO> matchesAll(Pattern[] regexs, String errMsg) {
|
||||||
return matchesAnd(regexs, convertExceptionCreator(errMsg));
|
return matchesAll(regexs, convertExceptionCreator(errMsg));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> matchesAnd(
|
public <E extends RuntimeException> StringValidator<DTO> matchesAll(
|
||||||
String[] regexs,
|
Pattern[] regexs,
|
||||||
Supplier<E> exceptionCreator) {
|
Supplier<E> exceptionCreator) {
|
||||||
return matchesAnd(regexs, convertExceptionCreator(exceptionCreator));
|
return matchesAll(regexs, convertExceptionCreator(exceptionCreator));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> matchesAnd(
|
public <E extends RuntimeException> StringValidator<DTO> matchesAll(
|
||||||
String[] regexs,
|
Pattern[] regexs,
|
||||||
Function<String, E> exceptionCreator) {
|
Function<String, E> exceptionCreator) {
|
||||||
withRule(input -> RegexUtil.matchesAnd(input, regexs), 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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===== notBlank =====
|
// ===== notBlank =====
|
||||||
|
|
||||||
|
static boolean isNotBlank(final String cs) {
|
||||||
|
if (cs == null || cs.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < cs.length(); i++) {
|
||||||
|
if (!Character.isWhitespace(cs.charAt(i))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public StringValidator<DTO> notBlank() {
|
public StringValidator<DTO> notBlank() {
|
||||||
return notBlank("This String argument must have text; it must not be null, empty, or blank");
|
return notBlank("This String argument must have text; it must not be null, empty, or blank");
|
||||||
}
|
}
|
||||||
|
@ -108,7 +140,7 @@ public class StringValidator<DTO> extends PropertyValidator<DTO, String, StringV
|
||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> notBlank(
|
public <E extends RuntimeException> StringValidator<DTO> notBlank(
|
||||||
Function<String, E> exceptionCreator) {
|
Function<String, E> exceptionCreator) {
|
||||||
withRule(input -> StrUtil.isNotBlank(input), exceptionCreator);
|
withRule(StringValidator::isNotBlank, exceptionCreator);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,7 +159,7 @@ public class StringValidator<DTO> extends PropertyValidator<DTO, String, StringV
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> email(Function<String, E> exceptionCreator) {
|
public <E extends RuntimeException> StringValidator<DTO> email(Function<String, E> exceptionCreator) {
|
||||||
return matches(RegexConsts.EMAIL, exceptionCreator);
|
return matches(PatternConsts.EMAIL, exceptionCreator);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====== notEmpty =====
|
// ====== notEmpty =====
|
||||||
|
@ -142,33 +174,66 @@ public class StringValidator<DTO> extends PropertyValidator<DTO, String, StringV
|
||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> notEmpty(
|
public <E extends RuntimeException> StringValidator<DTO> notEmpty(
|
||||||
Function<String, E> exceptionCreator) {
|
Function<String, E> exceptionCreator) {
|
||||||
withRule(value -> {
|
withRule(s -> s != null && !s.isEmpty(), exceptionCreator);
|
||||||
if (value == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return !(value.isEmpty());
|
|
||||||
}, exceptionCreator);
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ====== isEmpty =====
|
// ====== isNullOrEmpty =====
|
||||||
|
|
||||||
public StringValidator<DTO> isEmpty(String errMsg) {
|
public StringValidator<DTO> isNullOrEmpty(String errMsg) {
|
||||||
return isEmpty(convertExceptionCreator(errMsg));
|
return isNullOrEmpty(convertExceptionCreator(errMsg));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> isEmpty(Supplier<E> exceptionCreator) {
|
public <E extends RuntimeException> StringValidator<DTO> isNullOrEmpty(Supplier<E> exceptionCreator) {
|
||||||
return isEmpty(convertExceptionCreator(exceptionCreator));
|
return isNullOrEmpty(convertExceptionCreator(exceptionCreator));
|
||||||
}
|
}
|
||||||
|
|
||||||
public <E extends RuntimeException> StringValidator<DTO> isEmpty(
|
public <E extends RuntimeException> StringValidator<DTO> isNullOrEmpty(
|
||||||
Function<String, E> exceptionCreator) {
|
Function<String, E> exceptionCreator) {
|
||||||
withRule(value -> {
|
withRule(s -> s == null || s.isEmpty(), exceptionCreator);
|
||||||
if (value == null) {
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ====== 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) {
|
||||||
|
Preconditions.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;
|
return false;
|
||||||
}
|
}
|
||||||
return value.isEmpty();
|
final int len = str.length();
|
||||||
}, exceptionCreator);
|
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) {
|
||||||
|
Preconditions.checkArgument(min >= 0, "The minimum value must be greater than equal to 0.");
|
||||||
|
Preconditions.checkArgument(min < max, "The minimum value must be less than the maximum value.");
|
||||||
|
withRule(s -> length(s, min, max), exceptionCreator);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,22 @@
|
||||||
|
package xyz.zhouxy.plusone.validator.map;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.validator.BasePropertyValidator;
|
||||||
|
|
||||||
|
public class EntryValidator<K, V>
|
||||||
|
extends BasePropertyValidator<Map<K, ? super V>, V, EntryValidator<K, V>> {
|
||||||
|
|
||||||
|
public EntryValidator(K key) {
|
||||||
|
super(m -> {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
V v = (V) m.get(key);
|
||||||
|
return v;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected EntryValidator<K, V> thisObject() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,83 @@
|
||||||
|
package xyz.zhouxy.plusone.validator.map;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public abstract class MapValidator<K, V> {
|
||||||
|
|
||||||
|
private final List<Consumer<Map<K, V>>> consumers = new LinkedList<>();
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void validate(Map<K, V> obj) {
|
||||||
|
this.consumers.forEach(consumer -> consumer.accept(obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
protected final <VV extends V> EntryValidator<K, VV> checkValue(K key, Class<VV> clazz) {
|
||||||
|
return checkValue(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final <VV extends V> EntryValidator<K, VV> checkValue(K key) {
|
||||||
|
EntryValidator<K, VV> validator = new EntryValidator<>(key);
|
||||||
|
this.consumers.add(validator::validate);
|
||||||
|
return validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final void withRule(Predicate<? super Map<K, V>> rule, String errMsg) {
|
||||||
|
withRule(rule, map -> new IllegalArgumentException(errMsg));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final void withRule(Predicate<? super Map<K, V>> rule, String errMsgFormat, Object... args) {
|
||||||
|
withRule(rule, map -> new IllegalArgumentException(String.format(errMsgFormat, args)));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final <E extends RuntimeException> void withRule(Predicate<? super Map<K, V>> rule, Supplier<E> e) {
|
||||||
|
withRule(rule, map -> e.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final <E extends RuntimeException> void withRule(Predicate<? super Map<K, V>> rule, Function<Map<K, V>, E> e) {
|
||||||
|
this.consumers.add(map -> {
|
||||||
|
if (!rule.test(map)) {
|
||||||
|
throw e.apply(map);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
|
@ -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.Collection;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||||
|
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.validator.map.MapValidator;
|
||||||
|
|
||||||
|
public //
|
||||||
|
class MapValidatorTests {
|
||||||
|
|
||||||
|
private static final String USERNAME = "username";
|
||||||
|
private static final String ACCOUNT = "account";
|
||||||
|
private static final String PASSWORD = "password";
|
||||||
|
private static final String PASSWORD2 = "password2";
|
||||||
|
private static final String AGE = "age";
|
||||||
|
private static final String BOOLEAN = "boolean";
|
||||||
|
private static final String ROLE_LIST = "roleList";
|
||||||
|
|
||||||
|
private static final MapValidator<String, Object> validator = new MapValidator<String, Object>(
|
||||||
|
new String[] { USERNAME, ACCOUNT, PASSWORD, AGE, BOOLEAN, ROLE_LIST }) {
|
||||||
|
{
|
||||||
|
|
||||||
|
checkValue(USERNAME, String.class).withRule(
|
||||||
|
PredicateTools.from(StringUtils::isNotBlank)
|
||||||
|
.and(username -> RegexTools.matches(username, PatternConsts.USERNAME)),
|
||||||
|
username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username)));
|
||||||
|
|
||||||
|
checkValue(ACCOUNT, String.class).withRule(
|
||||||
|
PredicateTools.from(StringUtils::isNotBlank)
|
||||||
|
.and(account -> RegexTools.matchesOne(account,
|
||||||
|
new Pattern[] { PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE })),
|
||||||
|
"请输入正确的邮箱地址或手机号");
|
||||||
|
|
||||||
|
checkValue(PASSWORD, String.class)
|
||||||
|
.withRule(StringUtils::isNotEmpty, "密码不能为空")
|
||||||
|
.withRule(pwd -> RegexTools.matches(pwd, PatternConsts.PASSWORD), "密码不符合规范");
|
||||||
|
|
||||||
|
// 校验到多个属性,只能针对 map 本身进行校验
|
||||||
|
withRule(m -> Objects.equals(m.get(PASSWORD), m.get(PASSWORD2)),
|
||||||
|
"两次输入的密码不一样!");
|
||||||
|
|
||||||
|
// 通过泛型方式调用方法,指定数据类型
|
||||||
|
this.<Integer>checkValue(AGE)
|
||||||
|
.withRule(Objects::nonNull)
|
||||||
|
.withRule(age -> (18 <= age && 60 >= age));
|
||||||
|
|
||||||
|
checkValue(BOOLEAN, Boolean.class)
|
||||||
|
.withRule(Objects::nonNull, "Boolean property could not be null.")
|
||||||
|
.withRule(b -> b, "Boolean property must be true.");
|
||||||
|
|
||||||
|
this.<Collection<String>>checkValue(ROLE_LIST)
|
||||||
|
.withRule(CollectionTools::isNotEmpty, "角色列表不能为空!")
|
||||||
|
.withRule(l -> l.stream().allMatch(StringUtils::isNotBlank),
|
||||||
|
() -> new IllegalArgumentException("角色标识不能为空!"));
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testValidateAndCopy() {
|
||||||
|
Map<String, Object> params = new HashMap<>();
|
||||||
|
params.put("username", "ZhouXY");
|
||||||
|
params.put("account", "zhouxy@code108.cn");
|
||||||
|
params.put("password", "99Code108");
|
||||||
|
params.put("password2", "99Code108");
|
||||||
|
params.put("age", 18);
|
||||||
|
params.put("boolean", true);
|
||||||
|
params.put("roleList", Arrays.asList("admin", ""));
|
||||||
|
|
||||||
|
Map<String, Object> validedParams = validator.validateAndCopy(params);
|
||||||
|
System.out.println(validedParams);
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,42 +4,47 @@ import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import xyz.zhouxy.plusone.constant.RegexConsts;
|
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.validator.BaseValidator;
|
import xyz.zhouxy.plusone.validator.BaseValidator;
|
||||||
import xyz.zhouxy.plusone.validator.ValidateUtil;
|
import xyz.zhouxy.plusone.validator.ValidateUtil;
|
||||||
|
|
||||||
public class BaseValidatorTest {
|
class BaseValidatorTest {
|
||||||
@Test
|
@Test
|
||||||
public void testValidate() {
|
void testValidate() {
|
||||||
RegisterCommand registerCommand = new RegisterCommand("zhouxy108", "luquanlion@outlook.com", "22336", "A1b2C3d4",
|
RegisterCommand registerCommand = new RegisterCommand("zhouxy108", "luquanlion@outlook.com", "22336",
|
||||||
|
"A1b2C3d4",
|
||||||
"A1b2C3d4",
|
"A1b2C3d4",
|
||||||
Arrays.asList(new String[] { "admin", "editor" }));
|
Arrays.asList(new String[] { "admin", "editor" }));
|
||||||
RegisterCommandValidator.INSTANCE.validate(registerCommand);
|
RegisterCommandValidator.INSTANCE.validate(registerCommand);
|
||||||
ValidateUtil.validate(registerCommand, RegisterCommandValidator.INSTANCE);
|
ValidateUtil.validate(registerCommand, RegisterCommandValidator.INSTANCE);
|
||||||
System.out.println(registerCommand);
|
System.out.println(registerCommand);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
class RegisterCommandValidator extends BaseValidator<RegisterCommand> {
|
static class RegisterCommandValidator extends BaseValidator<RegisterCommand> {
|
||||||
|
|
||||||
static final RegisterCommandValidator INSTANCE = new RegisterCommandValidator();
|
static final RegisterCommandValidator INSTANCE = new RegisterCommandValidator();
|
||||||
|
|
||||||
private RegisterCommandValidator() {
|
private RegisterCommandValidator() {
|
||||||
ruleForString(RegisterCommand::getUsername)
|
ruleForString(RegisterCommand::getUsername)
|
||||||
.notNull("用户名不能为空")
|
.isTrue(PredicateTools.<String>from(Objects::nonNull)
|
||||||
.matches(RegexConsts.USERNAME,
|
.and(StringUtils::isNotEmpty)
|
||||||
username -> new IllegalArgumentException(String.format("用户名\"%s\"不符合规范", username)));
|
.and(StringUtils::isNotBlank)
|
||||||
|
.and(username -> RegexTools.matches(username, PatternConsts.USERNAME)),
|
||||||
|
username -> new IllegalArgumentException(String.format("用户名【%s】不符合规范", username)));
|
||||||
ruleForString(RegisterCommand::getAccount)
|
ruleForString(RegisterCommand::getAccount)
|
||||||
.notNull("请输入邮箱地址或手机号")
|
.notNull("请输入邮箱地址或手机号")
|
||||||
.matchesOr(new String[] { RegexConsts.EMAIL, RegexConsts.MOBILE_PHONE }, "请输入邮箱地址或手机号");
|
.matchesOne(Arrays.asList(PatternConsts.EMAIL, PatternConsts.MOBILE_PHONE), "请输入邮箱地址或手机号");
|
||||||
ruleForString(RegisterCommand::getCode)
|
ruleForString(RegisterCommand::getCode)
|
||||||
.notNull("验证码不能为空")
|
.notNull("验证码不能为空")
|
||||||
.matches(RegexConsts.CAPTCHA, "验证码不符合规范");
|
.matches(PatternConsts.CAPTCHA, "验证码不符合规范");
|
||||||
ruleForString(RegisterCommand::getPassword)
|
ruleForString(RegisterCommand::getPassword)
|
||||||
.notEmpty("密码不能为空")
|
.notEmpty("密码不能为空")
|
||||||
.matches(RegexConsts.PASSWORD, "密码不符合规范");
|
.matches(PatternConsts.PASSWORD, "密码不符合规范");
|
||||||
ruleForCollection(RegisterCommand::getRoles)
|
ruleForCollection(RegisterCommand::getRoles)
|
||||||
.notEmpty(() -> new RuntimeException("角色列表不能为空"));
|
.notEmpty(() -> new RuntimeException("角色列表不能为空"));
|
||||||
|
|
||||||
|
@ -51,7 +56,7 @@ class RegisterCommandValidator extends BaseValidator<RegisterCommand> {
|
||||||
/**
|
/**
|
||||||
* RegisterCommand
|
* RegisterCommand
|
||||||
*/
|
*/
|
||||||
class RegisterCommand {
|
static class RegisterCommand {
|
||||||
|
|
||||||
private String username;
|
private String username;
|
||||||
private String account;
|
private String account;
|
||||||
|
@ -123,10 +128,16 @@ class RegisterCommand {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder builder = new StringBuilder();
|
return new StringBuilder()
|
||||||
builder.append("RegisterCommand [username=").append(username).append(", account=").append(account)
|
.append("RegisterCommand [")
|
||||||
.append(", code=").append(code).append(", password=").append(password).append(", password2=")
|
.append("username=").append(username)
|
||||||
.append(password2).append(", roles=").append(roles).append("]");
|
.append(", account=").append(account)
|
||||||
return builder.toString();
|
.append(", code=").append(code)
|
||||||
|
.append(", password=").append(password)
|
||||||
|
.append(", password2=").append(password2)
|
||||||
|
.append(", roles=").append(roles)
|
||||||
|
.append("]")
|
||||||
|
.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,147 @@
|
||||||
|
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.apache.commons.lang3.StringUtils;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
|
import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||||
|
import xyz.zhouxy.plusone.commons.function.PredicateTools;
|
||||||
|
import xyz.zhouxy.plusone.commons.util.RegexTools;
|
||||||
|
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)
|
||||||
|
&& StringUtils.isNotEmpty(username)
|
||||||
|
&& StringUtils.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();
|
||||||
|
Preconditions.checkArgument(Objects.nonNull(code), "验证码不能为空");
|
||||||
|
Preconditions.checkArgument(RegexTools.matches(code, CAPTCHA), "验证码不符合规范");
|
||||||
|
})
|
||||||
|
// 传入 rule
|
||||||
|
.addRule(command -> {
|
||||||
|
String password = command.getPassword();
|
||||||
|
Preconditions.checkArgument(StringUtils.isNotEmpty(password), "密码不能为空");
|
||||||
|
Preconditions.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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue