25
25
import java .util .HashSet ;
26
26
import java .util .List ;
27
27
import java .util .Set ;
28
+ import java .util .stream .Collectors ;
28
29
29
30
public class OneOfValidator extends BaseJsonValidator implements JsonValidator {
30
31
private static final Logger logger = LoggerFactory .getLogger (RequiredValidator .class );
@@ -45,21 +46,33 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
45
46
debug (logger , node , rootNode , at );
46
47
47
48
int numberOfValidSchema = 0 ;
48
-
49
+ Set <ValidationMessage > errors = new HashSet <>();
50
+
49
51
for (JsonSchema schema : schemas ) {
50
- Set <ValidationMessage > errors = schema .validate (node , rootNode , at );
51
- if (errors == null || errors .isEmpty ()) {
52
+ Set <ValidationMessage > schemaErrors = schema .validate (node , rootNode , at );
53
+ if (schemaErrors .isEmpty ()) {
52
54
numberOfValidSchema ++;
55
+ errors = new HashSet <>();
53
56
}
57
+ if (numberOfValidSchema == 0 ){
58
+ errors .addAll (schemaErrors );
59
+ }
54
60
if (numberOfValidSchema > 1 ) {
55
61
break ;
56
62
}
57
63
}
58
-
59
- Set <ValidationMessage > errors = new HashSet <ValidationMessage >();
60
- if (numberOfValidSchema != 1 ) {
61
- errors .add (buildValidationMessage (at , "" ));
64
+
65
+ if (numberOfValidSchema == 0 ) {
66
+ errors = errors .stream ()
67
+ .filter (msg -> !ValidatorTypeCode .ADDITIONAL_PROPERTIES
68
+ .equals (ValidatorTypeCode .fromValue (msg .getType ())))
69
+ .collect (Collectors .toSet ());
70
+ }
71
+ if (numberOfValidSchema > 1 ) {
72
+ errors = new HashSet <>();
73
+ errors .add (buildValidationMessage (at , "" ));
62
74
}
75
+
63
76
return errors ;
64
77
}
65
78
0 commit comments