Compare commits
10 Commits
1.0.0-RC3
...
feat/jpeci
| Author | SHA1 | Date | |
|---|---|---|---|
| 08e228c5f7 | |||
| ba9ccebb56 | |||
| 821f43d154 | |||
| 4a604a64c4 | |||
| 9aa510820d | |||
| 98a7ed573b | |||
| f3c173818c | |||
| fdabd29347 | |||
| e22a95861d | |||
| edeaec85da |
9
NOTICE
Normal file
9
NOTICE
Normal file
@@ -0,0 +1,9 @@
|
||||
Plusone Validator
|
||||
Copyright 2022-present Zhou Xingyi
|
||||
|
||||
This product includes software developed by
|
||||
Zhou Xingyi (周兴毅) (https://gitea.zhouxy.xyz/plusone).
|
||||
|
||||
This product is licensed to you under the Apache License, Version 2.0
|
||||
(the "License"). You may not use this product except in compliance with
|
||||
the License.
|
||||
157
README.md
157
README.md
@@ -1,16 +1,41 @@
|
||||
# Plusone Validator
|
||||
|
||||
## 简介
|
||||
Plusone Validator 是一个校验库,使用 lambda 表达式(包括方法引用)和流式 API 构建校验规则,对对象进行校验。
|
||||
|
||||
Plusone Validator 是一个受 [FluentValidation](https://github.com/FluentValidation/FluentValidation) 启发的 Java 校验库,使用 Lambda 表达式(方法引用)和流式 API 构建声明式校验规则。
|
||||
|
||||
**特性:**
|
||||
- 链式调用
|
||||
- 支持 **POJO** 和 **Map** 两种校验模式
|
||||
- 每种校验规则支持四种错误信息提供方式(默认/自定义字符串/自定义异常/含属性值的自定义异常)
|
||||
- 基于 JDK 1.8
|
||||
|
||||
## 环境要求
|
||||
|
||||
- **JDK**:1.8 及以上
|
||||
- **编码**:UTF-8
|
||||
|
||||
## 安装
|
||||
Plusone Validator 暂未提交 maven 中央仓库,可 clone 代码仓库,通过 maven 安装到本地库,然后在项目中引入。
|
||||
|
||||
Plusone Validator 暂未发布至 Maven 中央仓库,需 clone 代码仓库后通过 `mvn install` 安装到本地仓库,然后在项目中引入:
|
||||
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-validator</artifactId>
|
||||
<version>${plusone-validator.version}</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
> 该项目依赖 [plusone-commons](https://gitea.zhouxy.xyz/plusone/plusone-commons),它本身依赖 Guava。
|
||||
|
||||
## 示例
|
||||
|
||||
### 校验对象
|
||||
以下示例基于一个包含姓名、邮箱、会员等级、客户编号、生日、地址等字段的 `Customer` 对象。
|
||||
|
||||
定义校验器:
|
||||
### 校验 POJO 对象
|
||||
|
||||
继承 `BaseValidator<T>`,在构造器中通过 `ruleFor` 添加规则链:
|
||||
```java
|
||||
class CustomerValidator extends BaseValidator<Customer> {
|
||||
|
||||
@@ -46,7 +71,11 @@ public void foo(Customer customer) {
|
||||
|
||||
### 校验 Map
|
||||
|
||||
定义校验器:
|
||||
继承 `MapValidator<K,V>`,在构造器中通过 `ruleForString`、`ruleForInt` 等方法按 key 添加规则链。
|
||||
|
||||
`validateAndCopy()` 会**先校验,然后仅保留白名单 key**(构造时传入的 `FIELD_NAMES`),多余字段会被自动剥离,可防止 Map 注入攻击。
|
||||
|
||||
> 注意泛型类型见证语法 `this.<LocalDate>ruleFor(...)` — 当 Map 的 value 类型是 `Object` 时,需显式指定实际类型。
|
||||
```java
|
||||
class CustomerMapValidator extends MapValidator<String, Object> {
|
||||
|
||||
@@ -86,10 +115,120 @@ public void foo(Map<String, Object> customer) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
## 快速参考
|
||||
|
||||
### ruleFor 方法一览
|
||||
|
||||
| 方法 | 属性类型 | 适用场景 |
|
||||
|------|---------|---------|
|
||||
| `ruleFor(getter)` | 任意类型 | 通用对象属性校验 |
|
||||
| `ruleForString(getter)` | `String` | 字符串属性校验 |
|
||||
| `ruleForInt(getter)` | `Integer` | 整数属性校验 |
|
||||
| `ruleForLong(getter)` | `Long` | 长整数属性校验 |
|
||||
| `ruleForDouble(getter)` | `Double` | 浮点数属性校验 |
|
||||
| `ruleForBool(getter)` | `Boolean` | 布尔属性校验 |
|
||||
| `ruleForComparable(getter)` | `Comparable` | 可比较类型属性校验 |
|
||||
| `ruleForCollection(getter)` | `Collection<E>` | 集合属性校验 |
|
||||
| `ruleForArray(getter)` | `E[]` | 数组属性校验 |
|
||||
| `ruleFor(g1, g2)` | 二元组 | 两个属性的联合校验 |
|
||||
|
||||
### 校验规则一览
|
||||
|
||||
| 规则 | Object | Comparable | 数值 | String | Bool | 集合/数组 | 说明 |
|
||||
|------|:---:|:---:|:---:|:---:|:---:|:---:|------|
|
||||
| `notNull()` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 不为 null |
|
||||
| `isNull()` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 必须为 null |
|
||||
| `equal(obj)` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 等于给定值 |
|
||||
| `notEqual(obj)` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 不等于给定值 |
|
||||
| `must(pred)` | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | 自定义条件 |
|
||||
| `gt/ge/lt/le` | — | — | ✅ | — | — | — | 大于/大于等于/小于/小于等于 |
|
||||
| `inRange(range)` | — | ✅ | ✅ | ✅ | — | — | 值在指定区间内 |
|
||||
| `notBlank()` | — | — | — | ✅ | — | — | 不为空白字符串 |
|
||||
| `notEmpty()` | — | — | — | ✅ | — | ✅ | 不为空(长度 > 0) |
|
||||
| `isEmpty()` | — | — | — | — | — | ✅ | 必须为空 |
|
||||
| `length(min, max)` | — | — | — | ✅ | — | ✅ | 长度/大小在范围内 |
|
||||
| `emailAddress()` | — | — | — | ✅ | — | — | 满足邮箱格式 |
|
||||
| `matches(pattern)` | — | — | — | ✅ | — | — | 匹配正则表达式 |
|
||||
| `matchesAny(patterns)` | — | — | — | ✅ | — | — | 匹配任一正则 |
|
||||
| `matchesAll(patterns)` | — | — | — | ✅ | — | — | 匹配所有正则 |
|
||||
| `allMatch(pred)` | — | — | — | — | — | ✅ | 所有元素满足条件 |
|
||||
| `isTrueValue()` | — | — | — | — | ✅ | — | 必须为 true |
|
||||
| `isFalseValue()` | — | — | — | — | ✅ | — | 必须为 false |
|
||||
|
||||
### 自定义错误信息
|
||||
|
||||
每个校验规则都支持四种错误信息提供方式:
|
||||
|
||||
```java
|
||||
// 1. 默认消息
|
||||
ruleFor(Person::getName).notNull();
|
||||
|
||||
// 2. 自定义字符串
|
||||
ruleFor(Person::getName).notNull("姓名不能为空");
|
||||
|
||||
// 3. 自定义异常(Supplier)
|
||||
ruleFor(Person::getName).notNull(() -> new BizException("姓名不能为空"));
|
||||
|
||||
// 4. 自定义异常 + 属性值注入(Function)
|
||||
ruleFor(Person::getAge).gt(0, age -> new BizException("年龄必须 > 0,当前值:%d", age));
|
||||
```
|
||||
|
||||
### Null 值处理
|
||||
|
||||
多数校验规则对 `null` **宽松处理**(视为通过),让用户通过 `notNull()` 显式控制 null 行为:
|
||||
|
||||
```java
|
||||
// ✅ null 视为通过 — 不会因 NPE 失败
|
||||
ruleFor(Person::getEmail).emailAddress();
|
||||
|
||||
// ✅ 显式拒绝 null
|
||||
ruleFor(Person::getEmail).notNull("邮箱不能为空").emailAddress();
|
||||
```
|
||||
|
||||
例外:`notBlank()` 和 `notEmpty()` 将 `null` 视为不通过(null 本身就是 blank/empty)。
|
||||
|
||||
### 嵌套对象校验
|
||||
|
||||
使用 `withRule(Consumer)` 在子类构造器中调用嵌套校验器:
|
||||
|
||||
```java
|
||||
class OrderValidator extends BaseValidator<Order> {
|
||||
private static final OrderValidator INSTANCE = new OrderValidator();
|
||||
|
||||
private OrderValidator() {
|
||||
ruleFor(Order::getOrderId).notBlank("订单号不能为空");
|
||||
|
||||
// 嵌套校验 Customer
|
||||
withRule(order -> {
|
||||
Customer customer = order.getCustomer();
|
||||
CustomerValidator.getInstance().validate(customer);
|
||||
});
|
||||
|
||||
// 需要自定义错误信息时,try-catch 重新包装
|
||||
withRule(order -> {
|
||||
try {
|
||||
AddressValidator.getInstance().validate(order.getAddress());
|
||||
} catch (ValidationException e) {
|
||||
throw ValidationException.withMessage("地址校验失败:%s", e.getMessage());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 对象整体校验
|
||||
|
||||
使用 `withRule(Predicate)` 对多个属性进行联合校验:
|
||||
|
||||
```java
|
||||
// 三个及以上字段的联合校验
|
||||
withRule(order -> order.getTotalAmount().compareTo(order.getPaidAmount()) >= 0,
|
||||
"订单金额不能小于已付金额");
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 其他
|
||||
## 关于本项目
|
||||
|
||||
Plusone Validator 是个人在学习 Microsoft 的 [eShop](https://github.com/dotnet/eShop) 时,被其中 [FluentValidation](https://github.com/FluentValidation/FluentValidation) 的 API 所吸引,出于学习和个人使用的目的进行开发和维护。使用 [Apache License 2.0](./LICENSE) 开源。
|
||||
|
||||
欢迎通过 issue 反馈使用过程中发现的问题和建议。
|
||||
Plusone Validator 受 .NET 的 [FluentValidation](https://github.com/FluentValidation/FluentValidation) API 启发,使用 [Apache License 2.0](./LICENSE) 开源。
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
<parent>
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-validator-parent</artifactId>
|
||||
<version>1.0.0-RC2</version>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>plusone-validator</artifactId>
|
||||
@@ -18,11 +18,37 @@
|
||||
Plusone Validator 是一个校验库,使用 lambda 表达式(包括方法引用)和流式 API 构建校验规则,对对象进行校验。
|
||||
</description>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>Apache License, Version 2.0</name>
|
||||
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
|
||||
<distribution>repo</distribution>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<name>Zhou Xingyi (周兴毅)</name>
|
||||
<url>https://gitea.zhouxy.xyz/plusone</url>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<scm>
|
||||
<connection>scm:git:https://gitea.zhouxy.xyz/plusone/plusone-validator.git</connection>
|
||||
<developerConnection>scm:git:ssh://gitea.zhouxy.xyz/plusone/plusone-validator.git</developerConnection>
|
||||
<url>https://gitea.zhouxy.xyz/plusone/plusone-validator</url>
|
||||
</scm>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-commons</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jspecify</groupId>
|
||||
<artifactId>jspecify</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2023-2025 the original author or authors.
|
||||
* Copyright 2023-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -151,11 +151,17 @@ public class ArrayPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足指定条件
|
||||
*
|
||||
* <p>
|
||||
* 当数组为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。
|
||||
*
|
||||
* @param condition 校验条件
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final ArrayPropertyValidator<T, E> allMatch(final Predicate<E> condition) {
|
||||
return withRule(c -> {
|
||||
if (ArrayTools.isEmpty(c)) {
|
||||
return;
|
||||
}
|
||||
for (E element : c) {
|
||||
if (!condition.test(element)) {
|
||||
throw ValidationException.withMessage("All elements must match the condition.");
|
||||
@@ -167,6 +173,9 @@ public class ArrayPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足指定条件
|
||||
*
|
||||
* <p>
|
||||
* 当数组为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。
|
||||
*
|
||||
* @param condition 校验条件
|
||||
* @param errorMessage 异常信息
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
@@ -174,6 +183,9 @@ public class ArrayPropertyValidator<T, E>
|
||||
public final ArrayPropertyValidator<T, E> allMatch(
|
||||
final Predicate<E> condition, final String errorMessage) {
|
||||
return withRule(c -> {
|
||||
if (ArrayTools.isEmpty(c)) {
|
||||
return;
|
||||
}
|
||||
for (E element : c) {
|
||||
if (!condition.test(element)) {
|
||||
throw ValidationException.withMessage(errorMessage);
|
||||
@@ -185,6 +197,9 @@ public class ArrayPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足指定条件
|
||||
*
|
||||
* <p>
|
||||
* 当数组为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param condition 校验条件
|
||||
* @param exceptionSupplier 自定义异常
|
||||
@@ -193,6 +208,9 @@ public class ArrayPropertyValidator<T, E>
|
||||
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> allMatch(
|
||||
final Predicate<E> condition, final Supplier<X> exceptionSupplier) {
|
||||
return withRule(c -> {
|
||||
if (ArrayTools.isEmpty(c)) {
|
||||
return;
|
||||
}
|
||||
for (E element : c) {
|
||||
if (!condition.test(element)) {
|
||||
throw exceptionSupplier.get();
|
||||
@@ -204,6 +222,9 @@ public class ArrayPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足指定条件
|
||||
*
|
||||
* <p>
|
||||
* 当数组为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param condition 校验条件
|
||||
* @param exceptionFunction 自定义异常
|
||||
@@ -212,6 +233,9 @@ public class ArrayPropertyValidator<T, E>
|
||||
public final <X extends RuntimeException> ArrayPropertyValidator<T, E> allMatch(
|
||||
final Predicate<E> condition, final Function<E, X> exceptionFunction) {
|
||||
return withRule(c -> {
|
||||
if (ArrayTools.isEmpty(c)) {
|
||||
return;
|
||||
}
|
||||
for (E element : c) {
|
||||
if (!condition.test(element)) {
|
||||
throw exceptionFunction.apply(element);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2024-2025 the original author or authors.
|
||||
* Copyright 2024-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,6 +24,8 @@ import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* 属性校验器。包含针对属性的校验规则。
|
||||
*
|
||||
@@ -135,9 +137,9 @@ public abstract class BasePropertyValidator<
|
||||
/**
|
||||
* 校验属性
|
||||
*
|
||||
* @param obj 属性所在的对象
|
||||
* @param obj 属性所在的对象,允许为 {@code null}
|
||||
*/
|
||||
public final void validate(T obj) {
|
||||
public final void validate(@Nullable T obj) {
|
||||
for (Consumer<? super TProperty> consumer : consumers) {
|
||||
consumer.accept(getter.apply(obj));
|
||||
}
|
||||
@@ -258,6 +260,9 @@ public abstract class BasePropertyValidator<
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
*
|
||||
* <p>
|
||||
* 当值为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。
|
||||
*
|
||||
* @param obj 用于比较的对象
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
@@ -269,6 +274,9 @@ public abstract class BasePropertyValidator<
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
*
|
||||
* <p>
|
||||
* 当值为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。
|
||||
*
|
||||
* @param obj 用于比较的对象
|
||||
* @param errorMessage 异常信息
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
@@ -281,6 +289,9 @@ public abstract class BasePropertyValidator<
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
*
|
||||
* <p>
|
||||
* 当值为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param obj 用于比较的对象
|
||||
* @param exceptionSupplier 自定义异常
|
||||
@@ -294,6 +305,9 @@ public abstract class BasePropertyValidator<
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
*
|
||||
* <p>
|
||||
* 当值为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param obj 用于比较的对象
|
||||
* @param exceptionFunction 自定义异常
|
||||
@@ -313,7 +327,10 @@ public abstract class BasePropertyValidator<
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
* 添加一条校验属性的规则,校验属性是否不等于给定值
|
||||
*
|
||||
* <p>
|
||||
* 当值为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。
|
||||
*
|
||||
* @param obj 用于比较的对象
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
@@ -324,7 +341,10 @@ public abstract class BasePropertyValidator<
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
* 添加一条校验属性的规则,校验属性是否不等于给定值
|
||||
*
|
||||
* <p>
|
||||
* 当值为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。
|
||||
*
|
||||
* @param obj 用于比较的对象
|
||||
* @param errorMessage 异常信息
|
||||
@@ -335,7 +355,10 @@ public abstract class BasePropertyValidator<
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
* 添加一条校验属性的规则,校验属性是否不等于给定值
|
||||
*
|
||||
* <p>
|
||||
* 当值为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param obj 用于比较的对象
|
||||
@@ -348,7 +371,10 @@ public abstract class BasePropertyValidator<
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验属性是否等于给定值
|
||||
* 添加一条校验属性的规则,校验属性是否不等于给定值
|
||||
*
|
||||
* <p>
|
||||
* 当值为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param obj 用于比较的对象
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2022-2025 the original author or authors.
|
||||
* Copyright 2022-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -28,6 +28,8 @@ import java.util.function.Supplier;
|
||||
|
||||
import xyz.zhouxy.plusone.validator.function.*;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* 校验器基类
|
||||
* <p>
|
||||
@@ -311,7 +313,7 @@ public abstract class BaseValidator<T> implements IValidator<T> {
|
||||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void validate(T obj) {
|
||||
public void validate(@Nullable T obj) {
|
||||
this.rules.forEach(rule -> rule.accept(obj));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2023-2025 the original author or authors.
|
||||
* Copyright 2023-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2023-2025 the original author or authors.
|
||||
* Copyright 2023-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -151,36 +151,56 @@ public class CollectionPropertyValidator<T, E>
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足指定条件
|
||||
*
|
||||
* <p>
|
||||
* 当集合为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。
|
||||
*
|
||||
* @param condition 校验条件
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final CollectionPropertyValidator<T, E> allMatch(
|
||||
final Predicate<E> condition) {
|
||||
return withRule(c -> c.forEach(element -> {
|
||||
if (!condition.test(element)) {
|
||||
throw ValidationException.withMessage("All elements must match the condition.");
|
||||
return withRule(c -> {
|
||||
if (CollectionTools.isEmpty(c)) {
|
||||
return;
|
||||
}
|
||||
}));
|
||||
c.forEach(element -> {
|
||||
if (!condition.test(element)) {
|
||||
throw ValidationException.withMessage("All elements must match the condition.");
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足指定条件
|
||||
*
|
||||
* <p>
|
||||
* 当集合为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。
|
||||
*
|
||||
* @param condition 校验条件
|
||||
* @param errorMessage 异常信息
|
||||
* @return 当前校验器实例,用于链式调用
|
||||
*/
|
||||
public final CollectionPropertyValidator<T, E> allMatch(
|
||||
final Predicate<E> condition, final String errorMessage) {
|
||||
return withRule(c -> c.forEach(element -> {
|
||||
if (!condition.test(element)) {
|
||||
throw ValidationException.withMessage(errorMessage);
|
||||
return withRule(c -> {
|
||||
if (CollectionTools.isEmpty(c)) {
|
||||
return;
|
||||
}
|
||||
}));
|
||||
c.forEach(element -> {
|
||||
if (!condition.test(element)) {
|
||||
throw ValidationException.withMessage(errorMessage);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足指定条件
|
||||
*
|
||||
* <p>
|
||||
* 当集合为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param condition 校验条件
|
||||
* @param exceptionSupplier 自定义异常
|
||||
@@ -188,16 +208,24 @@ public class CollectionPropertyValidator<T, E>
|
||||
*/
|
||||
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> allMatch(
|
||||
final Predicate<E> condition, final Supplier<X> exceptionSupplier) {
|
||||
return withRule(c -> c.forEach(element -> {
|
||||
if (!condition.test(element)) {
|
||||
throw exceptionSupplier.get();
|
||||
return withRule(c -> {
|
||||
if (CollectionTools.isEmpty(c)) {
|
||||
return;
|
||||
}
|
||||
}));
|
||||
c.forEach(element -> {
|
||||
if (!condition.test(element)) {
|
||||
throw exceptionSupplier.get();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加一条校验属性的规则,校验是否所有元素都满足指定条件
|
||||
*
|
||||
* <p>
|
||||
* 当集合为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。
|
||||
*
|
||||
* @param <X> 自定义异常类型
|
||||
* @param condition 校验条件
|
||||
* @param exceptionFunction 自定义异常
|
||||
@@ -205,11 +233,16 @@ public class CollectionPropertyValidator<T, E>
|
||||
*/
|
||||
public final <X extends RuntimeException> CollectionPropertyValidator<T, E> allMatch(
|
||||
final Predicate<E> condition, final Function<E, X> exceptionFunction) {
|
||||
return withRule(c -> c.forEach(element -> {
|
||||
if (!condition.test(element)) {
|
||||
throw exceptionFunction.apply(element);
|
||||
return withRule(c -> {
|
||||
if (CollectionTools.isEmpty(c)) {
|
||||
return;
|
||||
}
|
||||
}));
|
||||
c.forEach(element -> {
|
||||
if (!condition.test(element)) {
|
||||
throw exceptionFunction.apply(element);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ================================
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2023-2025 the original author or authors.
|
||||
* Copyright 2023-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -48,7 +48,7 @@ public class DoublePropertyValidator<T>
|
||||
*/
|
||||
public final DoublePropertyValidator<T> gt(final double min) {
|
||||
return withRule(Conditions.greaterThan(min),
|
||||
"The input must be greater than '%s'.", min);
|
||||
"The input must be greater than '%f'.", min);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,7 +105,7 @@ public class DoublePropertyValidator<T>
|
||||
*/
|
||||
public final DoublePropertyValidator<T> ge(final double min) {
|
||||
return withRule(Conditions.greaterThanOrEqualTo(min),
|
||||
"The input must be greater than or equal to '%s'.", min);
|
||||
"The input must be greater than or equal to '%f'.", min);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -162,7 +162,7 @@ public class DoublePropertyValidator<T>
|
||||
*/
|
||||
public final DoublePropertyValidator<T> lt(final double max) {
|
||||
return withRule(Conditions.lessThan(max),
|
||||
"The input must be less than '%s'.", max);
|
||||
"The input must be less than '%f'.", max);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -219,7 +219,7 @@ public class DoublePropertyValidator<T>
|
||||
*/
|
||||
public final DoublePropertyValidator<T> le(final double max) {
|
||||
return withRule(Conditions.lessThanOrEqualTo(max),
|
||||
"The input must be less than or equal to '%s'.", max);
|
||||
"The input must be less than or equal to '%f'.", max);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2023-2025 the original author or authors.
|
||||
* Copyright 2023-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package xyz.zhouxy.plusone.validator;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* 校验器
|
||||
*
|
||||
@@ -30,7 +32,7 @@ public interface IValidator<T> {
|
||||
/**
|
||||
* 校验指定对象是否符合预定义规则
|
||||
*
|
||||
* @param obj 待校验的对象实例
|
||||
* @param obj 待校验的对象实例,允许为 {@code null}
|
||||
*/
|
||||
void validate(T obj);
|
||||
void validate(@Nullable T obj);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2023-2025 the original author or authors.
|
||||
* Copyright 2023-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2023-2025 the original author or authors.
|
||||
* Copyright 2023-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2024-2025 the original author or authors.
|
||||
* Copyright 2024-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -23,6 +23,8 @@ import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* 对 Map 进行校验的校验器
|
||||
*
|
||||
@@ -59,10 +61,10 @@ public abstract class MapValidator<K, V> extends BaseValidator<Map<K, V>> {
|
||||
/**
|
||||
* 校验并拷贝,仅保留指定 key 的属性。
|
||||
*
|
||||
* @param obj 待校验的 map
|
||||
* @param obj 待校验的 map,允许为 {@code null}
|
||||
* @return 拷贝后的 map
|
||||
*/
|
||||
public final Map<K, V> validateAndCopy(Map<K, V> obj) {
|
||||
public final Map<K, V> validateAndCopy(@Nullable Map<K, V> obj) {
|
||||
return validateAndCopyInternal(obj, this.keys);
|
||||
}
|
||||
|
||||
@@ -89,11 +91,11 @@ public abstract class MapValidator<K, V> extends BaseValidator<Map<K, V>> {
|
||||
return validateAndCopyInternal(obj, Arrays.asList(keys));
|
||||
}
|
||||
|
||||
private final Map<K, V> validateAndCopyInternal(Map<K, V> obj, Collection<K> keys) {
|
||||
private final Map<K, V> validateAndCopyInternal(@Nullable Map<K, V> obj, Collection<K> keys) {
|
||||
validate(obj);
|
||||
return obj.entrySet().stream()
|
||||
.filter(kv -> keys.contains(kv.getKey()))
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v2));
|
||||
}
|
||||
|
||||
// ================================
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2023-2025 the original author or authors.
|
||||
* Copyright 2023-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -28,7 +28,7 @@ import java.util.function.Function;
|
||||
public class ObjectPropertyValidator<T, TProperty>
|
||||
extends BasePropertyValidator<T, TProperty, ObjectPropertyValidator<T, TProperty>> {
|
||||
|
||||
ObjectPropertyValidator(Function<T, TProperty> getter) {
|
||||
ObjectPropertyValidator(Function<T, ? extends TProperty> getter) {
|
||||
super(getter);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2023-2025 the original author or authors.
|
||||
* Copyright 2023-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -547,7 +547,7 @@ public class StringPropertyValidator<T>
|
||||
|
||||
private static Predicate<String> length(int length) {
|
||||
AssertTools.checkArgument(length >= 0,
|
||||
"The expected length must be non-negative.");
|
||||
"The expected length must be non-negative.");
|
||||
return input -> input == null || input.length() == length;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package xyz.zhouxy.plusone.validator;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* 校验失败异常
|
||||
*
|
||||
@@ -30,11 +32,11 @@ public class ValidationException extends RuntimeException {
|
||||
super(message);
|
||||
}
|
||||
|
||||
private ValidationException(Throwable cause) {
|
||||
private ValidationException(@Nullable Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
private ValidationException(String message, Throwable cause) {
|
||||
private ValidationException(String message, @Nullable Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
@@ -71,10 +73,10 @@ public class ValidationException extends RuntimeException {
|
||||
/**
|
||||
* 创建 {@code ValidationException} 实例
|
||||
*
|
||||
* @param cause 导致校验失败的根本异常
|
||||
* @param cause 导致校验失败的根本异常,允许为 {@code null}
|
||||
* @return {@code ValidationException} 实例
|
||||
*/
|
||||
public static ValidationException withCause(Throwable cause) {
|
||||
public static ValidationException withCause(@Nullable Throwable cause) {
|
||||
return new ValidationException(cause);
|
||||
}
|
||||
|
||||
@@ -82,10 +84,10 @@ public class ValidationException extends RuntimeException {
|
||||
* 创建 {@code ValidationException} 实例
|
||||
*
|
||||
* @param message 异常信息
|
||||
* @param cause 导致校验失败的根本异常
|
||||
* @param cause 导致校验失败的根本异常,允许为 {@code null}
|
||||
* @return {@code ValidationException} 实例
|
||||
*/
|
||||
public static ValidationException withMessageAndCause(String message, Throwable cause) {
|
||||
public static ValidationException withMessageAndCause(String message, @Nullable Throwable cause) {
|
||||
return new ValidationException(message, cause);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* 函数式接口包,提供带序列化能力的特殊 {@code Function} 子类型。
|
||||
*
|
||||
* <p>
|
||||
* 本包中的接口均继承自 {@link java.util.function.Function},同时实现
|
||||
* {@link java.io.Serializable},使 lambda 表达式或方法引用可以被序列化。
|
||||
* 主要用作 {@link xyz.zhouxy.plusone.validator.BaseValidator} 中
|
||||
* {@code ruleFor} 方法族的参数类型,以支持特定类型的属性取值。
|
||||
*
|
||||
* <ul>
|
||||
* <li>{@link xyz.zhouxy.plusone.validator.function.ToStringFunction ToStringFunction} — Function<T, String></li>
|
||||
* <li>{@link xyz.zhouxy.plusone.validator.function.ToIntegerFunction ToIntegerFunction} — Function<T, Integer></li>
|
||||
* <li>{@link xyz.zhouxy.plusone.validator.function.ToLongObjectFunction ToLongObjectFunction} — Function<T, Long></li>
|
||||
* <li>{@link xyz.zhouxy.plusone.validator.function.ToDoubleObjectFunction ToDoubleObjectFunction} — Function<T, Double></li>
|
||||
* <li>{@link xyz.zhouxy.plusone.validator.function.ToBoolObjectFunction ToBoolObjectFunction} — Function<T, Boolean></li>
|
||||
* </ul>
|
||||
*/
|
||||
@NullMarked
|
||||
package xyz.zhouxy.plusone.validator.function;
|
||||
|
||||
import org.jspecify.annotations.NullMarked;
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Plusone Validator 核心包,提供流式校验 API。
|
||||
*
|
||||
* <p>
|
||||
* 使用 lambda 表达式(包括方法引用)和流式 API 构建校验规则,对对象进行校验。
|
||||
* 支持校验普通对象(POJO)和 {@code Map} 对象。
|
||||
*
|
||||
* <h3>快速开始</h3>
|
||||
* <pre>{@code
|
||||
* public class PersonValidator extends BaseValidator<Person> {
|
||||
* public PersonValidator() {
|
||||
* ruleFor(Person::getName)
|
||||
* .notNull()
|
||||
* .notBlank();
|
||||
* ruleForInt(Person::getAge)
|
||||
* .gt(0);
|
||||
* }
|
||||
* }
|
||||
* }</pre>
|
||||
*
|
||||
* <h3>核心概念</h3>
|
||||
* <ul>
|
||||
* <li>{@link xyz.zhouxy.plusone.validator.IValidator IValidator} — 校验器接口</li>
|
||||
* <li>{@link xyz.zhouxy.plusone.validator.BaseValidator BaseValidator} — 校验器基类,通过继承并添加规则构建校验器</li>
|
||||
* <li>{@link xyz.zhouxy.plusone.validator.BasePropertyValidator BasePropertyValidator} — 属性校验器基类</li>
|
||||
* <li>{@link xyz.zhouxy.plusone.validator.MapValidator MapValidator} — Map 校验器</li>
|
||||
* <li>{@link xyz.zhouxy.plusone.validator.ValidationException ValidationException} — 校验失败异常</li>
|
||||
* </ul>
|
||||
*
|
||||
* @see xyz.zhouxy.plusone.validator.BaseValidator
|
||||
* @see xyz.zhouxy.plusone.validator.MapValidator
|
||||
*/
|
||||
@NullMarked
|
||||
package xyz.zhouxy.plusone.validator;
|
||||
|
||||
import org.jspecify.annotations.NullMarked;
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -293,8 +293,18 @@ public class ArrayPropertyValidatorTests {
|
||||
}
|
||||
};
|
||||
|
||||
ExampleCommand command = exampleCommandWithStringArrayProperty(new String[] { "1234", "12345", "123456" });
|
||||
assertDoesNotThrow(() -> validator.validate(command));
|
||||
{
|
||||
ExampleCommand command = exampleCommandWithStringArrayProperty(new String[] { "1234", "12345", "123456" });
|
||||
assertDoesNotThrow(() -> validator.validate(command));
|
||||
}
|
||||
{
|
||||
ExampleCommand command = exampleCommandWithStringArrayProperty(new String[0]);
|
||||
assertDoesNotThrow(() -> validator.validate(command));
|
||||
}
|
||||
{
|
||||
ExampleCommand command = exampleCommandWithStringArrayProperty(null);
|
||||
assertDoesNotThrow(() -> validator.validate(command));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2023-2025 the original author or authors.
|
||||
* Copyright 2023-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -298,8 +298,14 @@ public class CollectionPropertyValidatorTests {
|
||||
}
|
||||
};
|
||||
|
||||
ExampleCommand command = exampleCommandWithStringListProperty(Lists.newArrayList("1234", "12345", "123456"));
|
||||
assertDoesNotThrow(() -> validator.validate(command));
|
||||
{
|
||||
ExampleCommand command = exampleCommandWithStringListProperty(Lists.newArrayList("1234", "12345", "123456"));
|
||||
assertDoesNotThrow(() -> validator.validate(command));
|
||||
}
|
||||
{
|
||||
ExampleCommand command = exampleCommandWithStringListProperty(null);
|
||||
assertDoesNotThrow(() -> validator.validate(command));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -87,7 +87,7 @@ public class DoublePropertyValidatorTests {
|
||||
|
||||
ValidationException e = assertThrows(
|
||||
ValidationException.class, () -> validator.validate(command));
|
||||
assertEquals(String.format("The input must be greater than '%s'.", MIN), e.getMessage());
|
||||
assertEquals(String.format("The input must be greater than '%f'.", MIN), e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@@ -196,7 +196,7 @@ public class DoublePropertyValidatorTests {
|
||||
|
||||
ValidationException e = assertThrows(
|
||||
ValidationException.class, () -> validator.validate(command));
|
||||
assertEquals(String.format("The input must be greater than or equal to '%s'.", MIN), e.getMessage());
|
||||
assertEquals(String.format("The input must be greater than or equal to '%f'.", MIN), e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@@ -305,7 +305,7 @@ public class DoublePropertyValidatorTests {
|
||||
|
||||
ValidationException e = assertThrows(
|
||||
ValidationException.class, () -> validator.validate(command));
|
||||
assertEquals(String.format("The input must be less than '%s'.", MAX), e.getMessage());
|
||||
assertEquals(String.format("The input must be less than '%f'.", MAX), e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@@ -414,7 +414,7 @@ public class DoublePropertyValidatorTests {
|
||||
|
||||
ValidationException e = assertThrows(
|
||||
ValidationException.class, () -> validator.validate(command));
|
||||
assertEquals(String.format("The input must be less than or equal to '%s'.", MAX), e.getMessage());
|
||||
assertEquals(String.format("The input must be less than or equal to '%f'.", MAX), e.getMessage());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2025 the original author or authors.
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2024-2025 the original author or authors.
|
||||
* Copyright 2024-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,3 +1,19 @@
|
||||
/*
|
||||
* Copyright 2025-present ZhouXY
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package xyz.zhouxy.plusone.validator;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
27
pom.xml
27
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-SNAPSHOT</version>
|
||||
|
||||
<name>plusone-validator-parent</name>
|
||||
<url>http://gitea.zhouxy.xyz/plusone/plusone-validator</url>
|
||||
<url>https://gitea.zhouxy.xyz/plusone/plusone-validator</url>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
||||
@@ -21,12 +21,33 @@
|
||||
Plusone Validator 是一个校验库,使用 lambda 表达式(包括方法引用)和流式 API 构建校验规则,对对象进行校验。
|
||||
</description>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>Apache License, Version 2.0</name>
|
||||
<url>https://www.apache.org/licenses/LICENSE-2.0</url>
|
||||
<distribution>repo</distribution>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<name>Zhou Xingyi (周兴毅)</name>
|
||||
<url>https://gitea.zhouxy.xyz/plusone</url>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<scm>
|
||||
<connection>scm:git:https://gitea.zhouxy.xyz/plusone/plusone-validator.git</connection>
|
||||
<developerConnection>scm:git:ssh://gitea.zhouxy.xyz/plusone/plusone-validator.git</developerConnection>
|
||||
<url>https://gitea.zhouxy.xyz/plusone/plusone-validator</url>
|
||||
</scm>
|
||||
|
||||
<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>
|
||||
<plusone-commons.version>1.1.0-SNAPSHOT</plusone-commons.version>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
|
||||
Reference in New Issue
Block a user