From e38be765a7cf43efd86b4f220298337e0106309c Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 20 May 2025 18:00:19 +0800 Subject: [PATCH] =?UTF-8?q?refactor!:=20=E4=BF=AE=E6=94=B9=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E6=A0=A1=E9=AA=8C=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `StringPropertyValidator` 内置的校验规则中,除了 `notEmpty` 和 `notBlank` 之外,属性值为 null 时不做校验。 --- .../plusone/validator/StringPropertyValidator.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/StringPropertyValidator.java b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/StringPropertyValidator.java index 070ca09..6867dec 100644 --- a/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/StringPropertyValidator.java +++ b/plusone-validator/src/main/java/xyz/zhouxy/plusone/validator/StringPropertyValidator.java @@ -59,7 +59,7 @@ public class StringPropertyValidator extends BaseComparablePropertyValidato public StringPropertyValidator matches( Pattern regex, Function exceptionCreator) { - withRule(input -> RegexTools.matches(input, regex), exceptionCreator); + withRule(input -> (input == null || RegexTools.matches(input, regex)), exceptionCreator); return this; } @@ -101,7 +101,7 @@ public class StringPropertyValidator extends BaseComparablePropertyValidato public StringPropertyValidator matchesOne( List regexs, Function exceptionCreator) { - withRule(input -> RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); + withRule(input -> input == null || RegexTools.matchesOne(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); return this; } @@ -143,7 +143,7 @@ public class StringPropertyValidator extends BaseComparablePropertyValidato public StringPropertyValidator matchesAll( Collection regexs, Function exceptionCreator) { - withRule(input -> RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); + withRule(input -> input == null || RegexTools.matchesAll(input, regexs.toArray(new Pattern[regexs.size()])), exceptionCreator); return this; } @@ -246,13 +246,13 @@ public class StringPropertyValidator extends BaseComparablePropertyValidato Function exceptionCreator) { AssertTools.checkArgument(length >= 0, "The required length must be greater than or equal to 0."); - withRule(s -> s != null && s.length() == length, exceptionCreator); + withRule(s -> s == null || s.length() == length, exceptionCreator); return this; } static boolean length(String str, int min, int max) { if (str == null) { - return false; + return true; } final int len = str.length(); return len >= min && len <= max;