Skip to content

Commit 0f0c75f

Browse files
committed
Fixes #185 - OneOf validation error
1 parent aaa9e18 commit 0f0c75f

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,18 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
134134
// this validator considers a missing node as an error
135135
// set it here to true, however re-set it to its original value upon finishing the validation
136136
boolean missingNodeAsError = config.isMissingNodeAsError();
137-
config.setMissingNodeAsError(true);
137+
config.setMissingNodeAsError(false);
138138

139139
int numberOfValidSchema = 0;
140140
Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>();
141141

142+
// validate that only a single element has been received in the oneOf node
143+
// validation should not continue, as it contradicts the oneOf requirement of only one
144+
if(node.isObject() && node.size()>1) {
145+
errors = Collections.singleton(buildValidationMessage(at, ""));
146+
return Collections.unmodifiableSet(errors);
147+
}
148+
142149
for (ShortcutValidator validator : schemas) {
143150
if (!validator.allConstantsMatch(node)) {
144151
// take a shortcut: if there is any constant that does not match,
@@ -150,6 +157,10 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
150157
if (schemaErrors.isEmpty()) {
151158
numberOfValidSchema++;
152159
errors = new LinkedHashSet<ValidationMessage>();
160+
161+
// a valid schema has been identified, and the node was an object, break the loop
162+
if(node.isObject())
163+
break;
153164
}
154165
if(numberOfValidSchema == 0){
155166
errors.addAll(schemaErrors);
@@ -169,6 +180,8 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
169180
errors.add(buildValidationMessage(at, ""));
170181
}
171182
}
183+
184+
// validated upfront
172185
if (numberOfValidSchema > 1) {
173186
errors = Collections.singleton(buildValidationMessage(at, ""));
174187
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
4949
errors.addAll(propertySchema.validate(propertyNode, rootNode, at + "." + entry.getKey()));
5050
} else {
5151
// if a node could not be found, treat is as error/continue, depending on the SchemaValidatorsConfig
52-
if(config.isMissingNodeAsError()) {
52+
//if(config.isMissingNodeAsError()) {
5353
if(getParentSchema().hasRequiredValidator())
5454
errors.addAll(getParentSchema().getRequiredValidator().validate(node, rootNode, at));
55-
else
56-
errors.add(buildValidationMessage(at, node.toString()));
57-
}
55+
// else
56+
// errors.add(buildValidationMessage(at, node.toString()));
57+
//}
5858
}
5959
}
6060

0 commit comments

Comments
 (0)