@@ -35,7 +35,8 @@ public static void validateMapTypeValue(
35
35
final Map <String , Object > sourceValue ,
36
36
final Object fieldMap ,
37
37
final int depth ,
38
- final long maxDepth
38
+ final long maxDepth ,
39
+ final boolean allowEmpty
39
40
) {
40
41
if (sourceValue == null ) return ; // allow map type value to be null.
41
42
validateDepth (sourceKey , depth , maxDepth );
@@ -55,37 +56,51 @@ public static void validateMapTypeValue(
55
56
Object nextSourceValue = sourceValue .get (key );
56
57
if (nextSourceValue != null ) {
57
58
if (nextSourceValue instanceof List ) {
58
- validateListTypeValue (key , (List ) nextSourceValue , fieldMap , depth + 1 , maxDepth );
59
+ validateListTypeValue (key , (List ) nextSourceValue , fieldMap , depth + 1 , maxDepth , allowEmpty );
59
60
} else if (nextSourceValue instanceof Map ) {
60
- validateMapTypeValue (key , (Map <String , Object >) nextSourceValue , nextFieldMap , depth + 1 , maxDepth );
61
+ validateMapTypeValue (key , (Map <String , Object >) nextSourceValue , nextFieldMap , depth + 1 , maxDepth , allowEmpty );
61
62
} else if (!(nextSourceValue instanceof String )) {
62
63
throw new IllegalArgumentException ("map type field [" + key + "] is neither string nor nested type, cannot process it" );
63
- } else if (StringUtils .isBlank ((String ) nextSourceValue )) {
64
+ } else if (! allowEmpty && StringUtils .isBlank ((String ) nextSourceValue )) {
64
65
throw new IllegalArgumentException ("map type field [" + key + "] has empty string value, cannot process it" );
65
66
}
66
67
}
67
68
});
68
69
}
69
70
70
71
@ SuppressWarnings ({ "rawtypes" , "unchecked" })
71
- private static void validateListTypeValue (String sourceKey , List sourceValue , Object fieldMap , int depth , long maxDepth ) {
72
+ private static void validateListTypeValue (
73
+ String sourceKey ,
74
+ List sourceValue ,
75
+ Object fieldMap ,
76
+ int depth ,
77
+ long maxDepth ,
78
+ boolean allowEmpty
79
+ ) {
72
80
validateDepth (sourceKey , depth , maxDepth );
73
81
if (sourceValue == null || sourceValue .isEmpty ()) return ;
74
82
Object firstNonNullElement = sourceValue .stream ().filter (Objects ::nonNull ).findFirst ().orElse (null );
75
83
if (firstNonNullElement == null ) return ;
76
84
for (Object element : sourceValue ) {
77
85
if (firstNonNullElement instanceof List ) { // nested list case.
78
- validateListTypeValue (sourceKey , (List ) element , fieldMap , depth + 1 , maxDepth );
86
+ validateListTypeValue (sourceKey , (List ) element , fieldMap , depth + 1 , maxDepth , allowEmpty );
79
87
} else if (firstNonNullElement instanceof Map ) {
80
- validateMapTypeValue (sourceKey , (Map <String , Object >) element , ((Map ) fieldMap ).get (sourceKey ), depth + 1 , maxDepth );
88
+ validateMapTypeValue (
89
+ sourceKey ,
90
+ (Map <String , Object >) element ,
91
+ ((Map ) fieldMap ).get (sourceKey ),
92
+ depth + 1 ,
93
+ maxDepth ,
94
+ allowEmpty
95
+ );
81
96
} else if (!(firstNonNullElement instanceof String )) {
82
97
throw new IllegalArgumentException ("list type field [" + sourceKey + "] has non string value, cannot process it" );
83
98
} else {
84
99
if (element == null ) {
85
100
throw new IllegalArgumentException ("list type field [" + sourceKey + "] has null, cannot process it" );
86
101
} else if (!(element instanceof String )) {
87
102
throw new IllegalArgumentException ("list type field [" + sourceKey + "] has non string value, cannot process it" );
88
- } else if (StringUtils .isBlank (element .toString ())) {
103
+ } else if (! allowEmpty && StringUtils .isBlank (element .toString ())) {
89
104
throw new IllegalArgumentException ("list type field [" + sourceKey + "] has empty string, cannot process it" );
90
105
}
91
106
}
0 commit comments