Skip to content

Commit e7c21bc

Browse files
committed
Fixes #110 Validation Error when using OneOf in OpenAPI specs - additional recursive validation fix
1 parent 625234e commit e7c21bc

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,17 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
152152
errors = new LinkedHashSet<ValidationMessage>();
153153
}
154154
if(numberOfValidSchema == 0){
155-
errors.addAll(schemaErrors);
156-
}
155+
// one of has matched one of the elements
156+
// if it has an error here, it is due to an element validation error
157+
// within its child elements
158+
if(config.hasElementValidationError()) {
159+
errors.clear();
160+
errors.addAll(schemaErrors);
161+
break;
162+
} else {
163+
errors.addAll(schemaErrors);
164+
}
165+
}
157166
if (numberOfValidSchema > 1) {
158167
break;
159168
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
4747

4848
if (propertyNode != null) {
4949
errors.addAll(propertySchema.validate(propertyNode, rootNode, at + "." + entry.getKey()));
50+
51+
// this was a regular validation error; mark it as such
52+
if(!errors.isEmpty()) {
53+
config.setElementValidationError(true);
54+
}
5055
} else {
5156
// if a node could not be found, treat is as error/continue, depending on the SchemaValidatorsConfig
5257
if(config.isMissingNodeAsError())

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ public class SchemaValidatorsConfig {
1212
*/
1313
private boolean missingNodeAsError = false;
1414

15+
/**
16+
* if HAS_ELEMENT_VALIDATION_ERROR = true, the caller can decide, in conjunction with a missing node flag
17+
* on how to treat the error
18+
*/
19+
private boolean elementValidationError = false;
20+
1521
public boolean isTypeLoose() {
1622
return typeLoose;
1723
}
@@ -28,6 +34,14 @@ public void setMissingNodeAsError(boolean missingNodeAsError) {
2834
this.missingNodeAsError = missingNodeAsError;
2935
}
3036

37+
public boolean hasElementValidationError() {
38+
return elementValidationError;
39+
}
40+
41+
public void setElementValidationError(boolean elementValidationError) {
42+
this.elementValidationError = elementValidationError;
43+
}
44+
3145
public SchemaValidatorsConfig() {
3246
loadDefaultConfig();
3347
}

0 commit comments

Comments
 (0)