forked from plusone/plusone-validator
Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
483a3d5490 | |||
5705686375 | |||
5b5d7b50d6 | |||
aad5b6b4fe | |||
75a3c7798a | |||
7d0a25144b |
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-RC2</version>
|
||||
<version>1.0.0-RC3</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>plusone-validator</artifactId>
|
||||
<name>plusone-validator</name>
|
||||
<url>http://zhouxy.xyz</url>
|
||||
|
||||
<description>
|
||||
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-RC1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
|
@@ -31,7 +31,7 @@ import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
*
|
||||
* @param <T> 待校验对象的类型
|
||||
* @param <E> 数组元素的类型
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class ArrayPropertyValidator<T, E>
|
||||
extends BasePropertyValidator<T, E[], ArrayPropertyValidator<T, E>> {
|
||||
|
@@ -29,11 +29,11 @@ import com.google.common.collect.Range;
|
||||
* @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> {
|
||||
|
||||
@@ -47,9 +47,9 @@ public abstract class BaseComparablePropertyValidator<
|
||||
* @param range 区间
|
||||
* @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));
|
||||
"The input must in the interval %s. You entered %s.", range, value));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,7 +60,7 @@ public abstract class BaseComparablePropertyValidator<
|
||||
* @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);
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ public abstract class BaseComparablePropertyValidator<
|
||||
* @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);
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ public abstract class BaseComparablePropertyValidator<
|
||||
* @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);
|
||||
}
|
||||
|
||||
@@ -95,8 +95,8 @@ public abstract class BaseComparablePropertyValidator<
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@@ -33,7 +33,7 @@ import java.util.function.Supplier;
|
||||
* @param <T> 待校验对象的类型
|
||||
* @param <TProperty> 待校验属性的类型
|
||||
* @param <TPropertyValidator> 具体校验器类型,用于支持链式调用
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public abstract class BasePropertyValidator<
|
||||
T,
|
||||
|
@@ -34,7 +34,7 @@ import xyz.zhouxy.plusone.validator.function.*;
|
||||
* 子类可通过添加不同的校验规则,构建完整的校验逻辑,用于校验对象。
|
||||
*
|
||||
* @param <T> 待校验对象的类型
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
|
||||
@@ -109,7 +109,7 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
* 示例:{@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);
|
||||
|
@@ -27,7 +27,7 @@ import java.util.function.Supplier;
|
||||
* 用于构建校验 {@code Boolean} 类型属性的规则链。
|
||||
*
|
||||
* @param <T> 待校验对象的类型
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class BoolPropertyValidator<T>
|
||||
extends BasePropertyValidator<T, Boolean, BoolPropertyValidator<T>> {
|
||||
|
@@ -32,7 +32,7 @@ import xyz.zhouxy.plusone.commons.util.AssertTools;
|
||||
*
|
||||
* @param <T> 待校验对象的类型
|
||||
* @param <E> 集合元素的类型
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class CollectionPropertyValidator<T, E>
|
||||
extends BasePropertyValidator<T, Collection<E>, CollectionPropertyValidator<T, E>> {
|
||||
|
@@ -24,9 +24,9 @@ import java.util.function.Function;
|
||||
* @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) {
|
||||
|
@@ -27,7 +27,7 @@ import java.util.function.Supplier;
|
||||
* 用于构建校验 {@code Double} 类型属性的规则链。
|
||||
*
|
||||
* @param <T> 待校验对象的类型
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class DoublePropertyValidator<T>
|
||||
extends BaseComparablePropertyValidator<T, Double, DoublePropertyValidator<T>> {
|
||||
|
@@ -19,7 +19,7 @@ package xyz.zhouxy.plusone.validator;
|
||||
/**
|
||||
* 自带校验方法,校验不通过时直接抛异常。
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*
|
||||
* @see BaseValidator
|
||||
*/
|
||||
|
@@ -23,7 +23,7 @@ package xyz.zhouxy.plusone.validator;
|
||||
* </p>
|
||||
*
|
||||
* @param <T> 待校验对象的类型
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public interface IValidator<T> {
|
||||
|
||||
|
@@ -27,7 +27,7 @@ import java.util.function.Supplier;
|
||||
* 用于构建校验 {@code Integer} 类型属性的规则链。
|
||||
*
|
||||
* @param <T> 待校验对象的类型
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class IntPropertyValidator<T>
|
||||
extends BaseComparablePropertyValidator<T, Integer, IntPropertyValidator<T>> {
|
||||
|
@@ -27,7 +27,7 @@ import java.util.function.Supplier;
|
||||
* 用于构建校验 {@code Long} 类型属性的规则链。
|
||||
*
|
||||
* @param <T> 待校验对象的类型
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class LongPropertyValidator<T>
|
||||
extends BaseComparablePropertyValidator<T, Long, LongPropertyValidator<T>> {
|
||||
|
@@ -29,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>> {
|
||||
|
||||
@@ -173,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);
|
||||
|
@@ -23,7 +23,7 @@ import java.util.function.Function;
|
||||
*
|
||||
* @param <T> 待校验对象的类型
|
||||
* @param <TProperty> 待校验属性的类型
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class ObjectPropertyValidator<T, TProperty>
|
||||
extends BasePropertyValidator<T, TProperty, ObjectPropertyValidator<T, TProperty>> {
|
||||
|
@@ -27,7 +27,7 @@ import java.util.function.Supplier;
|
||||
* @param <T> 被验证对象类型
|
||||
* @param <V1> 第一个元素的类型
|
||||
* @param <V2> 第二个元素的类型
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class PairPropertyValidator<T, V1, V2>
|
||||
extends BasePropertyValidator<T, Entry<V1, V2>, PairPropertyValidator<T, V1, V2>> {
|
||||
|
@@ -35,7 +35,7 @@ import xyz.zhouxy.plusone.commons.util.StringTools;
|
||||
* 用于构建校验 {@code String} 类型属性的规则链。
|
||||
*
|
||||
* @param <T> 待校验对象的类型
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class StringPropertyValidator<T>
|
||||
extends BaseComparablePropertyValidator<T, String, StringPropertyValidator<T>> {
|
||||
|
@@ -18,7 +18,7 @@ package xyz.zhouxy.plusone.validator;
|
||||
/**
|
||||
* 校验失败异常
|
||||
*
|
||||
* @author ZhouXY
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
*/
|
||||
public class ValidationException extends RuntimeException {
|
||||
|
||||
|
@@ -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 {
|
||||
|
@@ -122,14 +122,14 @@ 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'.");
|
||||
}
|
||||
|
15
pom.xml
15
pom.xml
@@ -6,10 +6,10 @@
|
||||
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-validator-parent</artifactId>
|
||||
<version>1.0.0-RC2</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>
|
||||
|
||||
@@ -23,6 +23,10 @@
|
||||
|
||||
<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-RC1</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