Skip to content

Commit 625234e

Browse files
committed
Fixes #110 Validation Error when using OneOf in OpenAPI specs
1 parent dd0dac3 commit 625234e

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ public OneOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
130130

131131
public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) {
132132
debug(logger, node, rootNode, at);
133-
133+
134+
// this validator considers a missing node as an error
135+
// set it here to true, however re-set it to its original value upon finishing the validation
136+
boolean missingNodeAsError = config.isMissingNodeAsError();
137+
config.setMissingNodeAsError(true);
138+
134139
int numberOfValidSchema = 0;
135140
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
136141

@@ -171,6 +176,9 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
171176
errors = Collections.singleton(buildValidationMessage(at, ""));
172177
}
173178

179+
// reset the flag for error handling
180+
config.setMissingNodeAsError(missingNodeAsError);
181+
174182
return Collections.unmodifiableSet(errors);
175183
}
176184

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
4646
JsonNode propertyNode = node.get(entry.getKey());
4747

4848
if (propertyNode != null) {
49-
errors.addAll(propertySchema.validate(propertyNode, rootNode, at + "." + entry.getKey()));
49+
errors.addAll(propertySchema.validate(propertyNode, rootNode, at + "." + entry.getKey()));
50+
} else {
51+
// if a node could not be found, treat is as error/continue, depending on the SchemaValidatorsConfig
52+
if(config.isMissingNodeAsError())
53+
errors.add(buildValidationMessage(at, node.toString()));
5054
}
5155
}
5256

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ public class SchemaValidatorsConfig {
55
* when validate type, if TYPE_LOOSE = true, will try to convert string to different types to match the type defined in schema.
66
*/
77
private boolean typeLoose;
8-
8+
9+
/**
10+
* if IS_MISSING_NODE_AS_ERROR = true, the validator will ignore the missing node.
11+
* if set to false, then the validator will report an error
12+
*/
13+
private boolean missingNodeAsError = false;
14+
915
public boolean isTypeLoose() {
1016
return typeLoose;
1117
}
@@ -14,6 +20,14 @@ public void setTypeLoose(boolean typeLoose) {
1420
this.typeLoose = typeLoose;
1521
}
1622

23+
public boolean isMissingNodeAsError() {
24+
return missingNodeAsError;
25+
}
26+
27+
public void setMissingNodeAsError(boolean missingNodeAsError) {
28+
this.missingNodeAsError = missingNodeAsError;
29+
}
30+
1731
public SchemaValidatorsConfig() {
1832
loadDefaultConfig();
1933
}

0 commit comments

Comments
 (0)