Skip to content

Commit 981315f

Browse files
authored
move null check into AbstractConstraintAdapter (#272)
1 parent c1d2224 commit 981315f

File tree

11 files changed

+21
-54
lines changed

11 files changed

+21
-54
lines changed

validator/src/main/java/io/avaje/validation/adapter/AbstractConstraintAdapter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
import java.util.Set;
77

8+
import org.jspecify.annotations.NonNull;
9+
810
/** Abstract Adapter that validates objects based on Constraint Annotations. */
911
public abstract class AbstractConstraintAdapter<T> implements ValidationAdapter<T> {
1012

@@ -23,11 +25,11 @@ protected AbstractConstraintAdapter(AdapterCreateRequest request) {
2325
* @param value the object to validate
2426
* @return false if a violation error should be added
2527
*/
26-
protected abstract boolean isValid(T value);
28+
protected abstract boolean isValid(@NonNull T value);
2729

2830
@Override
2931
public final boolean validate(T value, ValidationRequest req, String propertyName) {
30-
if (!checkGroups(groups, req)) {
32+
if (value == null || !checkGroups(groups, req)) {
3133
return true;
3234
}
3335
if (!isValid(value)) {

validator/src/main/java/io/avaje/validation/core/adapters/BasicAdapters.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ static sealed class PatternAdapter extends AbstractConstraintAdapter<CharSequenc
6363

6464
@Override
6565
public boolean isValid(CharSequence value) {
66-
return value == null || !pattern.test(value.toString());
66+
return !pattern.test(value.toString());
6767
}
6868
}
6969

@@ -239,7 +239,7 @@ private static final class AssertBooleanAdapter extends PrimitiveAdapter<Boolean
239239

240240
@Override
241241
public boolean isValid(Boolean value) {
242-
return value == null || assertBool == value;
242+
return assertBool == value;
243243
}
244244

245245
@Override

validator/src/main/java/io/avaje/validation/core/adapters/DateRangeAdapter.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ private TemporalAmount nowTolerance(boolean negateTolerance) {
5252

5353
@Override
5454
protected boolean isValid(Object value) {
55-
if (value == null) {
56-
return true;
57-
}
5855
return switch (_type) {
5956
case "Temporal.Instant" -> compare((Instant) value);
6057
case "Temporal.LocalDate" -> compare((LocalDate) value);

validator/src/main/java/io/avaje/validation/core/adapters/EmailAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ final class EmailAdapter extends PatternAdapter {
4242

4343
@Override
4444
public boolean isValid(CharSequence value) {
45-
if (value == null || value.length() == 0) {
45+
if (value.length() == 0) {
4646
return true;
4747
}
4848

validator/src/main/java/io/avaje/validation/core/adapters/FuturePastAdapter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ final class FuturePastAdapter extends AbstractConstraintAdapter<Object> {
2828

2929
@Override
3030
public boolean isValid(Object obj) {
31-
if (obj == null) {
32-
return true;
33-
}
31+
3432
return switch (_type) {
3533
case "Temporal.Date" -> compare(((Date) obj).getTime(), Clock::millis);
3634
case "Temporal.Instant" -> compare((Instant) obj, Instant::now);

validator/src/main/java/io/avaje/validation/core/adapters/NumberAdapters.java

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ private static final class DecimalMaxAdapter extends AbstractConstraintAdapter<O
7676

7777
@Override
7878
public boolean isValid(Object number) {
79-
// null values are valid
80-
if (number == null) {
81-
return true;
82-
}
8379
final int comparisonResult =
8480
NumberComparatorHelper.compareDecimal(targetType, number, value, LESS_THAN);
8581
return !(inclusive ? comparisonResult > 0 : comparisonResult >= 0);
@@ -102,10 +98,6 @@ private static final class DecimalMinAdapter extends AbstractConstraintAdapter<O
10298

10399
@Override
104100
public boolean isValid(Object number) {
105-
// null values are valid
106-
if (number == null) {
107-
return true;
108-
}
109101
final int comparisonResult =
110102
NumberComparatorHelper.compareDecimal(targetType, number, value, LESS_THAN);
111103
return !(inclusive ? comparisonResult < 0 : comparisonResult <= 0);
@@ -130,10 +122,6 @@ private static final class MaxAdapter extends PrimitiveAdapter<Number>
130122

131123
@Override
132124
public boolean isValid(Number number) {
133-
// null values are valid
134-
if (number == null) {
135-
return true;
136-
}
137125
return switch (targetType) {
138126
case "Integer", "Long", "Short", "Byte" -> number.longValue() <= max;
139127
case "Double", "Number" -> compareDouble(number.doubleValue(), max, GREATER_THAN) <= 0;
@@ -185,7 +173,7 @@ static final class MaxBigDecimal extends AbstractConstraintAdapter<BigDecimal>
185173

186174
@Override
187175
public boolean isValid(BigDecimal number) {
188-
return number == null || number.compareTo(max) <= 0;
176+
return number.compareTo(max) <= 0;
189177
}
190178
}
191179

@@ -201,7 +189,7 @@ static final class MaxBigInteger extends AbstractConstraintAdapter<BigInteger>
201189

202190
@Override
203191
public boolean isValid(BigInteger number) {
204-
return number == null || number.compareTo(max) <= 0;
192+
return number.compareTo(max) <= 0;
205193
}
206194
}
207195

@@ -219,9 +207,6 @@ private static final class MinAdapter extends PrimitiveAdapter<Number>
219207

220208
@Override
221209
public boolean isValid(Number number) {
222-
if (number == null) {
223-
return true;
224-
}
225210
return switch (targetType) {
226211
case "Integer", "Long", "Short", "Byte" -> number.longValue() >= min;
227212
case "Double" -> compareDouble(number.doubleValue(), min, LESS_THAN) >= 0;
@@ -273,7 +258,7 @@ static final class MinBigDecimal extends AbstractConstraintAdapter<BigDecimal>
273258

274259
@Override
275260
public boolean isValid(BigDecimal number) {
276-
return number == null || number.compareTo(min) >= 0;
261+
return number.compareTo(min) >= 0;
277262
}
278263
}
279264

@@ -289,7 +274,7 @@ static final class MinBigInteger extends AbstractConstraintAdapter<BigInteger>
289274

290275
@Override
291276
public boolean isValid(BigInteger number) {
292-
return number == null || number.compareTo(min) >= 0;
277+
return number.compareTo(min) >= 0;
293278
}
294279
}
295280

@@ -306,10 +291,6 @@ private static final class DigitsAdapter extends AbstractConstraintAdapter<Objec
306291

307292
@Override
308293
public boolean isValid(Object value) {
309-
// null values are valid
310-
if (value == null) {
311-
return true;
312-
}
313294

314295
BigDecimal bigNum;
315296
if (value instanceof final BigDecimal bd) {
@@ -337,10 +318,6 @@ private static final class PositiveAdapter extends PrimitiveAdapter<Object> {
337318

338319
@Override
339320
public boolean isValid(Object value) {
340-
// null values are valid
341-
if (value == null) {
342-
return true;
343-
}
344321
final int sign = NumberSignHelper.signum(targetType, value, LESS_THAN);
345322
return !(inclusive ? sign < 0 : sign <= 0);
346323
}
@@ -389,10 +366,6 @@ private static final class NegativeAdapter extends PrimitiveAdapter<Object> {
389366

390367
@Override
391368
public boolean isValid(Object value) {
392-
// null values are valid
393-
if (value == null) {
394-
return true;
395-
}
396369
final int sign = NumberSignHelper.signum(targetType, value, GREATER_THAN);
397370
return !(inclusive ? sign > 0 : sign >= 0);
398371
}
@@ -446,9 +419,6 @@ private static final class RangeAdapter extends PrimitiveAdapter<Number> {
446419

447420
@Override
448421
public boolean isValid(Number value) {
449-
if (value == null) {
450-
return true;
451-
}
452422
return minAdapter.isValid(value) && maxAdapter.isValid(value);
453423
}
454424

@@ -496,9 +466,6 @@ private static final class RangeStringAdapter extends AbstractConstraintAdapter<
496466

497467
@Override
498468
public boolean isValid(Object value) {
499-
if (value == null) {
500-
return true;
501-
}
502469
final var decimal = new BigDecimal(value.toString());
503470
return min.compareTo(decimal) <= 0 && max.compareTo(decimal) >= 0;
504471
}

validator/src/main/java/io/avaje/validation/core/adapters/UriAdapter.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ final class UriAdapter extends AbstractConstraintAdapter<Object> {
3030

3131
@Override
3232
protected boolean isValid(Object value) {
33-
if (value == null) {
34-
return true;
35-
}
3633
try {
3734
final var stringValue = String.valueOf(value);
3835
final var uri = URI.create(stringValue);

validator/src/main/java/io/avaje/validation/core/adapters/UuidAdapter.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ final class UuidAdapter extends AbstractConstraintAdapter<Object> {
1313

1414
@Override
1515
protected boolean isValid(Object value) {
16-
if (value == null) {
17-
return true;
18-
}
1916
try {
2017
UUID.fromString(String.valueOf(value));
2118
return true;

validator/src/test/java/io/avaje/validation/core/AddressValidationAdapter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public AddressValidationAdapter(ValidationContext ctx) {
2121

2222
@Override
2323
public boolean validate(Address pojo, ValidationRequest request, String propertyName) {
24+
25+
if (pojo == null) return false;
26+
2427
if (propertyName != null) {
2528
request.pushPath(propertyName);
2629
}

validator/src/test/java/io/avaje/validation/core/ContactValidationAdapter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public ContactValidationAdapter(ValidationContext ctx) {
2727

2828
@Override
2929
public boolean validate(Contact pojo, ValidationRequest request, String propertyName) {
30+
31+
if (pojo == null) return false;
32+
3033
if (propertyName != null) {
3134
request.pushPath(propertyName);
3235
}

validator/src/test/java/io/avaje/validation/core/CustomerValidationAdapter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public CustomerValidationAdapter(ValidationContext ctx) {
5555

5656
@Override
5757
public boolean validate(Customer value, ValidationRequest request, String propertyName) {
58+
59+
if (value == null) return false;
60+
5861
activeAdapter.validate(value.active, request, "active");
5962
nameAdapter.validate(value.name, request, "name");
6063
activeDateAdapter.validate(value.activeDate, request, "activeDate");

0 commit comments

Comments
 (0)