Skip to content

Commit b47ef33

Browse files
committed
#173 - Do not ignore other errors in case of anyOf validator
1 parent 41b3623 commit b47ef33

File tree

3 files changed

+117
-7
lines changed

3 files changed

+117
-7
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,14 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
4545

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

5049
for (JsonSchema schema : schemas) {
5150
if (schema.validators.containsKey(typeValidatorName)) {
5251
TypeValidator typeValidator = ((TypeValidator) schema.validators.get(typeValidatorName));
5352
//If schema has type validator and node type doesn't match with schemaType then ignore it
5453
//For union type, it is must to call TypeValidator
5554
if (typeValidator.getSchemaType() != JsonType.UNION && !typeValidator.equalsToSchemaType(node)) {
56-
expectedTypeList.add(typeValidator.getSchemaType().toString());
55+
allErrors.add(buildValidationMessage(at, typeValidator.getSchemaType().toString()));
5756
continue;
5857
}
5958
}
@@ -63,9 +62,6 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
6362
}
6463
allErrors.addAll(errors);
6564
}
66-
if (!expectedTypeList.isEmpty()) {
67-
return Collections.singleton(buildValidationMessage(at, expectedTypeList.toArray(new String[expectedTypeList.size()])));
68-
}
6965
return Collections.unmodifiableSet(allErrors);
7066
}
7167

src/test/java/com/networknt/schema/JsonSchemaTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,25 @@ private void runTestFile(String testCaseFile) throws Exception {
9595

9696
if (test.get("valid").asBoolean()) {
9797
if (!errors.isEmpty()) {
98-
System.out.println("---- test case filed ----");
98+
System.out.println("---- test case failed ----");
9999
System.out.println("schema: " + schema.toString());
100100
System.out.println("data: " + test.get("data"));
101101
}
102102
Assert.assertEquals(0, errors.size());
103103
} else {
104104
if (errors.isEmpty()) {
105-
System.out.println("---- test case filed ----");
105+
System.out.println("---- test case failed ----");
106106
System.out.println("schema: " + schema);
107107
System.out.println("data: " + test.get("data"));
108+
} else {
109+
JsonNode errorCount = test.get("errorCount");
110+
if (errorCount != null && errorCount.isInt() && errors.size() != errorCount.asInt()) {
111+
System.out.println("---- test case failed ----");
112+
System.out.println("schema: " + schema);
113+
System.out.println("data: " + test.get("data"));
114+
System.out.println("errors: " + errors);
115+
Assert.assertEquals("expected error count", errorCount.asInt(), errors.size());
116+
}
108117
}
109118
Assert.assertEquals(false, errors.isEmpty());
110119
}

src/test/resources/tests/anyOf.json

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,110 @@
9494
"valid": true
9595
}
9696
]
97+
},
98+
{
99+
"description": "all errors are listed",
100+
"schema": {
101+
"anyOf": [
102+
{
103+
"type": "object",
104+
"properties": {
105+
"test": {
106+
"type": "string",
107+
"enum": [ "A", "B" ]
108+
}
109+
}
110+
},
111+
{
112+
"type": "null"
113+
}
114+
]
115+
},
116+
"tests": [
117+
{
118+
"description": "neither anyOf valid",
119+
"data": {
120+
"test": "C"
121+
},
122+
"valid": false,
123+
"errorCount": 2
124+
},
125+
{
126+
"description": "first anyOf valid",
127+
"data": null,
128+
"valid": true
129+
},
130+
{
131+
"description": "second anyOf valid",
132+
"data": {
133+
"test": "A"
134+
},
135+
"valid": true
136+
}
137+
]
138+
},
139+
{
140+
"description": "all errors are listed",
141+
"schema": {
142+
"type": "array",
143+
"items": {
144+
"anyOf": [
145+
{
146+
"type": "object",
147+
"properties": {
148+
"test": {
149+
"type": "string",
150+
"enum": [ "A", "B" ]
151+
}
152+
}
153+
},
154+
{
155+
"type": "null"
156+
}
157+
]
158+
}
159+
},
160+
"tests": [
161+
{
162+
"description": "neither anyOf valid",
163+
"data": [{
164+
"test": "C"
165+
}],
166+
"valid": false,
167+
"errorCount": 2
168+
},
169+
{
170+
"description": "first anyOf valid",
171+
"data": [null],
172+
"valid": true
173+
},
174+
{
175+
"description": "second anyOf valid",
176+
"data": [{
177+
"test": "A"
178+
}],
179+
"valid": true
180+
},
181+
{
182+
"description": "mixed anyOf valid",
183+
"data": [{
184+
"test": "A"
185+
}, null, {
186+
"test": 2
187+
}],
188+
"valid": false,
189+
"errorCount": 3
190+
},
191+
{
192+
"description": "mixed anyOf valid",
193+
"data": [{
194+
"test": "A"
195+
}, null, {
196+
"test": 2
197+
}, 2],
198+
"valid": false,
199+
"errorCount": 5
200+
}
201+
]
97202
}
98203
]

0 commit comments

Comments
 (0)