From fdabd29347db2221ad3ef0059b07d50356d52190 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 27 May 2026 11:15:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E5=92=8C=E9=9B=86=E5=90=88=E7=9A=84=20`allMatch`=20=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E6=96=B9=E6=B3=95=E5=AF=B9=E7=A9=BA=E5=80=BC=E7=9A=84?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 `ArrayPropertyValidator` 的 `allMatch` 方法中添加空值检查,当数组为空或 `null` 时直接返回 - 在 `CollectionPropertyValidator` 的 `allMatch` 方法中添加空值检查,当集合为空或 `null` 时直接返回 - 更新相关方法的文档注释,说明 `null` 值被视为通过校验 - 添加针对空数组、空集合和 `null` 值的测试用例 - 确保只有非空情况才会执行元素匹配校验逻辑 --- .../validator/ArrayPropertyValidator.java | 24 +++++++ .../CollectionPropertyValidator.java | 65 ++++++++++++++----- .../ArrayPropertyValidatorTests.java | 14 +++- .../CollectionPropertyValidatorTests.java | 10 ++- 4 files changed, 93 insertions(+), 20 deletions(-) diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ArrayPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ArrayPropertyValidator.java index b4db28b..88133f4 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ArrayPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/ArrayPropertyValidator.java @@ -151,11 +151,17 @@ public class ArrayPropertyValidator /** * 添加一条校验属性的规则,校验是否所有元素都满足指定条件 * + *

+ * 当数组为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。 + * * @param condition 校验条件 * @return 当前校验器实例,用于链式调用 */ public final ArrayPropertyValidator allMatch(final Predicate 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 /** * 添加一条校验属性的规则,校验是否所有元素都满足指定条件 * + *

+ * 当数组为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。 + * * @param condition 校验条件 * @param errorMessage 异常信息 * @return 当前校验器实例,用于链式调用 @@ -174,6 +183,9 @@ public class ArrayPropertyValidator public final ArrayPropertyValidator allMatch( final Predicate 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 /** * 添加一条校验属性的规则,校验是否所有元素都满足指定条件 * + *

+ * 当数组为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。 + * * @param 自定义异常类型 * @param condition 校验条件 * @param exceptionSupplier 自定义异常 @@ -193,6 +208,9 @@ public class ArrayPropertyValidator public final ArrayPropertyValidator allMatch( final Predicate condition, final Supplier 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 /** * 添加一条校验属性的规则,校验是否所有元素都满足指定条件 * + *

+ * 当数组为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。 + * * @param 自定义异常类型 * @param condition 校验条件 * @param exceptionFunction 自定义异常 @@ -212,6 +233,9 @@ public class ArrayPropertyValidator public final ArrayPropertyValidator allMatch( final Predicate condition, final Function exceptionFunction) { return withRule(c -> { + if (ArrayTools.isEmpty(c)) { + return; + } for (E element : c) { if (!condition.test(element)) { throw exceptionFunction.apply(element); diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/CollectionPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/CollectionPropertyValidator.java index 5bb8976..d357218 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/CollectionPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/CollectionPropertyValidator.java @@ -151,36 +151,56 @@ public class CollectionPropertyValidator /** * 添加一条校验属性的规则,校验是否所有元素都满足指定条件 * + *

+ * 当集合为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。 + * * @param condition 校验条件 * @return 当前校验器实例,用于链式调用 */ public final CollectionPropertyValidator allMatch( final Predicate 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."); + } + }); + }); } /** * 添加一条校验属性的规则,校验是否所有元素都满足指定条件 + * + *

+ * 当集合为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。 + * * @param condition 校验条件 * @param errorMessage 异常信息 * @return 当前校验器实例,用于链式调用 */ public final CollectionPropertyValidator allMatch( final Predicate 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); + } + }); + }); } /** * 添加一条校验属性的规则,校验是否所有元素都满足指定条件 * + *

+ * 当集合为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。 + * * @param 自定义异常类型 * @param condition 校验条件 * @param exceptionSupplier 自定义异常 @@ -188,16 +208,24 @@ public class CollectionPropertyValidator */ public final CollectionPropertyValidator allMatch( final Predicate condition, final Supplier 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(); + } + }); + }); } /** * 添加一条校验属性的规则,校验是否所有元素都满足指定条件 * + *

+ * 当集合为 {@code null} 时,视为通过。如果需要校验值不为 {@code null},请使用 {@link #notNull()} 方法。 + * * @param 自定义异常类型 * @param condition 校验条件 * @param exceptionFunction 自定义异常 @@ -205,11 +233,16 @@ public class CollectionPropertyValidator */ public final CollectionPropertyValidator allMatch( final Predicate condition, final Function 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); + } + }); + }); } // ================================ diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ArrayPropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ArrayPropertyValidatorTests.java index 99840fe..83ea01c 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ArrayPropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/ArrayPropertyValidatorTests.java @@ -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 diff --git a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/CollectionPropertyValidatorTests.java b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/CollectionPropertyValidatorTests.java index 01b43e6..a5d0e5f 100644 --- a/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/CollectionPropertyValidatorTests.java +++ b/plusone-validator/src/test/java/xyz/zhouxy/plusone/example/validator/CollectionPropertyValidatorTests.java @@ -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