Skip to content

Commit

Permalink
Merge pull request #176 from grssam/anyOfErrorFix
Browse files Browse the repository at this point in the history
#173 - Do not ignore other errors in case of anyOf validator
  • Loading branch information
stevehu committed Jul 29, 2019
2 parents 41b3623 + b47ef33 commit 3b53147
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 7 deletions.
6 changes: 1 addition & 5 deletions src/main/java/com/networknt/schema/AnyOfValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String

Set<ValidationMessage> allErrors = new LinkedHashSet<ValidationMessage>();
String typeValidatorName = "anyOf/type";
List<String> expectedTypeList = new ArrayList<String>();

for (JsonSchema schema : schemas) {
if (schema.validators.containsKey(typeValidatorName)) {
TypeValidator typeValidator = ((TypeValidator) schema.validators.get(typeValidatorName));
//If schema has type validator and node type doesn't match with schemaType then ignore it
//For union type, it is must to call TypeValidator
if (typeValidator.getSchemaType() != JsonType.UNION && !typeValidator.equalsToSchemaType(node)) {
expectedTypeList.add(typeValidator.getSchemaType().toString());
allErrors.add(buildValidationMessage(at, typeValidator.getSchemaType().toString()));
continue;
}
}
Expand All @@ -63,9 +62,6 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
}
allErrors.addAll(errors);
}
if (!expectedTypeList.isEmpty()) {
return Collections.singleton(buildValidationMessage(at, expectedTypeList.toArray(new String[expectedTypeList.size()])));
}
return Collections.unmodifiableSet(allErrors);
}

Expand Down
13 changes: 11 additions & 2 deletions src/test/java/com/networknt/schema/JsonSchemaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,25 @@ private void runTestFile(String testCaseFile) throws Exception {

if (test.get("valid").asBoolean()) {
if (!errors.isEmpty()) {
System.out.println("---- test case filed ----");
System.out.println("---- test case failed ----");
System.out.println("schema: " + schema.toString());
System.out.println("data: " + test.get("data"));
}
Assert.assertEquals(0, errors.size());
} else {
if (errors.isEmpty()) {
System.out.println("---- test case filed ----");
System.out.println("---- test case failed ----");
System.out.println("schema: " + schema);
System.out.println("data: " + test.get("data"));
} else {
JsonNode errorCount = test.get("errorCount");
if (errorCount != null && errorCount.isInt() && errors.size() != errorCount.asInt()) {
System.out.println("---- test case failed ----");
System.out.println("schema: " + schema);
System.out.println("data: " + test.get("data"));
System.out.println("errors: " + errors);
Assert.assertEquals("expected error count", errorCount.asInt(), errors.size());
}
}
Assert.assertEquals(false, errors.isEmpty());
}
Expand Down
105 changes: 105 additions & 0 deletions src/test/resources/tests/anyOf.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,110 @@
"valid": true
}
]
},
{
"description": "all errors are listed",
"schema": {
"anyOf": [
{
"type": "object",
"properties": {
"test": {
"type": "string",
"enum": [ "A", "B" ]
}
}
},
{
"type": "null"
}
]
},
"tests": [
{
"description": "neither anyOf valid",
"data": {
"test": "C"
},
"valid": false,
"errorCount": 2
},
{
"description": "first anyOf valid",
"data": null,
"valid": true
},
{
"description": "second anyOf valid",
"data": {
"test": "A"
},
"valid": true
}
]
},
{
"description": "all errors are listed",
"schema": {
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"properties": {
"test": {
"type": "string",
"enum": [ "A", "B" ]
}
}
},
{
"type": "null"
}
]
}
},
"tests": [
{
"description": "neither anyOf valid",
"data": [{
"test": "C"
}],
"valid": false,
"errorCount": 2
},
{
"description": "first anyOf valid",
"data": [null],
"valid": true
},
{
"description": "second anyOf valid",
"data": [{
"test": "A"
}],
"valid": true
},
{
"description": "mixed anyOf valid",
"data": [{
"test": "A"
}, null, {
"test": 2
}],
"valid": false,
"errorCount": 3
},
{
"description": "mixed anyOf valid",
"data": [{
"test": "A"
}, null, {
"test": 2
}, 2],
"valid": false,
"errorCount": 5
}
]
}
]

0 comments on commit 3b53147

Please sign in to comment.