Compare commits
15 Commits
8b63eb6fe4
...
1.0.0-RC3
Author | SHA1 | Date | |
---|---|---|---|
483a3d5490 | |||
5705686375 | |||
5b5d7b50d6 | |||
aad5b6b4fe | |||
75a3c7798a | |||
7d0a25144b | |||
2977c9a7fb | |||
09d596b599 | |||
b3ef6deebe | |||
f86232c404 | |||
d0785d35e8 | |||
a315edf88f | |||
b6d47f0d00 | |||
12a5740dd6 | |||
654ecd8c63 |
23
README.md
23
README.md
@@ -25,14 +25,9 @@ class CustomerValidator extends BaseValidator<Customer> {
|
||||
.notNull("生日不能为空")
|
||||
.must(LocalDate.now().minusYears(16)::isAfter, "用户必须大于16周岁");
|
||||
ruleFor(Customer::getAddress).length(20, 250, "地址长度必须在20-250之间");
|
||||
ruleFor((Customer customer) -> Pair.of(customer.getVipLevel(), customer.getBirthday()))
|
||||
.must(CustomerValidator::validateAge, "5级以上会员必须满18周岁");
|
||||
}
|
||||
|
||||
private static boolean validateAge(Pair<Integer, LocalDate> vipLevelAndBirthday) {
|
||||
Integer vipLevel = vipLevelAndBirthday.getLeft();
|
||||
LocalDate birthday = vipLevelAndBirthday.getRight();
|
||||
return vipLevel <= 5 || LocalDate.now().minusYears(18).isAfter(birthday);
|
||||
ruleFor(Customer::getVipLevel, Customer::getBirthday)
|
||||
.must((vipLevel, birthday) -> vipLevel <= 5 || LocalDate.now().minusYears(18).isAfter(birthday),
|
||||
"5级以上会员必须满18周岁");
|
||||
}
|
||||
|
||||
public static CustomerValidator getInstance() {
|
||||
@@ -73,15 +68,9 @@ class CustomerMapValidator extends MapValidator<String, Object> {
|
||||
.notNull("生日不能为空")
|
||||
.must(LocalDate.now().minusYears(16)::isAfter, "用户必须大于16周岁");
|
||||
ruleForString("address").length(20, 250, "地址长度必须在20-250之间");
|
||||
this.<Pair<Integer, LocalDate>>ruleFor((Map<String, Object> customer) ->
|
||||
Pair.of(MapUtils.getInteger(customer, "vipLevel"), (LocalDate) customer.get("birthday")))
|
||||
.must(CustomerMapValidator::validateAge, "5级以上会员必须满18周岁");
|
||||
}
|
||||
|
||||
private static boolean validateAge(Pair<Integer, LocalDate> vipLevelAndBirthday) {
|
||||
Integer vipLevel = vipLevelAndBirthday.getLeft();
|
||||
LocalDate birthday = vipLevelAndBirthday.getRight();
|
||||
return vipLevel <= 5 || LocalDate.now().minusYears(18).isAfter(birthday);
|
||||
this.<Integer, LocalDate>ruleForPair("vipLevel", "birthday")
|
||||
.must((vipLevel, birthday) -> vipLevel <= 5 || LocalDate.now().minusYears(18).isAfter(birthday),
|
||||
"5级以上会员必须满18周岁");
|
||||
}
|
||||
|
||||
public static CustomerMapValidator getInstance() {
|
||||
|
@@ -8,28 +8,20 @@
|
||||
<parent>
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-validator-parent</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<version>1.0.0-RC3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>plusone-validator</artifactId>
|
||||
<name>plusone-validator</name>
|
||||
<url>http://zhouxy.xyz</url>
|
||||
|
||||
<description>
|
||||
Plusone Validator 是一个校验库,用于构建校验规则对对象(尤其是入参)进行校验。API 参考自 .NET 的 FluentValidation。
|
||||
Plusone Validator 是一个校验库,使用 lambda 表达式(包括方法引用)和流式 API 构建校验规则,对对象进行校验。
|
||||
</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>
|
||||
|
@@ -24,12 +24,14 @@ import xyz.zhouxy.plusone.commons.util.ArrayTools;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
|
||||
/**
|
||||
* 针对数组类型的属性校验器
|
||||
* 数组类型属性的校验器
|
||||
*
|
||||
* <p>
|
||||
* 内置数组相关的校验规则。
|
||||
* 用于构建校验数组类型属性的规则链。
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @param <T> 待校验对象的类型
|
||||
* @param <E> 数组元素的类型
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class ArrayPropertyValidator<T, E>
|
||||
extends BasePropertyValidator<T, E[], ArrayPropertyValidator<T, E>> {
|
||||
@@ -45,7 +47,7 @@ public class ArrayPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否非空
|
||||
*
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final ArrayPropertyValidator<T, E> notEmpty() {
|
||||
return withRule(Conditions.notEmpty(), "The input must not be empty.");
|
||||
@@ -55,7 +57,7 @@ public class ArrayPropertyValidator<T, E>
|
||||
* 添加一条校验属性的规则,校验属性是否非空
|
||||
*
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final ArrayPropertyValidator<T, E> notEmpty(
|
||||
final String errorMessage) {
|
||||
@@ -67,7 +69,7 @@ public class ArrayPropertyValidator<T, E>
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> notEmpty(
|
||||
final Supplier<X> exceptionSupplier) {
|
||||
@@ -79,7 +81,7 @@ public class ArrayPropertyValidator<T, E>
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> notEmpty(
|
||||
final Function<E[], X> exceptionFunction) {
|
||||
@@ -97,7 +99,7 @@ public class ArrayPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final ArrayPropertyValidator<T, E> isEmpty() {
|
||||
return withRule(Conditions.isEmpty(), "The input must be empty.");
|
||||
@@ -107,7 +109,7 @@ public class ArrayPropertyValidator<T, E>
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final ArrayPropertyValidator<T, E> isEmpty(
|
||||
final String errorMessage) {
|
||||
@@ -117,8 +119,9 @@ public class ArrayPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> isEmpty(
|
||||
final Supplier<X> exceptionSupplier) {
|
||||
@@ -128,8 +131,9 @@ public class ArrayPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> isEmpty(
|
||||
final Function<E[], X> exceptionFunction) {
|
||||
@@ -145,10 +149,10 @@ public class ArrayPropertyValidator<T, E>
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足条件
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足指定条件
|
||||
*
|
||||
* @param condition 校验规则
|
||||
* @return 属性校验器
|
||||
* @param condition 校验条件
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final ArrayPropertyValidator<T, E> allMatch(final Predicate<E> condition) {
|
||||
return withRule(c -> {
|
||||
@@ -161,11 +165,11 @@ public class ArrayPropertyValidator<T, E>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足条件
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足指定条件
|
||||
*
|
||||
* @param condition 校验规则
|
||||
* @param condition 校验条件
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final ArrayPropertyValidator<T, E> allMatch(
|
||||
final Predicate<E> condition, final String errorMessage) {
|
||||
@@ -179,11 +183,12 @@ public class ArrayPropertyValidator<T, E>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足条件
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足指定条件
|
||||
*
|
||||
* @param condition 校验规则
|
||||
* @param <X> 自定义异常类型
|
||||
* @param condition 校验条件
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> allMatch(
|
||||
final Predicate<E> condition, final Supplier<X> exceptionSupplier) {
|
||||
@@ -197,11 +202,12 @@ public class ArrayPropertyValidator<T, E>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足条件
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足指定条件
|
||||
*
|
||||
* @param condition 校验规则
|
||||
* @param <X> 自定义异常类型
|
||||
* @param condition 校验条件
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> allMatch(
|
||||
final Predicate<E> condition, final Function<E, X> exceptionFunction) {
|
||||
@@ -225,9 +231,9 @@ public class ArrayPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性长度是否等于指定长度
|
||||
*
|
||||
* @param length 指定长度
|
||||
* @param length 预期长度
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final ArrayPropertyValidator<T, E> length(
|
||||
final int length, final String errorMessage) {
|
||||
@@ -237,10 +243,10 @@ public class ArrayPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性长度是否等于指定长度
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param length 指定长度
|
||||
* @param <X> 自定义异常类型
|
||||
* @param length 预期长度
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> length(
|
||||
final int length, final Supplier<X> exceptionSupplier) {
|
||||
@@ -250,10 +256,10 @@ public class ArrayPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性长度是否等于指定长度
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param length 指定长度
|
||||
* @param <X> 自定义异常类型
|
||||
* @param length 预期长度
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> length(
|
||||
final int length, final Function<E[], X> exceptionFunction) {
|
||||
@@ -263,10 +269,10 @@ public class ArrayPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性的长度范围
|
||||
*
|
||||
* @param min 最小长度
|
||||
* @param max 最大长度
|
||||
* @param min 最小长度(包含)
|
||||
* @param max 最大长度(包含)
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final ArrayPropertyValidator<T, E> length(
|
||||
final int min, final int max, final String errorMessage) {
|
||||
@@ -276,10 +282,11 @@ public class ArrayPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性的长度范围
|
||||
*
|
||||
* @param min 最小长度
|
||||
* @param max 最大长度
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小长度(包含)
|
||||
* @param max 最大长度(包含)
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> length(
|
||||
final int min, final int max, final Supplier<X> exceptionSupplier) {
|
||||
@@ -289,10 +296,11 @@ public class ArrayPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性的长度范围
|
||||
*
|
||||
* @param min 最小长度
|
||||
* @param max 最大长度
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小长度(包含)
|
||||
* @param max 最大长度(包含)
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> length(
|
||||
final int min, final int max, final Function<E[], X> exceptionFunction) {
|
||||
|
@@ -23,20 +23,17 @@ import java.util.function.Supplier;
|
||||
import com.google.common.collect.Range;
|
||||
|
||||
/**
|
||||
* 针对 {@code Comparable} 类型的属性校验器基类
|
||||
* {@code Comparable} 类型属性的校验器的基类
|
||||
*
|
||||
* <p>
|
||||
* 内置了判断属性是否在给定区间内的校验规则。
|
||||
*
|
||||
* @param <T> 待校验对象类型
|
||||
* @param <TProperty> 属性类型
|
||||
* @param <TPropertyValidator> 当前属性校验器类型,用于链式调用
|
||||
* @param <T> 待校验对象的类型
|
||||
* @param <TProperty> 待校验属性的类型,必须实现 {@code Comparable} 接口
|
||||
* @param <TPropertyValidator> 具体校验器类型,用于支持链式调用
|
||||
* @see Range
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public abstract class BaseComparablePropertyValidator<
|
||||
T,
|
||||
TProperty extends Comparable<TProperty>,
|
||||
TProperty extends Comparable<? super TProperty>,
|
||||
TPropertyValidator extends BaseComparablePropertyValidator<T, TProperty, TPropertyValidator>>
|
||||
extends BasePropertyValidator<T, TProperty, TPropertyValidator> {
|
||||
|
||||
@@ -45,55 +42,61 @@ public abstract class BaseComparablePropertyValidator<
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否在给定的区间之内
|
||||
* 添加一条校验属性的规则,校验属性的取值范围。
|
||||
*
|
||||
* @param range 区间
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final TPropertyValidator inRange(final Range<TProperty> range) {
|
||||
public final TPropertyValidator inRange(final Range<? super TProperty> range) {
|
||||
return withRule(Conditions.inRange(range), value -> ValidationException.withMessage(
|
||||
"The input must in the interval %s. You entered %s.", range, value));
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否在给定的区间之内
|
||||
* 添加一条校验属性的规则,校验属性的取值范围。
|
||||
*
|
||||
* @param range 区间
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @param errorMessage 自定义错误消息模板
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final TPropertyValidator inRange(
|
||||
final Range<TProperty> range, final String errorMessage) {
|
||||
final Range<? super TProperty> range, final String errorMessage) {
|
||||
return withRule(Conditions.inRange(range), errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否在给定的区间之内
|
||||
* 添加一条校验属性的规则,校验属性的取值范围。
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param range 区间
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> TPropertyValidator inRange(
|
||||
final Range<TProperty> range, final Supplier<X> exceptionSupplier) {
|
||||
final Range<? super TProperty> range, final Supplier<X> exceptionSupplier) {
|
||||
return withRule(Conditions.inRange(range), exceptionSupplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否在给定的区间之内
|
||||
* 添加一条校验属性的规则,校验属性的取值范围。
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param range 区间
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> TPropertyValidator inRange(
|
||||
final Range<TProperty> range, final Function<TProperty, X> exceptionFunction) {
|
||||
final Range<? super TProperty> range, final Function<TProperty, X> exceptionFunction) {
|
||||
return withRule(Conditions.inRange(range), exceptionFunction);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验条件的实现
|
||||
*/
|
||||
private static class Conditions {
|
||||
private static <TProperty extends Comparable<TProperty>> Predicate<TProperty> inRange(
|
||||
final Range<TProperty> range) {
|
||||
|
||||
private static <TProperty extends Comparable<? super TProperty>> Predicate<TProperty> inRange(
|
||||
final Range<? super TProperty> range) {
|
||||
return value -> value == null || range.contains(value);
|
||||
}
|
||||
}
|
||||
|
@@ -28,10 +28,12 @@ import java.util.function.Supplier;
|
||||
* 属性校验器。包含针对属性的校验规则。
|
||||
*
|
||||
* <p>
|
||||
* <i>内置基础的校验规则。</i>
|
||||
* </p>
|
||||
* 用于构建针对特定属性的校验规则链,支持通过链式调用添加多种校验规则。
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @param <T> 待校验对象的类型
|
||||
* @param <TProperty> 待校验属性的类型
|
||||
* @param <TPropertyValidator> 具体校验器类型,用于支持链式调用
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public abstract class BasePropertyValidator<
|
||||
T,
|
||||
@@ -47,11 +49,12 @@ public abstract class BasePropertyValidator<
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则
|
||||
* 添加一条校验属性的规则,当条件不满足时抛出异常。
|
||||
*
|
||||
* @param condition 校验条件
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
* @throws ValidationException 校验失败时抛出的异常
|
||||
*/
|
||||
protected final TPropertyValidator withRule(
|
||||
final Predicate<? super TProperty> condition,
|
||||
@@ -67,9 +70,10 @@ public abstract class BasePropertyValidator<
|
||||
* 添加一条校验属性的规则
|
||||
*
|
||||
* @param condition 校验条件
|
||||
* @param errorMessageTemplate 错误信息模版
|
||||
* @param errorMessageArgs 错误信息参数
|
||||
* @return 属性校验器
|
||||
* @param errorMessageTemplate 异常信息模板
|
||||
* @param errorMessageArgs 异常信息参数
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
* @throws ValidationException 校验失败时抛出的异常
|
||||
*/
|
||||
protected final TPropertyValidator withRule(
|
||||
final Predicate<? super TProperty> condition,
|
||||
@@ -84,9 +88,10 @@ public abstract class BasePropertyValidator<
|
||||
/**
|
||||
* 添加一条校验属性的规则
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param condition 校验条件
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
protected final <X extends RuntimeException> TPropertyValidator withRule(
|
||||
final Predicate<? super TProperty> condition,
|
||||
@@ -99,11 +104,12 @@ public abstract class BasePropertyValidator<
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则
|
||||
* 添加一条校验属性的规则,当条件不满足时抛出自定义异常。可以根据当前属性的值创建异常。
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param condition 校验条件
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
protected final <X extends RuntimeException> TPropertyValidator withRule(
|
||||
final Predicate<? super TProperty> condition,
|
||||
@@ -118,8 +124,8 @@ public abstract class BasePropertyValidator<
|
||||
/**
|
||||
* 添加一条校验属性的规则
|
||||
*
|
||||
* @param rule 校验规则
|
||||
* @return 属性校验器
|
||||
* @param rule 自定义校验规则
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
protected final TPropertyValidator withRule(Consumer<? super TProperty> rule) {
|
||||
this.consumers.add(rule);
|
||||
@@ -150,7 +156,7 @@ public abstract class BasePropertyValidator<
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否不为空
|
||||
*
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final TPropertyValidator notNull() {
|
||||
return withRule(Objects::nonNull, "The input must not be null.");
|
||||
@@ -160,7 +166,7 @@ public abstract class BasePropertyValidator<
|
||||
* 添加一条校验属性的规则,校验属性是否不为空
|
||||
*
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final TPropertyValidator notNull(final String errorMessage) {
|
||||
return withRule(Objects::nonNull, errorMessage);
|
||||
@@ -171,7 +177,7 @@ public abstract class BasePropertyValidator<
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> TPropertyValidator notNull(
|
||||
final Supplier<X> exceptionSupplier) {
|
||||
@@ -183,7 +189,7 @@ public abstract class BasePropertyValidator<
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> TPropertyValidator notNull(
|
||||
final Function<TProperty, X> exceptionFunction) {
|
||||
@@ -201,7 +207,7 @@ public abstract class BasePropertyValidator<
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final TPropertyValidator isNull() {
|
||||
return withRule(Objects::isNull, "The input must be null.");
|
||||
@@ -211,7 +217,7 @@ public abstract class BasePropertyValidator<
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final TPropertyValidator isNull(final String errorMessage) {
|
||||
return withRule(Objects::isNull, errorMessage);
|
||||
@@ -222,7 +228,7 @@ public abstract class BasePropertyValidator<
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> TPropertyValidator isNull(
|
||||
final Supplier<X> exceptionSupplier) {
|
||||
@@ -234,7 +240,7 @@ public abstract class BasePropertyValidator<
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> TPropertyValidator isNull(
|
||||
final Function<TProperty, X> exceptionFunction) {
|
||||
@@ -252,50 +258,50 @@ public abstract class BasePropertyValidator<
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
*
|
||||
* @param that 给定值
|
||||
* @return 属性校验器
|
||||
* @param obj 用于比较的对象
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final TPropertyValidator equal(Object that) {
|
||||
return withRule(Conditions.equal(that),
|
||||
"The input must be equal to '%s'.", that);
|
||||
public final TPropertyValidator equal(Object obj) {
|
||||
return withRule(Conditions.equal(obj),
|
||||
"The input must be equal to '%s'.", obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
*
|
||||
* @param that 给定值
|
||||
* @param obj 用于比较的对象
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final TPropertyValidator equal(
|
||||
final Object that, final String errorMessage) {
|
||||
return withRule(Conditions.equal(that), errorMessage);
|
||||
final Object obj, final String errorMessage) {
|
||||
return withRule(Conditions.equal(obj), errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param that 给定值
|
||||
* @param obj 用于比较的对象
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> TPropertyValidator equal(
|
||||
final Object that, final Supplier<X> exceptionSupplier) {
|
||||
return withRule(Conditions.equal(that), exceptionSupplier);
|
||||
final Object obj, final Supplier<X> exceptionSupplier) {
|
||||
return withRule(Conditions.equal(obj), exceptionSupplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param that 给定值
|
||||
* @param obj 用于比较的对象
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> TPropertyValidator equal(
|
||||
final Object that, final Function<TProperty, X> exceptionFunction) {
|
||||
return withRule(Conditions.equal(that), exceptionFunction);
|
||||
final Object obj, final Function<TProperty, X> exceptionFunction) {
|
||||
return withRule(Conditions.equal(obj), exceptionFunction);
|
||||
}
|
||||
|
||||
// ================================
|
||||
@@ -309,49 +315,49 @@ public abstract class BasePropertyValidator<
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
*
|
||||
* @param that 给定值
|
||||
* @return 属性校验器
|
||||
* @param obj 用于比较的对象
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final TPropertyValidator notEqual(final Object that) {
|
||||
return withRule(Conditions.notEqual(that),
|
||||
"The input must not equal '%s'.", that);
|
||||
public final TPropertyValidator notEqual(final Object obj) {
|
||||
return withRule(Conditions.notEqual(obj),
|
||||
"The input must not equal '%s'.", obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
*
|
||||
* @param that 给定值
|
||||
* @param obj 用于比较的对象
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final TPropertyValidator notEqual(final Object that, final String errorMessage) {
|
||||
return withRule(Conditions.notEqual(that), errorMessage);
|
||||
public final TPropertyValidator notEqual(final Object obj, final String errorMessage) {
|
||||
return withRule(Conditions.notEqual(obj), errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param that 给定值
|
||||
* @param obj 用于比较的对象
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> TPropertyValidator notEqual(
|
||||
final Object that, final Supplier<X> exceptionSupplier) {
|
||||
return withRule(Conditions.notEqual(that), exceptionSupplier);
|
||||
final Object obj, final Supplier<X> exceptionSupplier) {
|
||||
return withRule(Conditions.notEqual(obj), exceptionSupplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param that 给定值
|
||||
* @param obj 用于比较的对象
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> TPropertyValidator notEqual(
|
||||
final Object that, final Function<TProperty, X> exceptionFunction) {
|
||||
return withRule(Conditions.notEqual(that), exceptionFunction);
|
||||
final Object obj, final Function<TProperty, X> exceptionFunction) {
|
||||
return withRule(Conditions.notEqual(obj), exceptionFunction);
|
||||
}
|
||||
|
||||
// ================================
|
||||
@@ -366,7 +372,7 @@ public abstract class BasePropertyValidator<
|
||||
* 添加一条校验属性的规则,校验属性是否满足给定的条件
|
||||
*
|
||||
* @param condition 校验条件
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final TPropertyValidator must(final Predicate<? super TProperty> condition) {
|
||||
return withRule(condition,
|
||||
@@ -378,7 +384,7 @@ public abstract class BasePropertyValidator<
|
||||
*
|
||||
* @param condition 校验条件
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final TPropertyValidator must(
|
||||
final Predicate<? super TProperty> condition,
|
||||
@@ -392,7 +398,7 @@ public abstract class BasePropertyValidator<
|
||||
* @param <X> 自定义异常类型
|
||||
* @param condition 校验规则
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> TPropertyValidator must(
|
||||
final Predicate<? super TProperty> condition,
|
||||
@@ -406,7 +412,7 @@ public abstract class BasePropertyValidator<
|
||||
* @param <X> 自定义异常类型
|
||||
* @param condition 校验规则
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> TPropertyValidator must(
|
||||
final Predicate<? super TProperty> condition,
|
||||
@@ -422,6 +428,9 @@ public abstract class BasePropertyValidator<
|
||||
// #region - conditions
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 常用校验条件的实现
|
||||
*/
|
||||
private static class Conditions {
|
||||
|
||||
private static <TProperty> Predicate<TProperty> equal(Object obj) {
|
||||
|
@@ -19,6 +19,7 @@ package xyz.zhouxy.plusone.validator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.AbstractMap.SimpleImmutableEntry;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
@@ -28,22 +29,19 @@ import java.util.function.Supplier;
|
||||
import xyz.zhouxy.plusone.validator.function.*;
|
||||
|
||||
/**
|
||||
* 校验器的基类
|
||||
*
|
||||
* 校验器基类
|
||||
* <p>
|
||||
* 通过继承 {@code BaseValidator},可以自定义一个针对特定类型的校验器,包含对该类型的校验逻辑。
|
||||
* 子类可通过添加不同的校验规则,构建完整的校验逻辑,用于校验对象。
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @param <T> 待校验对象的类型
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
|
||||
/**
|
||||
* 规则集合
|
||||
*/
|
||||
private final List<Consumer<? super T>> rules = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 添加一个校验规则
|
||||
* 添加一条用于校验整个对象的规则
|
||||
*
|
||||
* @param condition 校验条件
|
||||
* @param errorMessage 异常信息
|
||||
@@ -53,7 +51,7 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个校验规则
|
||||
* 添加一条用于校验整个对象的规则
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param condition 校验条件
|
||||
@@ -65,7 +63,7 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个校验规则
|
||||
* 添加一条用于校验整个对象的规则
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param condition 校验条件
|
||||
@@ -81,19 +79,21 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个校验规则
|
||||
* 添加一条用于校验整个对象的规则
|
||||
*
|
||||
* @param rule 校验规则。内部包含断言条件,如果条件不满足,则抛出异常。
|
||||
* @param rule 自定义校验规则
|
||||
*/
|
||||
protected final void withRule(Consumer<? super T> rule) {
|
||||
this.rules.add(rule);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个属性校验器
|
||||
* 添加一个通用的属性校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
* @param <R> 属性类型
|
||||
* @param getter 用于从目标对象获取属性值的函数式接口。
|
||||
* 示例:{@code Person::getName}。
|
||||
* @return {@code ObjectPropertyValidator}。用于添加针对该属性的校验规则。
|
||||
*/
|
||||
protected final <R> ObjectPropertyValidator<T, R> ruleFor(Function<T, R> getter) {
|
||||
ObjectPropertyValidator<T, R> validator = new ObjectPropertyValidator<>(getter);
|
||||
@@ -102,13 +102,14 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Comparable} 属性的校验器
|
||||
* 添加一个用于校验 {@code Comparable} 类型的属性校验器
|
||||
*
|
||||
* @param <R> 属性类型
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
* @param getter 用于从目标对象获取属性值的函数式接口。
|
||||
* 示例:{@code Person::getName}。
|
||||
* @return {@code ComparablePropertyValidator}。用于添加针对该属性的校验规则。
|
||||
*/
|
||||
protected final <R extends Comparable<R>> ComparablePropertyValidator<T, R> ruleForComparable(
|
||||
protected final <R extends Comparable<? super R>> ComparablePropertyValidator<T, R> ruleForComparable(
|
||||
Function<T, R> getter) {
|
||||
ComparablePropertyValidator<T, R> validator = new ComparablePropertyValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
@@ -116,10 +117,11 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Integer} 属性的校验器
|
||||
* 添加一个用于校验 {@code Integer} 类型的属性校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
* @param getter 用于从目标对象获取属性值的函数式接口。
|
||||
* 示例:{@code Person::getName}。
|
||||
* @return {@code IntPropertyValidator}。用于添加针对该属性的校验规则。
|
||||
*/
|
||||
protected final IntPropertyValidator<T> ruleForInt(Function<T, Integer> getter) {
|
||||
IntPropertyValidator<T> validator = new IntPropertyValidator<>(getter);
|
||||
@@ -128,10 +130,11 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Integer} 属性的校验器
|
||||
* 添加一个用于校验 {@code Integer} 类型的属性校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
* @param getter 用于从目标对象获取属性值的函数式接口。
|
||||
* 示例:{@code Person::getName}。
|
||||
* @return {@code IntPropertyValidator}。用于添加针对该属性的校验规则。
|
||||
*/
|
||||
protected final IntPropertyValidator<T> ruleFor(ToIntegerFunction<T> getter) {
|
||||
IntPropertyValidator<T> validator = new IntPropertyValidator<>(getter);
|
||||
@@ -140,10 +143,11 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Long} 属性的校验器
|
||||
* 添加一个用于校验 {@code Long} 类型的属性校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
* @param getter 用于从目标对象获取属性值的函数式接口。
|
||||
* 示例:{@code Person::getName}。
|
||||
* @return {@code LongPropertyValidator}。用于添加针对该属性的校验规则。
|
||||
*/
|
||||
protected final LongPropertyValidator<T> ruleForLong(Function<T, Long> getter) {
|
||||
LongPropertyValidator<T> validator = new LongPropertyValidator<>(getter);
|
||||
@@ -152,10 +156,11 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Long} 属性的校验器
|
||||
* 添加一个用于校验 {@code Long} 类型的属性校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
* @param getter 用于从目标对象获取属性值的函数式接口。
|
||||
* 示例:{@code Person::getName}。
|
||||
* @return {@code LongPropertyValidator}。用于添加针对该属性的校验规则。
|
||||
*/
|
||||
protected final LongPropertyValidator<T> ruleFor(ToLongObjectFunction<T> getter) {
|
||||
LongPropertyValidator<T> validator = new LongPropertyValidator<>(getter);
|
||||
@@ -164,10 +169,11 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Double} 属性的校验器
|
||||
* 添加一个用于校验 {@code Double} 类型的属性校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
* @param getter 用于从目标对象获取属性值的函数式接口。
|
||||
* 示例:{@code Person::getName}。
|
||||
* @return {@code DoublePropertyValidator}。用于添加针对该属性的校验规则。
|
||||
*/
|
||||
protected final DoublePropertyValidator<T> ruleForDouble(Function<T, Double> getter) {
|
||||
DoublePropertyValidator<T> validator = new DoublePropertyValidator<>(getter);
|
||||
@@ -176,10 +182,11 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Double} 属性的校验器
|
||||
* 添加一个用于校验 {@code Double} 类型的属性校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
* @param getter 用于从目标对象获取属性值的函数式接口。
|
||||
* 示例:{@code Person::getName}。
|
||||
* @return {@code DoublePropertyValidator}。用于添加针对该属性的校验规则。
|
||||
*/
|
||||
protected final DoublePropertyValidator<T> ruleFor(ToDoubleObjectFunction<T> getter) {
|
||||
DoublePropertyValidator<T> validator = new DoublePropertyValidator<>(getter);
|
||||
@@ -188,10 +195,11 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Boolean} 属性的校验器
|
||||
* 添加一个用于校验 {@code Boolean} 类型的属性校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
* @param getter 用于从目标对象获取属性值的函数式接口。
|
||||
* 示例:{@code Person::getName}。
|
||||
* @return {@code BoolPropertyValidator}。用于添加针对该属性的校验规则。
|
||||
*/
|
||||
protected final BoolPropertyValidator<T> ruleForBool(Function<T, Boolean> getter) {
|
||||
BoolPropertyValidator<T> validator = new BoolPropertyValidator<>(getter);
|
||||
@@ -200,10 +208,11 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Boolean} 属性的校验器
|
||||
* 添加一个用于校验 {@code Boolean} 类型的属性校验器
|
||||
*
|
||||
* @param getter 属性获取函数
|
||||
* @return 属性校验器
|
||||
* @param getter 用于从目标对象获取属性值的函数式接口。
|
||||
* 示例:{@code Person::getName}。
|
||||
* @return {@code BoolPropertyValidator}。用于添加针对该属性的校验规则。
|
||||
*/
|
||||
protected final BoolPropertyValidator<T> ruleFor(ToBoolObjectFunction<T> getter) {
|
||||
BoolPropertyValidator<T> validator = new BoolPropertyValidator<>(getter);
|
||||
@@ -212,10 +221,11 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code String} 属性的校验器
|
||||
* 添加一个用于校验 {@code String} 类型的属性校验器
|
||||
*
|
||||
* @param getter 获取属性值的函数
|
||||
* @return 属性校验器
|
||||
* @param getter 用于从目标对象获取属性值的函数式接口。
|
||||
* 示例:{@code Person::getName}。
|
||||
* @return {@code StringPropertyValidator}。用于添加针对该属性的校验规则。
|
||||
*/
|
||||
protected final StringPropertyValidator<T> ruleForString(Function<T, String> getter) {
|
||||
StringPropertyValidator<T> validator = new StringPropertyValidator<>(getter);
|
||||
@@ -224,10 +234,11 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code String} 属性的校验器
|
||||
* 添加一个用于校验 {@code String} 类型的属性校验器
|
||||
*
|
||||
* @param getter 获取属性值的函数
|
||||
* @return 属性校验器
|
||||
* @param getter 用于从目标对象获取属性值的函数式接口。
|
||||
* 示例:{@code Person::getName}。
|
||||
* @return {@code StringPropertyValidator}。用于添加针对该属性的校验规则。
|
||||
*/
|
||||
protected final StringPropertyValidator<T> ruleFor(ToStringFunction<T> getter) {
|
||||
StringPropertyValidator<T> validator = new StringPropertyValidator<>(getter);
|
||||
@@ -236,10 +247,12 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对 {@code Collection} 属性的校验器
|
||||
* 添加一个用于校验集合类型的属性校验器
|
||||
*
|
||||
* @param getter 获取属性值的函数
|
||||
* @return 集合属性校验器
|
||||
* @param <E> 集合元素类型
|
||||
* @param getter 用于从目标对象获取属性值的函数式接口。
|
||||
* 示例:{@code Person::getName}。
|
||||
* @return {@code CollectionPropertyValidator}。用于添加针对该属性的校验规则。
|
||||
*/
|
||||
protected final <E> CollectionPropertyValidator<T, E> ruleForCollection(Function<T, Collection<E>> getter) {
|
||||
CollectionPropertyValidator<T, E> validator = new CollectionPropertyValidator<>(getter);
|
||||
@@ -248,10 +261,12 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对数组属性的校验器
|
||||
* 添加一个用于校验数组类型的属性校验器
|
||||
*
|
||||
* @param getter 获取属性值的函数
|
||||
* @return 集合属性校验器
|
||||
* @param <E> 数组元素类型
|
||||
* @param getter 用于从目标对象获取属性值的函数式接口。
|
||||
* 示例:{@code Person::getName}。
|
||||
* @return {@code ArrayPropertyValidator}。用于添加针对该属性的校验规则。
|
||||
*/
|
||||
protected final <E> ArrayPropertyValidator<T, E> ruleForArray(Function<T, E[]> getter) {
|
||||
ArrayPropertyValidator<T, E> validator = new ArrayPropertyValidator<>(getter);
|
||||
@@ -261,17 +276,39 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
|
||||
/**
|
||||
* 添加一个针对二元组的校验器
|
||||
*
|
||||
* @param <V1> 第一个元素的类型
|
||||
* @param <V2> 第二个元素的类型
|
||||
* @param getter 获取属性值的函数
|
||||
* @return 二元组校验器
|
||||
* @param getter 根据对象构造一个二元组,通常是两个属性的值。
|
||||
* @return {@code PairPropertyValidator}。用于添加针对该二元组的校验规则。
|
||||
*
|
||||
* @deprecated 请使用 {@link #ruleFor(Function, Function)} 代替。
|
||||
*/
|
||||
protected final <V1, V2> PairPropertyValidator<T, V1, V2> ruleForPair(Function<T, Entry<V1, V2>> getter) {
|
||||
@Deprecated
|
||||
protected final <V1, V2> PairPropertyValidator<T, V1, V2> ruleForPair( // NOSONAR
|
||||
Function<T, Entry<V1, V2>> getter) {
|
||||
PairPropertyValidator<T, V1, V2> validator = new PairPropertyValidator<>(getter);
|
||||
this.rules.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一个针对二元组的校验器
|
||||
*
|
||||
* @param <V1> 第1个元素的类型
|
||||
* @param <V2> 第2个元素的类型
|
||||
* @param v1Getter 用于从目标对象获取第1个元素的函数式接口。示例:{@code Person::getName1}。
|
||||
* @param v2Getter 用于从目标对象获取第2个元素的函数式接口。示例:{@code Person::getName2}。
|
||||
* @return {@code PairPropertyValidator}。用于添加针对该二元组的校验规则。
|
||||
*/
|
||||
protected final <V1, V2> PairPropertyValidator<T, V1, V2> ruleFor(
|
||||
Function<T, V1> v1Getter, Function<T, V2> v2Getter) {
|
||||
PairPropertyValidator<T, V1, V2> validator = new PairPropertyValidator<>(
|
||||
t -> new SimpleImmutableEntry<>(v1Getter.apply(t), v2Getter.apply(t)));
|
||||
this.rules.add(validator::validate);
|
||||
return validator;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void validate(T obj) {
|
||||
|
@@ -21,13 +21,13 @@ import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 针对 {@code Boolean} 类型的属性校验器
|
||||
* {@code Boolean} 类型属性的校验器
|
||||
*
|
||||
* <p>
|
||||
* 内置了判断 Boolean 值是否为 true 或 false 的校验规则。
|
||||
* </p>
|
||||
* 用于构建校验 {@code Boolean} 类型属性的规则链。
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @param <T> 待校验对象的类型
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class BoolPropertyValidator<T>
|
||||
extends BasePropertyValidator<T, Boolean, BoolPropertyValidator<T>> {
|
||||
@@ -39,30 +39,30 @@ public class BoolPropertyValidator<T>
|
||||
// ====== isTrueValue ======
|
||||
|
||||
/**
|
||||
* 添加一条判断属性值是否为 {@code true} 的校验规则
|
||||
* 添加一条校验属性的规则,校验属性是否为 {@code true}
|
||||
*
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final BoolPropertyValidator<T> isTrueValue() {
|
||||
return withRule(Conditions.isTrueValue(), "The input must be true.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条判断属性值是否为 {@code true} 的校验规则
|
||||
* 添加一条校验属性的规则,校验属性是否为 {@code true}
|
||||
*
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final BoolPropertyValidator<T> isTrueValue(final String errorMessage) {
|
||||
return withRule(Conditions.isTrueValue(), errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条判断属性值是否为 {@code true} 的校验规则
|
||||
* 添加一条校验属性的规则,校验属性是否为 {@code true}
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> BoolPropertyValidator<T> isTrueValue(
|
||||
final Supplier<X> exceptionSupplier) {
|
||||
@@ -70,44 +70,44 @@ public class BoolPropertyValidator<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条判断属性值是否为 {@code true} 的校验规则
|
||||
* 添加一条校验属性的规则,校验属性是否为 {@code true}
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> BoolPropertyValidator<T> isTrueValue(
|
||||
Function<Boolean, X> exceptionFunction) {
|
||||
final Function<Boolean, X> exceptionFunction) {
|
||||
return withRule(Conditions.isTrueValue(), exceptionFunction);
|
||||
}
|
||||
|
||||
// ====== isFalseValue ======
|
||||
|
||||
/**
|
||||
* 添加一条判断属性值是否为 {@code false} 的校验规则
|
||||
* 添加一条校验属性的规则,校验属性是否为 {@code false}
|
||||
*
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final BoolPropertyValidator<T> isFalseValue() {
|
||||
return withRule(Conditions.isFalseValue(), "The input must be false.");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条判断属性值是否为 {@code false} 的校验规则
|
||||
* 添加一条校验属性的规则,校验属性是否为 {@code false}
|
||||
*
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final BoolPropertyValidator<T> isFalseValue(final String errorMessage) {
|
||||
return withRule(Conditions.isFalseValue(), errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条判断属性值是否为 {@code false} 的校验规则
|
||||
* 添加一条校验属性的规则,校验属性是否为 {@code false}
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> BoolPropertyValidator<T> isFalseValue(
|
||||
final Supplier<X> exceptionSupplier) {
|
||||
@@ -115,11 +115,11 @@ public class BoolPropertyValidator<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条判断属性值是否为 {@code false} 的校验规则
|
||||
* 添加一条校验属性的规则,校验属性是否为 {@code false}
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> BoolPropertyValidator<T> isFalseValue(
|
||||
final Function<Boolean, X> exceptionFunction) {
|
||||
|
@@ -25,12 +25,14 @@ import xyz.zhouxy.plusone.commons.collection.CollectionTools;
|
||||
import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
|
||||
/**
|
||||
* 针对集合类型的属性校验器
|
||||
* 集合类型属性的校验器
|
||||
*
|
||||
* <p>
|
||||
* 内置集合相关的校验规则。
|
||||
* 用于构建校验集合类型属性的规则链。
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @param <T> 待校验对象的类型
|
||||
* @param <E> 集合元素的类型
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class CollectionPropertyValidator<T, E>
|
||||
extends BasePropertyValidator<T, Collection<E>, CollectionPropertyValidator<T, E>> {
|
||||
@@ -46,7 +48,7 @@ public class CollectionPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否非空
|
||||
*
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final CollectionPropertyValidator<T, E> notEmpty() {
|
||||
return withRule(Conditions.notEmpty(), "The input must not be empty.");
|
||||
@@ -56,7 +58,7 @@ public class CollectionPropertyValidator<T, E>
|
||||
* 添加一条校验属性的规则,校验属性是否非空
|
||||
*
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final CollectionPropertyValidator<T, E> notEmpty(final String errorMessage) {
|
||||
return withRule(Conditions.notEmpty(), errorMessage);
|
||||
@@ -67,7 +69,7 @@ public class CollectionPropertyValidator<T, E>
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> notEmpty(
|
||||
final Supplier<X> exceptionSupplier) {
|
||||
@@ -79,7 +81,7 @@ public class CollectionPropertyValidator<T, E>
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> notEmpty(
|
||||
final Function<Collection<E>, X> exceptionFunction) {
|
||||
@@ -97,7 +99,7 @@ public class CollectionPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final CollectionPropertyValidator<T, E> isEmpty() {
|
||||
return withRule(Conditions.isEmpty(), "The input must be empty.");
|
||||
@@ -107,7 +109,7 @@ public class CollectionPropertyValidator<T, E>
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final CollectionPropertyValidator<T, E> isEmpty(
|
||||
final String errorMessage) {
|
||||
@@ -117,8 +119,9 @@ public class CollectionPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> isEmpty(
|
||||
final Supplier<X> exceptionSupplier) {
|
||||
@@ -128,8 +131,9 @@ public class CollectionPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否为空
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> isEmpty(
|
||||
final Function<Collection<E>, X> exceptionFunction) {
|
||||
@@ -145,10 +149,10 @@ public class CollectionPropertyValidator<T, E>
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足条件
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足指定条件
|
||||
*
|
||||
* @param condition 校验条件
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final CollectionPropertyValidator<T, E> allMatch(
|
||||
final Predicate<E> condition) {
|
||||
@@ -160,11 +164,10 @@ public class CollectionPropertyValidator<T, E>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足条件
|
||||
*
|
||||
* @param condition 校验规则
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足指定条件
|
||||
* @param condition 校验条件
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final CollectionPropertyValidator<T, E> allMatch(
|
||||
final Predicate<E> condition, final String errorMessage) {
|
||||
@@ -176,11 +179,12 @@ public class CollectionPropertyValidator<T, E>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足条件
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足指定条件
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param condition 校验条件
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> allMatch(
|
||||
final Predicate<E> condition, final Supplier<X> exceptionSupplier) {
|
||||
@@ -192,11 +196,12 @@ public class CollectionPropertyValidator<T, E>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足条件
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足指定条件
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param condition 校验条件
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> allMatch(
|
||||
final Predicate<E> condition, final Function<E, X> exceptionFunction) {
|
||||
@@ -216,11 +221,11 @@ public class CollectionPropertyValidator<T, E>
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性大小是否等于指定大小
|
||||
* 添加一条校验属性的规则,校验集合大小
|
||||
*
|
||||
* @param size 指定大小
|
||||
* @param size 预期集合大小
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final CollectionPropertyValidator<T, E> size(
|
||||
final int size, final String errorMessage) {
|
||||
@@ -228,12 +233,12 @@ public class CollectionPropertyValidator<T, E>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性大小是否等于指定大小
|
||||
* 添加一条校验属性的规则,校验集合大小
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param size 指定大小
|
||||
* @param <X> 自定义异常类型
|
||||
* @param size 预期集合大小
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> size(
|
||||
final int size, final Supplier<X> exceptionSupplier) {
|
||||
@@ -241,12 +246,12 @@ public class CollectionPropertyValidator<T, E>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性大小是否等于指定大小
|
||||
* 添加一条校验属性的规则,校验集合大小
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param size 指定大小
|
||||
* @param <X> 自定义异常类型
|
||||
* @param size 预期集合大小
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> size(
|
||||
final int size, final Function<Collection<E>, X> exceptionFunction) {
|
||||
@@ -254,12 +259,12 @@ public class CollectionPropertyValidator<T, E>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性的大小范围
|
||||
* 添加一条校验属性的规则,校验集合大小是否在指定范围内
|
||||
*
|
||||
* @param min 最小大小
|
||||
* @param max 最大大小
|
||||
* @param min 最小大小(包含)
|
||||
* @param max 最大大小(包含)
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final CollectionPropertyValidator<T, E> size(
|
||||
final int min, final int max, final String errorMessage) {
|
||||
@@ -267,12 +272,13 @@ public class CollectionPropertyValidator<T, E>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性的大小范围
|
||||
* 添加一条校验属性的规则,校验集合大小是否在指定范围内
|
||||
*
|
||||
* @param min 最小大小
|
||||
* @param max 最大大小
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小大小(包含)
|
||||
* @param max 最大大小(包含)
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> size(
|
||||
final int min, final int max, final Supplier<X> exceptionSupplier) {
|
||||
@@ -280,12 +286,13 @@ public class CollectionPropertyValidator<T, E>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性的大小范围
|
||||
* 添加一条校验属性的规则,校验集合大小是否在指定范围内
|
||||
*
|
||||
* @param min 最小大小
|
||||
* @param max 最大大小
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小大小(包含)
|
||||
* @param max 最大大小(包含)
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> size(
|
||||
final int min, final int max, final Function<Collection<E>, X> exceptionFunction) {
|
||||
|
@@ -19,17 +19,14 @@ package xyz.zhouxy.plusone.validator;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* 针对 {@code Comparable} 类型的默认属性校验器
|
||||
* {@code Comparable} 类型属性的校验器
|
||||
*
|
||||
* <p>
|
||||
* 继承自 {@link BaseComparablePropertyValidator},内置了判断属性是否在给定区间内的校验规则。
|
||||
*
|
||||
* @param <T> 待校验对象类型
|
||||
* @param <TProperty> 属性类型
|
||||
* @param <T> 待校验对象的类型
|
||||
* @param <TProperty> 待校验属性的类型,必须实现 {@code Comparable} 接口
|
||||
* @see com.google.common.collect.Range
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class ComparablePropertyValidator<T, TProperty extends Comparable<TProperty>>
|
||||
public class ComparablePropertyValidator<T, TProperty extends Comparable<? super TProperty>>
|
||||
extends BaseComparablePropertyValidator<T, TProperty, ComparablePropertyValidator<T, TProperty>> {
|
||||
|
||||
ComparablePropertyValidator(Function<T, ? extends TProperty> getter) {
|
||||
|
@@ -21,12 +21,13 @@ import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 浮点数属性校验器
|
||||
* {@code Double} 类型属性的校验器
|
||||
*
|
||||
* <p>
|
||||
* 内置对 {@code Double} 类型常用的校验规则。
|
||||
* 用于构建校验 {@code Double} 类型属性的规则链。
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @param <T> 待校验对象的类型
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class DoublePropertyValidator<T>
|
||||
extends BaseComparablePropertyValidator<T, Double, DoublePropertyValidator<T>> {
|
||||
@@ -43,7 +44,7 @@ public class DoublePropertyValidator<T>
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final DoublePropertyValidator<T> gt(final double min) {
|
||||
return withRule(Conditions.greaterThan(min),
|
||||
@@ -55,7 +56,7 @@ public class DoublePropertyValidator<T>
|
||||
*
|
||||
* @param min 最小值
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final DoublePropertyValidator<T> gt(
|
||||
final double min, final String errorMessage) {
|
||||
@@ -65,10 +66,10 @@ public class DoublePropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小值
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> DoublePropertyValidator<T> gt(
|
||||
final double min, final Supplier<X> exceptionSupplier) {
|
||||
@@ -78,10 +79,10 @@ public class DoublePropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小值
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> DoublePropertyValidator<T> gt(
|
||||
final double min, final Function<Double, X> exceptionFunction) {
|
||||
@@ -100,7 +101,7 @@ public class DoublePropertyValidator<T>
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final DoublePropertyValidator<T> ge(final double min) {
|
||||
return withRule(Conditions.greaterThanOrEqualTo(min),
|
||||
@@ -112,7 +113,7 @@ public class DoublePropertyValidator<T>
|
||||
*
|
||||
* @param min 最小值
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final DoublePropertyValidator<T> ge(
|
||||
final double min, final String errorMessage) {
|
||||
@@ -122,10 +123,10 @@ public class DoublePropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小值
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> DoublePropertyValidator<T> ge(
|
||||
final double min, final Supplier<X> exceptionSupplier) {
|
||||
@@ -135,10 +136,10 @@ public class DoublePropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小值
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> DoublePropertyValidator<T> ge(
|
||||
final double min, final Function<Double, X> exceptionFunction) {
|
||||
@@ -157,7 +158,7 @@ public class DoublePropertyValidator<T>
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final DoublePropertyValidator<T> lt(final double max) {
|
||||
return withRule(Conditions.lessThan(max),
|
||||
@@ -169,7 +170,7 @@ public class DoublePropertyValidator<T>
|
||||
*
|
||||
* @param max 最大值
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final DoublePropertyValidator<T> lt(
|
||||
final double max, final String errorMessage) {
|
||||
@@ -179,10 +180,10 @@ public class DoublePropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param max 最大值
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> DoublePropertyValidator<T> lt(
|
||||
final double max, final Supplier<X> exceptionSupplier) {
|
||||
@@ -192,10 +193,10 @@ public class DoublePropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param max 最大值
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> DoublePropertyValidator<T> lt(
|
||||
final double max, final Function<Double, X> exceptionFunction) {
|
||||
@@ -214,7 +215,7 @@ public class DoublePropertyValidator<T>
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final DoublePropertyValidator<T> le(final double max) {
|
||||
return withRule(Conditions.lessThanOrEqualTo(max),
|
||||
@@ -226,7 +227,7 @@ public class DoublePropertyValidator<T>
|
||||
*
|
||||
* @param max 最大值
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final DoublePropertyValidator<T> le(
|
||||
final double max, final String errorMessage) {
|
||||
@@ -236,10 +237,10 @@ public class DoublePropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param max 最大值
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> DoublePropertyValidator<T> le(
|
||||
final double max, final Supplier<X> exceptionSupplier) {
|
||||
@@ -249,10 +250,10 @@ public class DoublePropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param max 最大值
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> DoublePropertyValidator<T> le(
|
||||
final double max, final Function<Double, X> exceptionFunction) {
|
||||
|
@@ -19,7 +19,7 @@ package xyz.zhouxy.plusone.validator;
|
||||
/**
|
||||
* 自带校验方法,校验不通过时直接抛异常。
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*
|
||||
* @see BaseValidator
|
||||
*/
|
||||
|
@@ -18,14 +18,19 @@ package xyz.zhouxy.plusone.validator;
|
||||
/**
|
||||
* 校验器
|
||||
*
|
||||
* @author ZhouXY
|
||||
* <p>
|
||||
* 用于定义对特定类型对象的校验规则
|
||||
* </p>
|
||||
*
|
||||
* @param <T> 待校验对象的类型
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public interface IValidator<T> {
|
||||
|
||||
/**
|
||||
* 校验对象
|
||||
* 校验指定对象是否符合预定义规则
|
||||
*
|
||||
* @param obj
|
||||
* @param obj 待校验的对象实例
|
||||
*/
|
||||
void validate(T obj);
|
||||
}
|
||||
|
@@ -17,16 +17,17 @@
|
||||
package xyz.zhouxy.plusone.validator;
|
||||
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 整数属性校验器
|
||||
* {@code Integer} 类型属性的校验器
|
||||
*
|
||||
* <p>
|
||||
* 内置对 {@code Integer} 类型常用的校验规则。
|
||||
* 用于构建校验 {@code Integer} 类型属性的规则链。
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @param <T> 待校验对象的类型
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class IntPropertyValidator<T>
|
||||
extends BaseComparablePropertyValidator<T, Integer, IntPropertyValidator<T>> {
|
||||
@@ -43,9 +44,9 @@ public class IntPropertyValidator<T>
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public IntPropertyValidator<T> gt(final int min) {
|
||||
public final IntPropertyValidator<T> gt(final int min) {
|
||||
return withRule(Conditions.greaterThan(min),
|
||||
"The input must be greater than '%d'.", min);
|
||||
}
|
||||
@@ -55,9 +56,9 @@ public class IntPropertyValidator<T>
|
||||
*
|
||||
* @param min 最小值
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public IntPropertyValidator<T> gt(
|
||||
public final IntPropertyValidator<T> gt(
|
||||
final int min, final String errorMessage) {
|
||||
return withRule(Conditions.greaterThan(min), errorMessage);
|
||||
}
|
||||
@@ -65,12 +66,12 @@ public class IntPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小值
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public <X extends RuntimeException> IntPropertyValidator<T> gt(
|
||||
public final <X extends RuntimeException> IntPropertyValidator<T> gt(
|
||||
final int min, final Supplier<X> exceptionSupplier) {
|
||||
return withRule(Conditions.greaterThan(min), exceptionSupplier);
|
||||
}
|
||||
@@ -78,12 +79,12 @@ public class IntPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小值
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public <X extends RuntimeException> IntPropertyValidator<T> gt(
|
||||
public final <X extends RuntimeException> IntPropertyValidator<T> gt(
|
||||
final int min, final Function<Integer, X> exceptionFunction) {
|
||||
return withRule(Conditions.greaterThan(min), exceptionFunction);
|
||||
}
|
||||
@@ -100,9 +101,9 @@ public class IntPropertyValidator<T>
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public IntPropertyValidator<T> ge(final int min) {
|
||||
public final IntPropertyValidator<T> ge(final int min) {
|
||||
return withRule(Conditions.greaterThanOrEqualTo(min),
|
||||
"The input must be greater than or equal to '%d'.", min);
|
||||
}
|
||||
@@ -112,21 +113,21 @@ public class IntPropertyValidator<T>
|
||||
*
|
||||
* @param min 最小值
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public IntPropertyValidator<T> ge(final int min, final String errorMessage) {
|
||||
public final IntPropertyValidator<T> ge(final int min, final String errorMessage) {
|
||||
return withRule(Conditions.greaterThanOrEqualTo(min), errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小值
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public <X extends RuntimeException> IntPropertyValidator<T> ge(
|
||||
public final <X extends RuntimeException> IntPropertyValidator<T> ge(
|
||||
final int min, final Supplier<X> exceptionSupplier) {
|
||||
return withRule(Conditions.greaterThanOrEqualTo(min), exceptionSupplier);
|
||||
}
|
||||
@@ -134,12 +135,12 @@ public class IntPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小值
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public <X extends RuntimeException> IntPropertyValidator<T> ge(
|
||||
public final <X extends RuntimeException> IntPropertyValidator<T> ge(
|
||||
final int min, final Function<Integer, X> exceptionFunction) {
|
||||
return withRule(Conditions.greaterThanOrEqualTo(min), exceptionFunction);
|
||||
}
|
||||
@@ -156,9 +157,9 @@ public class IntPropertyValidator<T>
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public IntPropertyValidator<T> lt(final int max) {
|
||||
public final IntPropertyValidator<T> lt(final int max) {
|
||||
return withRule(Conditions.lessThan(max),
|
||||
"The input must be less than '%d'.", max);
|
||||
}
|
||||
@@ -168,9 +169,9 @@ public class IntPropertyValidator<T>
|
||||
*
|
||||
* @param max 最大值
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public IntPropertyValidator<T> lt(
|
||||
public final IntPropertyValidator<T> lt(
|
||||
final int max, final String errorMessage) {
|
||||
return withRule(Conditions.lessThan(max), errorMessage);
|
||||
}
|
||||
@@ -178,12 +179,12 @@ public class IntPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param max 最大值
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public <X extends RuntimeException> IntPropertyValidator<T> lt(
|
||||
public final <X extends RuntimeException> IntPropertyValidator<T> lt(
|
||||
final int max, final Supplier<X> exceptionSupplier) {
|
||||
return withRule(Conditions.lessThan(max), exceptionSupplier);
|
||||
}
|
||||
@@ -191,12 +192,12 @@ public class IntPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param max 最大值
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public <X extends RuntimeException> IntPropertyValidator<T> lt(
|
||||
public final <X extends RuntimeException> IntPropertyValidator<T> lt(
|
||||
final int max, final Function<Integer, X> exceptionFunction) {
|
||||
return withRule(Conditions.lessThan(max), exceptionFunction);
|
||||
}
|
||||
@@ -213,9 +214,9 @@ public class IntPropertyValidator<T>
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public IntPropertyValidator<T> le(final int max) {
|
||||
public final IntPropertyValidator<T> le(final int max) {
|
||||
return withRule(Conditions.lessThanOrEqualTo(max),
|
||||
"The input must be less than or equal to '%d'.", max);
|
||||
}
|
||||
@@ -225,9 +226,9 @@ public class IntPropertyValidator<T>
|
||||
*
|
||||
* @param max 最大值
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public IntPropertyValidator<T> le(
|
||||
public final IntPropertyValidator<T> le(
|
||||
final int max, final String errorMessage) {
|
||||
return withRule(Conditions.lessThanOrEqualTo(max), errorMessage);
|
||||
}
|
||||
@@ -235,12 +236,12 @@ public class IntPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param max 最大值
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public <X extends RuntimeException> IntPropertyValidator<T> le(
|
||||
public final <X extends RuntimeException> IntPropertyValidator<T> le(
|
||||
final int max, final Supplier<X> exceptionSupplier) {
|
||||
return withRule(Conditions.lessThanOrEqualTo(max), exceptionSupplier);
|
||||
}
|
||||
@@ -248,12 +249,12 @@ public class IntPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param max 最大值
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public <X extends RuntimeException> IntPropertyValidator<T> le(
|
||||
public final <X extends RuntimeException> IntPropertyValidator<T> le(
|
||||
final int max, final Function<Integer, X> exceptionFunction) {
|
||||
return withRule(Conditions.lessThanOrEqualTo(max), exceptionFunction);
|
||||
}
|
||||
|
@@ -21,12 +21,13 @@ import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* 长整数属性校验器
|
||||
* {@code Long} 类型属性的校验器
|
||||
*
|
||||
* <p>
|
||||
* 内置对 {@code Long} 类型常用的校验规则。
|
||||
* 用于构建校验 {@code Long} 类型属性的规则链。
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @param <T> 待校验对象的类型
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class LongPropertyValidator<T>
|
||||
extends BaseComparablePropertyValidator<T, Long, LongPropertyValidator<T>> {
|
||||
@@ -43,7 +44,7 @@ public class LongPropertyValidator<T>
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final LongPropertyValidator<T> gt(final long min) {
|
||||
return withRule(Conditions.greaterThan(min),
|
||||
@@ -55,7 +56,7 @@ public class LongPropertyValidator<T>
|
||||
*
|
||||
* @param min 最小值
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final LongPropertyValidator<T> gt(
|
||||
final long min, final String errorMessage) {
|
||||
@@ -65,10 +66,10 @@ public class LongPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小值
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> LongPropertyValidator<T> gt(
|
||||
final long min, final Supplier<X> exceptionSupplier) {
|
||||
@@ -78,10 +79,10 @@ public class LongPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小值
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> LongPropertyValidator<T> gt(
|
||||
final long min, final Function<Long, X> exceptionFunction) {
|
||||
@@ -100,7 +101,7 @@ public class LongPropertyValidator<T>
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param min 最小值
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final LongPropertyValidator<T> ge(final long min) {
|
||||
return withRule(Conditions.greaterThanOrEqualTo(min),
|
||||
@@ -112,20 +113,19 @@ public class LongPropertyValidator<T>
|
||||
*
|
||||
* @param min 最小值
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final LongPropertyValidator<T> ge(
|
||||
final long min, final String errorMessage) {
|
||||
public final LongPropertyValidator<T> ge(final long min, final String errorMessage) {
|
||||
return withRule(Conditions.greaterThanOrEqualTo(min), errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小值
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> LongPropertyValidator<T> ge(
|
||||
final long min, final Supplier<X> exceptionSupplier) {
|
||||
@@ -135,10 +135,10 @@ public class LongPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否大于等于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小值
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> LongPropertyValidator<T> ge(
|
||||
final long min, final Function<Long, X> exceptionFunction) {
|
||||
@@ -157,7 +157,7 @@ public class LongPropertyValidator<T>
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final LongPropertyValidator<T> lt(final long max) {
|
||||
return withRule(Conditions.lessThan(max),
|
||||
@@ -169,7 +169,7 @@ public class LongPropertyValidator<T>
|
||||
*
|
||||
* @param max 最大值
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final LongPropertyValidator<T> lt(
|
||||
final long max, final String errorMessage) {
|
||||
@@ -179,10 +179,10 @@ public class LongPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param max 最大值
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> LongPropertyValidator<T> lt(
|
||||
final long max, final Supplier<X> exceptionSupplier) {
|
||||
@@ -192,10 +192,10 @@ public class LongPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param max 最大值
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> LongPropertyValidator<T> lt(
|
||||
final long max, final Function<Long, X> exceptionFunction) {
|
||||
@@ -214,7 +214,7 @@ public class LongPropertyValidator<T>
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param max 最大值
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final LongPropertyValidator<T> le(final long max) {
|
||||
return withRule(Conditions.lessThanOrEqualTo(max),
|
||||
@@ -226,7 +226,7 @@ public class LongPropertyValidator<T>
|
||||
*
|
||||
* @param max 最大值
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final LongPropertyValidator<T> le(
|
||||
final long max, final String errorMessage) {
|
||||
@@ -236,10 +236,10 @@ public class LongPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param max 最大值
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> LongPropertyValidator<T> le(
|
||||
final long max, final Supplier<X> exceptionSupplier) {
|
||||
@@ -249,10 +249,10 @@ public class LongPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否小于等于给定值
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param max 最大值
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前验证器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> LongPropertyValidator<T> le(
|
||||
final long max, final Function<Long, X> exceptionFunction) {
|
||||
|
@@ -16,11 +16,9 @@
|
||||
|
||||
package xyz.zhouxy.plusone.validator;
|
||||
|
||||
import java.util.AbstractMap.SimpleImmutableEntry;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -31,7 +29,7 @@ import java.util.stream.Collectors;
|
||||
* <p>
|
||||
* 校验后拷贝出一个新的 Map 对象,仅保留指定的 key。
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public abstract class MapValidator<K, V> extends BaseValidator<Map<K, V>> {
|
||||
|
||||
@@ -175,7 +173,8 @@ public abstract class MapValidator<K, V> extends BaseValidator<Map<K, V>> {
|
||||
* @param key key
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final <E extends Comparable<E>> ComparablePropertyValidator<Map<K, V>, E> ruleForComparable(K key) {
|
||||
protected final <E extends Comparable<? super E>> //
|
||||
ComparablePropertyValidator<Map<K, V>, E> ruleForComparable(K key) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Function<Map<K, V>, E> getter = m -> (E) m.get(key);
|
||||
return ruleForComparable(getter);
|
||||
@@ -196,18 +195,19 @@ public abstract class MapValidator<K, V> extends BaseValidator<Map<K, V>> {
|
||||
|
||||
/**
|
||||
* 添加一个属性校验器,对指定的两个 key 对应的 value 进行校验
|
||||
* @param <V1> 第一个属性的类型
|
||||
* @param <V2> 第二个属性的类型
|
||||
* @param k1 第一个 key
|
||||
* @param k2 第二个 key
|
||||
* @param <V1> 第1个属性的类型
|
||||
* @param <V2> 第2个属性的类型
|
||||
* @param k1 第1个 key
|
||||
* @param k2 第2个 key
|
||||
* @return 属性校验器
|
||||
*/
|
||||
protected final <V1 extends V, V2 extends V>
|
||||
PairPropertyValidator<Map<K, V>, V1, V2> ruleForPair(K k1, K k2) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Function<Map<K, V>, Entry<V1, V2>> getter = m ->
|
||||
new SimpleImmutableEntry<>((V1) m.get(k1), (V2) m.get(k2));
|
||||
return ruleForPair(getter);
|
||||
final Function<Map<K, V>, V1> v1Getter = m -> (V1) m.get(k1);
|
||||
@SuppressWarnings("unchecked")
|
||||
final Function<Map<K, V>, V2> v2Getter = m -> (V2) m.get(k2);
|
||||
return ruleFor(v1Getter, v2Getter);
|
||||
}
|
||||
|
||||
// ================================
|
||||
|
@@ -21,7 +21,9 @@ import java.util.function.Function;
|
||||
/**
|
||||
* 通用类型属性校验器。继承自 {@link BasePropertyValidator},包含针对属性的校验规则。
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @param <T> 待校验对象的类型
|
||||
* @param <TProperty> 待校验属性的类型
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class ObjectPropertyValidator<T, TProperty>
|
||||
extends BasePropertyValidator<T, TProperty, ObjectPropertyValidator<T, TProperty>> {
|
||||
|
@@ -24,7 +24,10 @@ import java.util.function.Supplier;
|
||||
/**
|
||||
* 针对二元组的属性校验器
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @param <T> 被验证对象类型
|
||||
* @param <V1> 第一个元素的类型
|
||||
* @param <V2> 第二个元素的类型
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class PairPropertyValidator<T, V1, V2>
|
||||
extends BasePropertyValidator<T, Entry<V1, V2>, PairPropertyValidator<T, V1, V2>> {
|
||||
@@ -57,6 +60,7 @@ public class PairPropertyValidator<T, V1, V2>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验二元组是否满足给定的条件
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param condition 校验条件
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
@@ -66,9 +70,10 @@ public class PairPropertyValidator<T, V1, V2>
|
||||
return must(pair -> condition.test(pair.getKey(), pair.getValue()), exceptionSupplier);
|
||||
}
|
||||
|
||||
/**
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验二元组是否满足给定的条件
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param condition 校验条件
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
|
@@ -29,9 +29,13 @@ import xyz.zhouxy.plusone.commons.util.RegexTools;
|
||||
import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
|
||||
/**
|
||||
* 针对文本类型的属性校验器。
|
||||
* {@code String} 类型属性的校验器
|
||||
*
|
||||
* @author ZhouXY
|
||||
* <p>
|
||||
* 用于构建校验 {@code String} 类型属性的规则链。
|
||||
*
|
||||
* @param <T> 待校验对象的类型
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class StringPropertyValidator<T>
|
||||
extends BaseComparablePropertyValidator<T, String, StringPropertyValidator<T>> {
|
||||
@@ -49,7 +53,7 @@ public class StringPropertyValidator<T>
|
||||
*
|
||||
* @param pattern 正则表达式
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final StringPropertyValidator<T> matches(
|
||||
final Pattern pattern, final String errorMessage) {
|
||||
@@ -59,10 +63,10 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配正则表达式
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param pattern 正则表达式
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public <X extends RuntimeException> StringPropertyValidator<T> matches(
|
||||
final Pattern pattern, final Supplier<X> exceptionSupplier) {
|
||||
@@ -72,10 +76,10 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配正则表达式
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param pattern 正则表达式
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> matches(
|
||||
final Pattern pattern, final Function<String, X> exceptionFunction) {
|
||||
@@ -91,11 +95,11 @@ public class StringPropertyValidator<T>
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的任一正则表达式
|
||||
*
|
||||
* @param patterns 正则表达式
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final StringPropertyValidator<T> matchesAny(
|
||||
final Pattern[] patterns, final String errorMessage) {
|
||||
@@ -103,12 +107,12 @@ public class StringPropertyValidator<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的任一正则表达式
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param patterns 正则表达式
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> matchesAny(
|
||||
final Pattern[] patterns, final Supplier<X> exceptionSupplier) {
|
||||
@@ -116,12 +120,12 @@ public class StringPropertyValidator<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的任一正则表达式
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param patterns 正则表达式
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> matchesAny(
|
||||
final Pattern[] patterns, final Function<String, X> exceptionFunction) {
|
||||
@@ -129,11 +133,11 @@ public class StringPropertyValidator<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的任一正则表达式
|
||||
*
|
||||
* @param patterns 正则表达式
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final StringPropertyValidator<T> matchesAny(
|
||||
final Collection<Pattern> patterns, final String errorMessage) {
|
||||
@@ -141,12 +145,12 @@ public class StringPropertyValidator<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的任一正则表达式
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param patterns 正则表达式
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> matchesAny(
|
||||
final Collection<Pattern> patterns, final Supplier<X> exceptionSupplier) {
|
||||
@@ -154,12 +158,12 @@ public class StringPropertyValidator<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的多个正则表达式的其中一个
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的任一正则表达式
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param patterns 正则表达式
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> matchesAny(
|
||||
final Collection<Pattern> patterns, final Function<String, X> exceptionFunction) {
|
||||
@@ -179,7 +183,7 @@ public class StringPropertyValidator<T>
|
||||
*
|
||||
* @param patterns 正则表达式
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final StringPropertyValidator<T> matchesAll(
|
||||
final Pattern[] patterns, final String errorMessage) {
|
||||
@@ -189,10 +193,10 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的所有正则表达式
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param patterns 正则表达式
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> matchesAll(
|
||||
final Pattern[] patterns, final Supplier<X> exceptionSupplier) {
|
||||
@@ -202,10 +206,10 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的所有正则表达式
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param patterns 正则表达式
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> matchesAll(
|
||||
final Pattern[] patterns, final Function<String, X> exceptionFunction) {
|
||||
@@ -217,7 +221,7 @@ public class StringPropertyValidator<T>
|
||||
*
|
||||
* @param patterns 正则表达式
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final StringPropertyValidator<T> matchesAll(
|
||||
final Collection<Pattern> patterns, final String errorMessage) {
|
||||
@@ -227,10 +231,10 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的所有正则表达式
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param patterns 正则表达式
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> matchesAll(
|
||||
final Collection<Pattern> patterns, final Supplier<X> exceptionSupplier) {
|
||||
@@ -240,10 +244,10 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否匹配指定的所有正则表达式
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param patterns 正则表达式
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> matchesAll(
|
||||
final Collection<Pattern> patterns, final Function<String, X> exceptionFunction) {
|
||||
@@ -261,7 +265,7 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否不为空白字符串
|
||||
*
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final StringPropertyValidator<T> notBlank() {
|
||||
return withRule(Conditions.notBlank(),
|
||||
@@ -272,7 +276,7 @@ public class StringPropertyValidator<T>
|
||||
* 添加一条校验属性的规则,校验属性是否不为空白字符串
|
||||
*
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final StringPropertyValidator<T> notBlank(final String errorMessage) {
|
||||
return withRule(Conditions.notBlank(), errorMessage);
|
||||
@@ -281,9 +285,9 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否不为空白字符串
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> notBlank(
|
||||
final Supplier<X> exceptionSupplier) {
|
||||
@@ -293,9 +297,9 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否不为空白字符串
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> notBlank(
|
||||
final Function<String, X> exceptionFunction) {
|
||||
@@ -311,9 +315,9 @@ public class StringPropertyValidator<T>
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否是邮箱地址
|
||||
* 添加一条校验属性的规则,校验属性是否满足邮箱格式
|
||||
*
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final StringPropertyValidator<T> emailAddress() {
|
||||
return withRule(Conditions.emailAddress(),
|
||||
@@ -321,21 +325,21 @@ public class StringPropertyValidator<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否是邮箱地址
|
||||
* 添加一条校验属性的规则,校验属性是否满足邮箱格式
|
||||
*
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final StringPropertyValidator<T> emailAddress(final String errorMessage) {
|
||||
return withRule(Conditions.emailAddress(), errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否是邮箱地址
|
||||
* 添加一条校验属性的规则,校验属性是否满足邮箱格式
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> emailAddress(
|
||||
final Supplier<X> exceptionSupplier) {
|
||||
@@ -343,11 +347,11 @@ public class StringPropertyValidator<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否是邮箱地址
|
||||
* 添加一条校验属性的规则,校验属性是否满足邮箱格式
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> emailAddress(
|
||||
final Function<String, X> exceptionFunction) {
|
||||
@@ -365,7 +369,7 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验字符串属性是否不为空
|
||||
*
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final StringPropertyValidator<T> notEmpty() {
|
||||
return withRule(Conditions.notEmpty(),
|
||||
@@ -376,7 +380,7 @@ public class StringPropertyValidator<T>
|
||||
* 添加一条校验属性的规则,校验字符串属性是否不为空
|
||||
*
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final StringPropertyValidator<T> notEmpty(final String errorMessage) {
|
||||
return withRule(Conditions.notEmpty(), errorMessage);
|
||||
@@ -385,9 +389,9 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验字符串属性是否不为空
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> notEmpty(
|
||||
final Supplier<X> exceptionSupplier) {
|
||||
@@ -397,9 +401,9 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验字符串属性是否不为空
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> notEmpty(
|
||||
final Function<String, X> exceptionFunction) {
|
||||
@@ -419,7 +423,7 @@ public class StringPropertyValidator<T>
|
||||
*
|
||||
* @param length 指定长度
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final StringPropertyValidator<T> length(
|
||||
final int length, final String errorMessage) {
|
||||
@@ -429,10 +433,10 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性长度是否等于指定长度
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param length 指定长度
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> length(
|
||||
final int length, final Supplier<X> exceptionSupplier) {
|
||||
@@ -442,10 +446,10 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性长度是否等于指定长度
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param <X> 自定义异常类型
|
||||
* @param length 指定长度
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> length(
|
||||
final int length, final Function<String, X> exceptionFunction) {
|
||||
@@ -455,10 +459,10 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性的长度范围
|
||||
*
|
||||
* @param min 最小长度
|
||||
* @param max 最大长度
|
||||
* @param min 最小长度(包含)
|
||||
* @param max 最大长度(包含)
|
||||
* @param errorMessage 异常信息
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final StringPropertyValidator<T> length(
|
||||
final int min, final int max, final String errorMessage) {
|
||||
@@ -468,11 +472,11 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性的长度范围
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param min 最小长度
|
||||
* @param max 最大长度
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小长度(包含)
|
||||
* @param max 最大长度(包含)
|
||||
* @param exceptionSupplier 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> length(
|
||||
final int min, final int max, final Supplier<X> exceptionSupplier) {
|
||||
@@ -482,11 +486,11 @@ public class StringPropertyValidator<T>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性的长度范围
|
||||
*
|
||||
* @param <X> 异常类型
|
||||
* @param min 最小长度
|
||||
* @param max 最大长度
|
||||
* @param <X> 自定义异常类型
|
||||
* @param min 最小长度(包含)
|
||||
* @param max 最大长度(包含)
|
||||
* @param exceptionFunction 自定义异常
|
||||
* @return 属性校验器
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final <X extends RuntimeException> StringPropertyValidator<T> length(
|
||||
final int min, final int max, final Function<String, X> exceptionFunction) {
|
||||
|
@@ -16,9 +16,9 @@
|
||||
package xyz.zhouxy.plusone.validator;
|
||||
|
||||
/**
|
||||
* 验证失败的异常
|
||||
* 校验失败异常
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class ValidationException extends RuntimeException {
|
||||
|
||||
@@ -39,51 +39,51 @@ public class ValidationException extends RuntimeException {
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个验证失败异常
|
||||
* 创建 {@code ValidationException} 实例
|
||||
*
|
||||
* @return 异常
|
||||
* @return {@code ValidationException} 实例
|
||||
*/
|
||||
public static ValidationException withDefaultMessage() {
|
||||
return new ValidationException(DEFAULT_MESSAGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个验证失败异常
|
||||
* 创建 {@code ValidationException} 实例
|
||||
*
|
||||
* @param message 错误信息
|
||||
* @return 异常
|
||||
* @param message 异常信息
|
||||
* @return {@code ValidationException} 实例
|
||||
*/
|
||||
public static ValidationException withMessage(String message) {
|
||||
return new ValidationException(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个验证失败异常
|
||||
* 创建 {@code ValidationException} 实例
|
||||
*
|
||||
* @param errorMessageTemplate 异常信息模版
|
||||
* @param errorMessageArgs 异常信息参数
|
||||
* @return 异常
|
||||
* @param errorMessageTemplate 异常信息模板
|
||||
* @param errorMessageArgs 异常信息参数列表
|
||||
* @return {@code ValidationException} 实例
|
||||
*/
|
||||
public static ValidationException withMessage(String errorMessageTemplate, Object... errorMessageArgs) {
|
||||
return new ValidationException(String.format(errorMessageTemplate, errorMessageArgs));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个验证失败异常
|
||||
* 创建 {@code ValidationException} 实例
|
||||
*
|
||||
* @param cause 错误 cause
|
||||
* @return 异常
|
||||
* @param cause 导致校验失败的根本异常
|
||||
* @return {@code ValidationException} 实例
|
||||
*/
|
||||
public static ValidationException withCause(Throwable cause) {
|
||||
return new ValidationException(cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个验证失败异常
|
||||
* 创建 {@code ValidationException} 实例
|
||||
*
|
||||
* @param message 错误信息
|
||||
* @param cause 错误 cause
|
||||
* @return 异常
|
||||
* @param message 异常信息
|
||||
* @param cause 导致校验失败的根本异常
|
||||
* @return {@code ValidationException} 实例
|
||||
*/
|
||||
public static ValidationException withMessageAndCause(String message, Throwable cause) {
|
||||
return new ValidationException(message, cause);
|
||||
|
@@ -21,7 +21,7 @@ import java.util.function.Function;
|
||||
/**
|
||||
* Function<T, Boolean>
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ToBoolObjectFunction<T> extends Function<T, Boolean>, Serializable {
|
||||
|
@@ -21,7 +21,7 @@ import java.util.function.Function;
|
||||
/**
|
||||
* Function<T, Double>
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ToDoubleObjectFunction<T> extends Function<T, Double>, Serializable {
|
||||
|
@@ -21,7 +21,7 @@ import java.util.function.Function;
|
||||
/**
|
||||
* Function<T, Integer>
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ToIntegerFunction<T> extends Function<T, Integer>, Serializable {
|
||||
|
@@ -21,7 +21,7 @@ import java.util.function.Function;
|
||||
/**
|
||||
* Function<T, Long>
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ToLongObjectFunction<T> extends Function<T, Long>, Serializable {
|
||||
|
@@ -21,7 +21,7 @@ import java.util.function.Function;
|
||||
/**
|
||||
* Function<T, String>
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface ToStringFunction<T> extends Function<T, String>, Serializable {
|
||||
|
@@ -31,6 +31,7 @@ import xyz.zhouxy.plusone.validator.BaseValidator;
|
||||
import xyz.zhouxy.plusone.validator.IValidator;
|
||||
import xyz.zhouxy.plusone.validator.ValidationException;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public class PairPropertyValidatorTests {
|
||||
|
||||
static final String MESSAGE = "Validation failed.";
|
||||
@@ -41,7 +42,9 @@ public class PairPropertyValidatorTests {
|
||||
|
||||
@Test
|
||||
void must_validInput() {
|
||||
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||
ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "100");
|
||||
|
||||
IValidator<ExampleCommand> validator1 = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry<String,Integer>(command.getStringProperty(), command.getIntProperty()))
|
||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()))
|
||||
@@ -51,69 +54,113 @@ public class PairPropertyValidatorTests {
|
||||
(str, intValue) -> ExampleException.withMessage("Validation failed: ('%s', %d).", str, intValue));
|
||||
}
|
||||
};
|
||||
assertDoesNotThrow(() -> validator1.validate(command));
|
||||
|
||||
ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "100");
|
||||
assertDoesNotThrow(() -> validator.validate(command));
|
||||
IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleFor(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty)
|
||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()))
|
||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()), MESSAGE)
|
||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()), () -> ExampleException.withMessage(MESSAGE))
|
||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()),
|
||||
(str, intValue) -> ExampleException.withMessage("Validation failed: ('%s', %d).", str, intValue));
|
||||
}
|
||||
};
|
||||
assertDoesNotThrow(() -> validator2.validate(command));
|
||||
}
|
||||
|
||||
@Test
|
||||
void must_default_invalidInput() {
|
||||
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||
ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "");
|
||||
|
||||
IValidator<ExampleCommand> validator1 = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry<String,Integer>(command.getStringProperty(), command.getIntProperty()))
|
||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()));
|
||||
}
|
||||
};
|
||||
ValidationException e1 = assertThrows(ValidationException.class, () -> validator1.validate(command));
|
||||
assertEquals("The specified condition was not met for the input.", e1.getMessage());
|
||||
|
||||
ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "");
|
||||
|
||||
ValidationException e = assertThrows(ValidationException.class, () -> validator.validate(command));
|
||||
assertEquals("The specified condition was not met for the input.", e.getMessage());
|
||||
IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleFor(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty)
|
||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()));
|
||||
}
|
||||
};
|
||||
ValidationException e2 = assertThrows(ValidationException.class, () -> validator2.validate(command));
|
||||
assertEquals("The specified condition was not met for the input.", e2.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void must_message_invalidInput() {
|
||||
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||
ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "");
|
||||
|
||||
IValidator<ExampleCommand> validator1 = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry<String,Integer>(command.getStringProperty(), command.getIntProperty()))
|
||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()), MESSAGE);
|
||||
}
|
||||
};
|
||||
ValidationException e1 = assertThrows(ValidationException.class, () -> validator1.validate(command));
|
||||
assertEquals(MESSAGE, e1.getMessage());
|
||||
|
||||
ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "");
|
||||
ValidationException e = assertThrows(ValidationException.class, () -> validator.validate(command));
|
||||
assertEquals(MESSAGE, e.getMessage());
|
||||
IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleFor(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty)
|
||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()), MESSAGE);
|
||||
}
|
||||
};
|
||||
ValidationException e2 = assertThrows(ValidationException.class, () -> validator2.validate(command));
|
||||
assertEquals(MESSAGE, e2.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void must_exceptionSupplier_invalidInput() {
|
||||
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||
ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "");
|
||||
|
||||
IValidator<ExampleCommand> validator1 = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry<String,Integer>(command.getStringProperty(), command.getIntProperty()))
|
||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()), () -> ExampleException.withMessage(MESSAGE));
|
||||
}
|
||||
};
|
||||
ExampleException e1 = assertThrows(ExampleException.class, () -> validator1.validate(command));
|
||||
assertEquals(MESSAGE, e1.getMessage());
|
||||
|
||||
ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "");
|
||||
|
||||
ExampleException e = assertThrows(ExampleException.class, () -> validator.validate(command));
|
||||
assertEquals(MESSAGE, e.getMessage());
|
||||
IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleFor(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty)
|
||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()), () -> ExampleException.withMessage(MESSAGE));
|
||||
}
|
||||
};
|
||||
ExampleException e2 = assertThrows(ExampleException.class, () -> validator2.validate(command));
|
||||
assertEquals(MESSAGE, e2.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void must_exceptionFunction_invalidInput() {
|
||||
IValidator<ExampleCommand> validator = new BaseValidator<ExampleCommand>() {
|
||||
ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "");
|
||||
|
||||
IValidator<ExampleCommand> validator1 = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleForPair((ExampleCommand command) -> new SimpleImmutableEntry<String,Integer>(command.getStringProperty(), command.getIntProperty()))
|
||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()),
|
||||
(str, intValue) -> ExampleException.withMessage("Validation failed: ('%s', %d).", str, intValue));
|
||||
}
|
||||
};
|
||||
ExampleException e1 = assertThrows(ExampleException.class, () -> validator1.validate(command));
|
||||
assertEquals("Validation failed: ('', 100).", e1.getMessage());
|
||||
|
||||
ExampleCommand command = exampleCommandWithIntAndStringListProperty(100, "");
|
||||
|
||||
ExampleException e = assertThrows(ExampleException.class, () -> validator.validate(command));
|
||||
assertEquals("Validation failed: ('', 100).", e.getMessage());
|
||||
IValidator<ExampleCommand> validator2 = new BaseValidator<ExampleCommand>() {
|
||||
{
|
||||
ruleFor(ExampleCommand::getStringProperty, ExampleCommand::getIntProperty)
|
||||
.must((str, intValue) -> Objects.equals(str, intValue.toString()),
|
||||
(str, intValue) -> ExampleException.withMessage("Validation failed: ('%s', %d).", str, intValue));
|
||||
}
|
||||
};
|
||||
ExampleException e2 = assertThrows(ExampleException.class, () -> validator2.validate(command));
|
||||
assertEquals("Validation failed: ('', 100).", e2.getMessage());
|
||||
}
|
||||
|
||||
// ================================
|
||||
|
@@ -19,6 +19,7 @@ package xyz.zhouxy.plusone.map.validator;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -58,7 +59,7 @@ class MapValidatorTests {
|
||||
params.put(ParamsValidator.STRING_PROPERTY2, "Foo");
|
||||
assertAll(() -> {
|
||||
Map<String, Object> validatedParams = validator.validateAndCopy(params);
|
||||
assertEquals(ParamsValidator.keySet(), validatedParams.keySet());
|
||||
assertEquals(ImmutableSet.copyOf(ParamsValidator.reservedProperties()), validatedParams.keySet());
|
||||
|
||||
assertEquals(true, validatedParams.get(ParamsValidator.BOOL_PROPERTY));
|
||||
assertEquals(Integer.MAX_VALUE, validatedParams.get(ParamsValidator.INT_PROPERTY));
|
||||
@@ -103,11 +104,14 @@ class ParamsValidator extends MapValidator<String, Object> {
|
||||
public static final String OBJECT_PROPERTY = "objectProperty";
|
||||
public static final String STRING_LIST_PROPERTY = "stringListProperty";
|
||||
|
||||
private static final String[] RESERVED_PROPERTIES = {
|
||||
BOOL_PROPERTY, INT_PROPERTY, LONG_PROPERTY, DOUBLE_PROPERTY, STRING_PROPERTY,
|
||||
DATE_TIME_PROPERTY, OBJECT_PROPERTY, STRING_LIST_PROPERTY };
|
||||
|
||||
public static final ParamsValidator INSTANCE = new ParamsValidator();
|
||||
|
||||
private ParamsValidator() {
|
||||
super(new String[] { BOOL_PROPERTY, INT_PROPERTY, LONG_PROPERTY, DOUBLE_PROPERTY, STRING_PROPERTY,
|
||||
DATE_TIME_PROPERTY, OBJECT_PROPERTY, STRING_LIST_PROPERTY });
|
||||
super(RESERVED_PROPERTIES);
|
||||
ruleForBool(BOOL_PROPERTY)
|
||||
.notNull();
|
||||
ruleForInt(INT_PROPERTY)
|
||||
@@ -118,20 +122,19 @@ class ParamsValidator extends MapValidator<String, Object> {
|
||||
.notNull(d -> ExampleException.withMessage("The doubleProperty cannot be null, but it was %s", d));
|
||||
ruleForString(STRING_PROPERTY)
|
||||
.notNull();
|
||||
ruleForComparable(DATE_TIME_PROPERTY)
|
||||
this.<LocalDateTime>ruleForComparable(DATE_TIME_PROPERTY)
|
||||
.notNull("The dateTimeProperty cannot be null");
|
||||
ruleFor(OBJECT_PROPERTY)
|
||||
.notNull(() -> ExampleException.withMessage("The objectProperty cannot be null"));
|
||||
ruleForCollection(STRING_LIST_PROPERTY)
|
||||
this.<String>ruleForCollection(STRING_LIST_PROPERTY)
|
||||
.notNull(d -> ExampleException.withMessage("The stringListProperty cannot be null, but it was %s", d));
|
||||
|
||||
ruleForPair(STRING_PROPERTY, STRING_PROPERTY2)
|
||||
this.<String, String>ruleForPair(STRING_PROPERTY, STRING_PROPERTY2)
|
||||
.must((str1, str2) -> str1 != null && str1.equals(str2),
|
||||
"'stringProperty' must be equal to 'stringProperty2'.");
|
||||
}
|
||||
|
||||
public static Set<String> keySet() {
|
||||
return ImmutableSet.of(BOOL_PROPERTY, INT_PROPERTY, LONG_PROPERTY, DOUBLE_PROPERTY, STRING_PROPERTY,
|
||||
DATE_TIME_PROPERTY, OBJECT_PROPERTY, STRING_LIST_PROPERTY);
|
||||
public static String[] reservedProperties() {
|
||||
return Arrays.copyOf(RESERVED_PROPERTIES, RESERVED_PROPERTIES.length);
|
||||
}
|
||||
}
|
||||
|
17
pom.xml
17
pom.xml
@@ -6,10 +6,10 @@
|
||||
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-validator-parent</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<version>1.0.0-RC3</version>
|
||||
|
||||
<name>plusone-validator-parent</name>
|
||||
<url>http://zhouxy.xyz</url>
|
||||
<url>http://gitea.zhouxy.xyz/plusone/plusone-validator</url>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
||||
@@ -18,11 +18,15 @@
|
||||
</modules>
|
||||
|
||||
<description>
|
||||
Plusone Validator 是一个校验库,用于构建校验规则对对象(尤其是入参)进行校验。API 参考自 .NET 的 FluentValidation。
|
||||
Plusone Validator 是一个校验库,使用 lambda 表达式(包括方法引用)和流式 API 构建校验规则,对对象进行校验。
|
||||
</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>
|
||||
|
||||
<plusone-commons.version>1.1.0-RC1</plusone-commons.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
@@ -30,10 +34,15 @@
|
||||
<dependency>
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-dependencies</artifactId>
|
||||
<version>1.1.0-SNAPSHOT</version>
|
||||
<version>${plusone-commons.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-commons</artifactId>
|
||||
<version>${plusone-commons.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
Reference in New Issue
Block a user