Skip to content

Commit 22e28b9

Browse files
author
Klas Kalaß
committed
One of was broken - it did not fail when there were no valid schemas
1 parent 4393ff5 commit 22e28b9

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
141141
continue;
142142
}
143143
JsonSchema schema = validator.schema;
144-
Set<ValidationMessage> schemaErrors = schema.validate(node, rootNode, at);
144+
Set<ValidationMessage> schemaErrors = schema.validate(node, rootNode, at);
145145
if (schemaErrors.isEmpty()) {
146146
numberOfValidSchema++;
147147
errors = new LinkedHashSet<ValidationMessage>();
148148
}
149149
if(numberOfValidSchema == 0){
150150
errors.addAll(schemaErrors);
151-
}
151+
}
152152
if (numberOfValidSchema > 1) {
153153
break;
154154
}
@@ -161,10 +161,10 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
161161
if (ValidatorTypeCode.ADDITIONAL_PROPERTIES.getValue().equals(msg.getType())) {
162162
it.remove();
163163
}
164-
if (errors.isEmpty()) {
165-
// ensure there is always an error reported if number of valid schemas is 0
166-
errors.add(buildValidationMessage(at, ""));
167-
}
164+
}
165+
if (errors.isEmpty()) {
166+
// ensure there is always an error reported if number of valid schemas is 0
167+
errors.add(buildValidationMessage(at, ""));
168168
}
169169
}
170170
if (numberOfValidSchema > 1) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public void testNotValidator() throws Exception {
228228
public void testOneOfValidator() throws Exception {
229229
runTestFile("tests/oneOf.json");
230230
}
231-
231+
232232
@Test
233233
public void testPatternValidator() throws Exception {
234234
runTestFile("tests/pattern.json");

src/test/resources/tests/oneOf.json

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,62 @@
6464
"valid": false
6565
}
6666
]
67+
},
68+
{
69+
"description": "oneOf with objects and type pattern",
70+
"schema": {
71+
"oneOf": [
72+
{
73+
"type": "object",
74+
"required": [
75+
"type"
76+
],
77+
"properties": {
78+
"type": {
79+
"enum": [
80+
"TYPE_1"
81+
]
82+
}
83+
}
84+
},
85+
{
86+
"type": "object",
87+
"required": [
88+
"type"
89+
],
90+
"properties": {
91+
"type": {
92+
"enum": [
93+
"TYPE_2"
94+
]
95+
}
96+
}
97+
}
98+
99+
]
100+
},
101+
"tests": [
102+
{
103+
"description": "first oneOf valid",
104+
"data": {
105+
"type": "TYPE_1"
106+
},
107+
"valid": true
108+
},
109+
{
110+
"description": "second oneOf valid",
111+
"data": {
112+
"type": "TYPE_2"
113+
},
114+
"valid": true
115+
},
116+
{
117+
"description": "neither oneOf valid",
118+
"data": {
119+
"type": "INVALID_TYPE"
120+
},
121+
"valid": false
122+
}
123+
]
67124
}
68125
]

0 commit comments

Comments
 (0)