@@ -70,6 +70,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
70
70
CollectorContext .getInstance ().add (UnEvaluatedPropertiesValidator .EVALUATED_PROPERTIES , new ArrayList <>());
71
71
72
72
try {
73
+ int numberOfValidSubSchemas = 0 ;
73
74
for (JsonSchema schema : schemas ) {
74
75
Set <ValidationMessage > errors = new HashSet <>();
75
76
if (schema .getValidators ().containsKey (typeValidatorName )) {
@@ -86,6 +87,17 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
86
87
} else {
87
88
errors = schema .walk (node , rootNode , at , true );
88
89
}
90
+
91
+ // check if any validation errors have occurred
92
+ if (errors .isEmpty ()) {
93
+ // check whether there are no errors HOWEVER we have validated the exact validator
94
+ if (!state .hasMatchedNode ()) {
95
+ continue ;
96
+ }
97
+ // we found a valid subschema, so increase counter
98
+ numberOfValidSubSchemas ++;
99
+ }
100
+
89
101
if (errors .isEmpty () && (!this .validationContext .getConfig ().isOpenAPI3StyleDiscriminators ())) {
90
102
// Clear all errors.
91
103
allErrors .clear ();
@@ -106,6 +118,14 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
106
118
allErrors .addAll (errors );
107
119
}
108
120
121
+ // determine only those errors which are NOT of type "required" property missing
122
+ Set <ValidationMessage > childNotRequiredErrors = allErrors .stream ().filter (error -> !ValidatorTypeCode .REQUIRED .getValue ().equals (error .getType ())).collect (Collectors .toSet ());
123
+
124
+ // in case we had at least one (anyOf, i.e. any number >= 1 of) valid subschemas, we can remove all other errors about "required" properties
125
+ if (numberOfValidSubSchemas >= 1 && childNotRequiredErrors .isEmpty ()) {
126
+ allErrors = childNotRequiredErrors ;
127
+ }
128
+
109
129
if (this .validationContext .getConfig ().isOpenAPI3StyleDiscriminators () && discriminatorContext .isActive ()) {
110
130
final Set <ValidationMessage > errors = new HashSet <ValidationMessage >();
111
131
errors .add (buildValidationMessage (at , "based on the provided discriminator. No alternative could be chosen based on the discriminator property" ));
0 commit comments