@@ -70,6 +70,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
7070 CollectorContext .getInstance ().add (UnEvaluatedPropertiesValidator .EVALUATED_PROPERTIES , new ArrayList <>());
7171
7272 try {
73+ int numberOfValidSubSchemas = 0 ;
7374 for (JsonSchema schema : schemas ) {
7475 Set <ValidationMessage > errors = new HashSet <>();
7576 if (schema .getValidators ().containsKey (typeValidatorName )) {
@@ -86,6 +87,17 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
8687 } else {
8788 errors = schema .walk (node , rootNode , at , true );
8889 }
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+
89101 if (errors .isEmpty () && (!this .validationContext .getConfig ().isOpenAPI3StyleDiscriminators ())) {
90102 // Clear all errors.
91103 allErrors .clear ();
@@ -106,6 +118,14 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
106118 allErrors .addAll (errors );
107119 }
108120
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+
109129 if (this .validationContext .getConfig ().isOpenAPI3StyleDiscriminators () && discriminatorContext .isActive ()) {
110130 final Set <ValidationMessage > errors = new HashSet <ValidationMessage >();
111131 errors .add (buildValidationMessage (at , "based on the provided discriminator. No alternative could be chosen based on the discriminator property" ));
0 commit comments