1
1
package io .avaje .validation .core .adapters ;
2
2
3
- import java .lang .reflect .Array ;
4
3
import java .util .Collection ;
5
4
import java .util .List ;
6
5
import java .util .Map ;
15
14
public final class BasicAdapters {
16
15
private BasicAdapters () {}
17
16
18
- public static final ValidationContext .AnnotationFactory FACTORY = (annotationType , context , attributes ) ->
19
- switch (annotationType .getSimpleName ()) {
20
- case "Email" -> new EmailAdapter (context .message (attributes ), attributes );
21
- case "Null" -> new NullableAdapter (context .message (attributes ), true );
22
- case "NotNull" , "NonNull" -> new NullableAdapter (context .message (attributes ), false );
23
- case "AssertTrue" -> new AssertBooleanAdapter (context .message (attributes ), Boolean .FALSE );
24
- case "AssertFalse" -> new AssertBooleanAdapter (context .message (attributes ), Boolean .TRUE );
25
- case "NotBlank" -> new NotBlankAdapter (context .message (attributes ));
26
- case "NotEmpty" -> new NotEmptyAdapter (context .message (attributes ));
27
- case "Past" -> new FuturePastAdapter (context .message (attributes ), true , false );
28
- case "PastOrPresent" -> new FuturePastAdapter (context .message (attributes ), true , true );
29
- case "Future" -> new FuturePastAdapter (context .message (attributes ), false , false );
30
- case "FutureOrPresent" -> new FuturePastAdapter (context .message (attributes ), false , true );
31
- case "Pattern" -> new PatternAdapter (context .message (attributes ), attributes );
32
- case "Size" -> new SizeAdapter (context .message (attributes ), attributes );
33
- default -> null ;
34
- };
17
+ public static final ValidationContext .AnnotationFactory FACTORY =
18
+ (annotationType , context , attributes ) ->
19
+ switch (annotationType .getSimpleName ()) {
20
+ case "Email" -> new EmailAdapter (context .message (attributes ), attributes );
21
+ case "Null" -> new NullableAdapter (context .message (attributes ), true );
22
+ case "NotNull" , "NonNull" -> new NullableAdapter (context .message (attributes ), false );
23
+ case "AssertTrue" -> new AssertBooleanAdapter (context .message (attributes ), Boolean .TRUE );
24
+ case "AssertFalse" -> new AssertBooleanAdapter (context .message (attributes ), Boolean .FALSE );
25
+ case "NotBlank" -> new NotBlankAdapter (context .message (attributes ));
26
+ case "NotEmpty" -> new NotEmptyAdapter (context .message (attributes ));
27
+ case "Pattern" -> new PatternAdapter (context .message (attributes ), attributes );
28
+ case "Size" -> new SizeAdapter (context .message (attributes ), attributes );
29
+ default -> null ;
30
+ };
35
31
36
32
private static final class PatternAdapter implements ValidationAdapter <CharSequence > {
37
33
@@ -54,7 +50,11 @@ private static final class PatternAdapter implements ValidationAdapter<CharSeque
54
50
55
51
@ Override
56
52
public boolean validate (CharSequence value , ValidationRequest req , String propertyName ) {
57
- if (value == null || pattern .test (value .toString ())) {
53
+ if (value == null ) {
54
+ return true ;
55
+ }
56
+
57
+ if (pattern .test (value .toString ())) {
58
58
req .addViolation (message , propertyName );
59
59
return false ;
60
60
}
@@ -99,7 +99,7 @@ public boolean validate(Object value, ValidationRequest req, String propertyName
99
99
return len > 0 ;
100
100
}
101
101
} else if (value .getClass ().isArray ()) {
102
- final var len = Array . getLength (value );
102
+ final var len = arrayLength (value );
103
103
if (len > max || len < min ) {
104
104
req .addViolation (message , propertyName );
105
105
return len > 0 ;
@@ -170,7 +170,7 @@ public boolean validate(Object value, ValidationRequest req, String propertyName
170
170
return false ;
171
171
}
172
172
} else if (value .getClass ().isArray ()) {
173
- final var len = Array . getLength (value );
173
+ final var len = arrayLength (value );
174
174
if (len == 0 ) {
175
175
req .addViolation (message , propertyName );
176
176
return false ;
@@ -193,7 +193,11 @@ private static final class AssertBooleanAdapter implements ValidationAdapter<Boo
193
193
194
194
@ Override
195
195
public boolean validate (Boolean type , ValidationRequest req , String propertyName ) {
196
- if (assertBool .equals (type )) {
196
+ if (!assertBool .booleanValue () && type == null ) {
197
+ return true ;
198
+ }
199
+
200
+ if (!assertBool .equals (type )) {
197
201
req .addViolation (message , propertyName );
198
202
return false ;
199
203
}
@@ -220,4 +224,27 @@ public boolean validate(Object value, ValidationRequest req, String propertyName
220
224
return true ;
221
225
}
222
226
}
227
+
228
+ private static int arrayLength (Object array ) {
229
+
230
+ if (array instanceof int [] arr ) {
231
+ return arr .length ;
232
+ } else if (array instanceof boolean [] arr ) {
233
+ return arr .length ;
234
+ } else if (array instanceof byte [] arr ) {
235
+ return arr .length ;
236
+ } else if (array instanceof char [] arr ) {
237
+ return arr .length ;
238
+ } else if (array instanceof short [] arr ) {
239
+ return arr .length ;
240
+ } else if (array instanceof float [] arr ) {
241
+ return arr .length ;
242
+ } else if (array instanceof double [] arr ) {
243
+ return arr .length ;
244
+ } else if (array instanceof long [] arr ) {
245
+ return arr .length ;
246
+ } else {
247
+ return ((Object []) array ).length ;
248
+ }
249
+ }
223
250
}
0 commit comments