Skip to content

Commit 7e7bb53

Browse files
authored
Merge pull request #178 from jawaff/fix/#177-one-of-bug
Fix/#177 One Of Bug
2 parents 9ccc25e + 23b4fe2 commit 7e7bb53

File tree

4 files changed

+58
-32
lines changed

4 files changed

+58
-32
lines changed

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

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,7 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
152152
errors = new LinkedHashSet<ValidationMessage>();
153153
}
154154
if(numberOfValidSchema == 0){
155-
// one of has matched one of the elements
156-
// if it has an error here, it is due to an element validation error
157-
// within its child elements
158-
if(config.hasElementValidationError()) {
159-
errors.clear();
160-
errors.addAll(schemaErrors);
161-
break;
162-
} else {
163-
errors.addAll(schemaErrors);
164-
}
155+
errors.addAll(schemaErrors);
165156
}
166157
}
167158

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,7 @@ 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()));
50-
51-
// this was a regular validation error; mark it as such
52-
if(!errors.isEmpty()) {
53-
config.setElementValidationError(true);
54-
}
49+
errors.addAll(propertySchema.validate(propertyNode, rootNode, at + "." + entry.getKey()));
5550
} else {
5651
// if a node could not be found, treat is as error/continue, depending on the SchemaValidatorsConfig
5752
if(config.isMissingNodeAsError()) {

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,7 @@ public class SchemaValidatorsConfig {
3030
* if set to false, then the validator will report an error
3131
*/
3232
private boolean missingNodeAsError = false;
33-
34-
/**
35-
* if HAS_ELEMENT_VALIDATION_ERROR = true, the caller can decide, in conjunction with a missing node flag
36-
* on how to treat the error
37-
*/
38-
private boolean elementValidationError = false;
39-
33+
4034
/**
4135
* Map of public, normally internet accessible schema URLs to alternate locations; this allows for offline
4236
* validation of schemas that refer to public URLs. This is merged with any mappings the {@link JsonSchemaFactory}
@@ -68,15 +62,7 @@ public boolean isMissingNodeAsError() {
6862
public void setMissingNodeAsError(boolean missingNodeAsError) {
6963
this.missingNodeAsError = missingNodeAsError;
7064
}
71-
72-
public boolean hasElementValidationError() {
73-
return elementValidationError;
74-
}
75-
76-
public void setElementValidationError(boolean elementValidationError) {
77-
this.elementValidationError = elementValidationError;
78-
}
79-
65+
8066
public SchemaValidatorsConfig() {
8167
loadDefaultConfig();
8268
}

src/test/resources/tests/oneOf.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,59 @@
121121
"valid": false
122122
}
123123
]
124+
},
125+
{
126+
"description": "oneOf with objects and regex pattern",
127+
"schema": {
128+
"oneOf": [
129+
{
130+
"type": "object",
131+
"required": [
132+
"type"
133+
],
134+
"properties": {
135+
"type": {
136+
"type": "string",
137+
"pattern": "^TYPE_1$"
138+
}
139+
}
140+
},
141+
{
142+
"type": "object",
143+
"required": [
144+
"type"
145+
],
146+
"properties": {
147+
"type": {
148+
"type": "string",
149+
"pattern": "^TYPE_2$"
150+
}
151+
}
152+
}
153+
]
154+
},
155+
"tests": [
156+
{
157+
"description": "first oneOf valid",
158+
"data": {
159+
"type": "TYPE_1"
160+
},
161+
"valid": true
162+
},
163+
{
164+
"description": "second oneOf valid",
165+
"data": {
166+
"type": "TYPE_2"
167+
},
168+
"valid": true
169+
},
170+
{
171+
"description": "neither oneOf valid",
172+
"data": {
173+
"type": "INVALID_TYPE"
174+
},
175+
"valid": false
176+
}
177+
]
124178
}
125179
]

0 commit comments

Comments
 (0)