Skip to content

Commit ba17f1d

Browse files
authored
proposal for issue #535, part 2: fix the same issue in AnyOfValidator, too (#559)
(in addition to OneOfValidator)
1 parent 63cd3bf commit ba17f1d

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<modelVersion>4.0.0</modelVersion>
2121
<groupId>com.networknt</groupId>
2222
<artifactId>json-schema-validator</artifactId>
23-
<version>1.0.69</version>
23+
<version>1.0.70-SNAPSHOT</version>
2424
<packaging>bundle</packaging>
2525
<description>A json schema validator that supports draft v4, v6, v7 and v2019-09</description>
2626
<url>https://github.com/networknt/json-schema-validator</url>

src/main/java/com/networknt/schema/AnyOfValidator.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)