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