|
17 | 17 | package com.networknt.schema;
|
18 | 18 |
|
19 | 19 | import com.fasterxml.jackson.databind.JsonNode;
|
20 |
| -import com.fasterxml.jackson.databind.node.ArrayNode; |
21 |
| -import com.fasterxml.jackson.databind.node.NullNode; |
22 |
| -import com.fasterxml.jackson.databind.node.ObjectNode; |
23 |
| -import com.networknt.schema.utils.JsonNodeUtil; |
24 | 20 | import org.slf4j.Logger;
|
25 | 21 | import org.slf4j.LoggerFactory;
|
26 | 22 |
|
27 | 23 | import java.util.*;
|
28 | 24 |
|
29 | 25 | public class OneOfValidator extends BaseJsonValidator implements JsonValidator {
|
30 |
| - |
31 | 26 | private static final Logger logger = LoggerFactory.getLogger(OneOfValidator.class);
|
32 | 27 |
|
33 | 28 | private final List<ShortcutValidator> schemas = new ArrayList<ShortcutValidator>();
|
34 | 29 |
|
35 |
| - private final ValidationContext validationContext; |
36 |
| - |
37 | 30 | private static class ShortcutValidator {
|
38 | 31 | private final JsonSchema schema;
|
39 | 32 | private final Map<String, String> constants;
|
40 |
| - private final ValidationContext validationContext; |
41 | 33 |
|
42 | 34 | ShortcutValidator(JsonNode schemaNode, JsonSchema parentSchema,
|
43 | 35 | ValidationContext validationContext, JsonSchema schema) {
|
44 | 36 | JsonNode refNode = schemaNode.get(ValidatorTypeCode.REF.getValue());
|
45 | 37 | JsonSchema resolvedRefSchema = refNode != null && refNode.isTextual() ? RefValidator.getRefSchema(parentSchema, validationContext, refNode.textValue()).getSchema() : null;
|
46 | 38 | this.constants = extractConstants(schemaNode, resolvedRefSchema);
|
47 | 39 | this.schema = schema;
|
48 |
| - this.validationContext = validationContext; |
49 | 40 | }
|
50 | 41 |
|
51 | 42 | private Map<String, String> extractConstants(JsonNode schemaNode, JsonSchema resolvedRefSchema) {
|
@@ -130,7 +121,6 @@ public OneOfValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentS
|
130 | 121 | JsonSchema childSchema = new JsonSchema(validationContext, getValidatorType().getValue(), parentSchema.getCurrentUri(), childNode, parentSchema);
|
131 | 122 | schemas.add(new ShortcutValidator(childNode, parentSchema, validationContext, childSchema));
|
132 | 123 | }
|
133 |
| - this.validationContext = validationContext; |
134 | 124 | parseErrorCode(getValidatorType().getErrorCodeKey());
|
135 | 125 | }
|
136 | 126 |
|
@@ -163,49 +153,33 @@ public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String
|
163 | 153 | continue;
|
164 | 154 | }*/
|
165 | 155 |
|
166 |
| - //Check to see if it is already validated. |
167 |
| - if(!childErrors.isEmpty() && JsonNodeUtil.matchOneOfTypeNode(schemaNode,TypeFactory.getValueNodeType(node, this.validationContext.getConfig()))){ |
168 |
| - continue; |
169 |
| - } |
170 |
| - |
171 | 156 | // get the current validator
|
172 | 157 | JsonSchema schema = validator.schema;
|
173 |
| - |
174 |
| - //Skip the validation when the current node is oneOf type and it is not equal to schemaType. |
175 |
| - if (JsonNodeUtil.matchOneOfTypeNode(schemaNode, TypeFactory.getValueNodeType(node, this.validationContext.getConfig())) && |
176 |
| - !JsonNodeUtil.equalsToSchemaType(node, schema, this.validationContext.getConfig()) && !(JsonType.UNKNOWN.equals(JsonNodeUtil.getSchemaJsonType(schema)))) { |
177 |
| - continue; |
178 |
| - } |
179 |
| - |
180 | 158 | if (!state.isWalkEnabled()) {
|
181 | 159 | schemaErrors = schema.validate(node, rootNode, at);
|
182 | 160 | } else {
|
183 | 161 | schemaErrors = schema.walk(node, rootNode, at, state.isValidationEnabled());
|
184 | 162 | }
|
185 | 163 |
|
186 |
| - |
187 | 164 | // check if any validation errors have occurred
|
188 | 165 | if (schemaErrors.isEmpty()) {
|
189 | 166 | // check whether there are no errors HOWEVER we have validated the exact validator
|
190 | 167 | if (!state.hasMatchedNode())
|
191 | 168 | continue;
|
192 |
| - else |
193 |
| - numberOfValidSchema++; |
| 169 | + |
| 170 | + numberOfValidSchema++; |
194 | 171 | }
|
195 | 172 | childErrors.addAll(schemaErrors);
|
196 | 173 | }
|
| 174 | + |
| 175 | + |
197 | 176 | // ensure there is always an "OneOf" error reported if number of valid schemas is not equal to 1.
|
198 |
| - if (numberOfValidSchema > 1) { |
199 |
| - // check if the parent schema declares the fields as nullable |
200 |
| - if (!JsonType.NULL.equals(TypeFactory.getValueNodeType(node, this.validationContext.getConfig())) || |
201 |
| - !JsonNodeUtil.isNodeNullable(parentSchema.getSchemaNode(), this.validationContext.getConfig()) && |
202 |
| - !JsonNodeUtil.isChildNodeNullable((ArrayNode) schemaNode, this.validationContext.getConfig())) { |
203 |
| - final ValidationMessage message = getMultiSchemasValidErrorMsg(at); |
204 |
| - if (failFast) { |
205 |
| - throw new JsonSchemaException(message); |
206 |
| - } |
207 |
| - errors.add(message); |
| 177 | + if(numberOfValidSchema > 1){ |
| 178 | + final ValidationMessage message = getMultiSchemasValidErrorMsg(at); |
| 179 | + if( failFast ) { |
| 180 | + throw new JsonSchemaException(message); |
208 | 181 | }
|
| 182 | + errors.add(message); |
209 | 183 | }
|
210 | 184 | // ensure there is always an "OneOf" error reported if number of valid schemas is not equal to 1.
|
211 | 185 | else if (numberOfValidSchema < 1) {
|
@@ -262,7 +236,7 @@ private ValidationMessage getMultiSchemasValidErrorMsg(String at){
|
262 | 236 | }
|
263 | 237 |
|
264 | 238 | return ValidationMessage.of(getValidatorType().getValue(), ValidatorTypeCode.ONE_OF ,
|
265 |
| - at, String.format("but more than one schemas {%s} are valid ",msg)); |
| 239 | + at, String.format("but more than one schemas {%s} are valid ",msg)); |
266 | 240 | }
|
267 | 241 |
|
268 | 242 | @Override
|
|
0 commit comments