|
1 | | -/* |
2 | | - * Copyright (c) 2016 Network New Technologies Inc. |
3 | | - * |
4 | | - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | - * you may not use this file except in compliance with the License. |
6 | | - * You may obtain a copy of the License at |
7 | | - * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
10 | | - * Unless required by applicable law or agreed to in writing, software |
11 | | - * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | - * See the License for the specific language governing permissions and |
14 | | - * limitations under the License. |
15 | | - */ |
16 | | - |
17 | | -package com.networknt.schema; |
18 | | - |
19 | | -import com.fasterxml.jackson.databind.JsonNode; |
20 | | -import org.slf4j.Logger; |
21 | | -import org.slf4j.LoggerFactory; |
22 | | - |
23 | | -import java.util.ArrayList; |
24 | | -import java.util.Collections; |
25 | | -import java.util.LinkedHashSet; |
26 | | -import java.util.List; |
27 | | -import java.util.Set; |
28 | | - |
29 | | -public class ItemsValidator extends BaseJsonValidator implements JsonValidator { |
30 | | - private static final Logger logger = LoggerFactory.getLogger(ItemsValidator.class); |
31 | | - private static final String PROPERTY_ADDITIONAL_ITEMS = "additionalItems"; |
32 | | - |
33 | | - private JsonSchema schema; |
34 | | - private List<JsonSchema> tupleSchema; |
35 | | - private boolean additionalItems = true; |
36 | | - private JsonSchema additionalSchema; |
37 | | - |
38 | | - public ItemsValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) { |
39 | | - super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ITEMS, validationContext); |
40 | | - if (schemaNode.isObject()) { |
41 | | - schema = new JsonSchema(validationContext, getValidatorType().getValue(), schemaNode, parentSchema); |
42 | | - } else { |
43 | | - tupleSchema = new ArrayList<JsonSchema>(); |
44 | | - for (JsonNode s : schemaNode) { |
45 | | - tupleSchema.add(new JsonSchema(validationContext, getValidatorType().getValue(), s, parentSchema)); |
46 | | - } |
47 | | - |
48 | | - JsonNode addItemNode = getParentSchema().getSchemaNode().get(PROPERTY_ADDITIONAL_ITEMS); |
49 | | - if (addItemNode != null) { |
50 | | - if (addItemNode.isBoolean()) { |
51 | | - additionalItems = addItemNode.asBoolean(); |
52 | | - } else if (addItemNode.isObject()) { |
53 | | - additionalSchema = new JsonSchema(validationContext, addItemNode); |
54 | | - } |
55 | | - } |
56 | | - } |
57 | | - |
58 | | - parseErrorCode(getValidatorType().getErrorCodeKey()); |
59 | | - } |
60 | | - |
61 | | - public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) { |
62 | | - debug(logger, node, rootNode, at); |
63 | | - |
64 | | - Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>(); |
65 | | - |
66 | | - if (!node.isArray()) { |
67 | | - // ignores non-arrays |
68 | | - return errors; |
69 | | - } |
70 | | - |
71 | | - int i = 0; |
72 | | - for (JsonNode n : node) { |
73 | | - if (schema != null) { |
74 | | - // validate with item schema (the whole array has the same item |
75 | | - // schema) |
76 | | - errors.addAll(schema.validate(n, rootNode, at + "[" + i + "]")); |
77 | | - } |
78 | | - |
79 | | - if (tupleSchema != null) { |
80 | | - if (i < tupleSchema.size()) { |
81 | | - // validate against tuple schema |
82 | | - errors.addAll(tupleSchema.get(i).validate(n, rootNode, at + "[" + i + "]")); |
83 | | - } else { |
84 | | - if (additionalSchema != null) { |
85 | | - // validate against additional item schema |
86 | | - errors.addAll(additionalSchema.validate(n, rootNode, at + "[" + i + "]")); |
87 | | - } else if (!additionalItems) { |
88 | | - // no additional item allowed, return error |
89 | | - errors.add(buildValidationMessage(at, "" + i)); |
90 | | - } |
91 | | - } |
92 | | - } |
93 | | - |
94 | | - i++; |
95 | | - } |
96 | | - return Collections.unmodifiableSet(errors); |
97 | | - } |
98 | | - |
99 | | -} |
| 1 | +/* |
| 2 | + * Copyright (c) 2016 Network New Technologies Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.networknt.schema; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.databind.JsonNode; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.LinkedHashSet; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Set; |
| 28 | + |
| 29 | +public class ItemsValidator extends BaseJsonValidator implements JsonValidator { |
| 30 | + private static final Logger logger = LoggerFactory.getLogger(ItemsValidator.class); |
| 31 | + private static final String PROPERTY_ADDITIONAL_ITEMS = "additionalItems"; |
| 32 | + |
| 33 | + private JsonSchema schema; |
| 34 | + private List<JsonSchema> tupleSchema; |
| 35 | + private boolean additionalItems = true; |
| 36 | + private JsonSchema additionalSchema; |
| 37 | + |
| 38 | + public ItemsValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSchema, ValidationContext validationContext) { |
| 39 | + super(schemaPath, schemaNode, parentSchema, ValidatorTypeCode.ITEMS, validationContext); |
| 40 | + if (schemaNode.isObject()) { |
| 41 | + schema = new JsonSchema(validationContext, getValidatorType().getValue(), schemaNode, parentSchema); |
| 42 | + } else { |
| 43 | + tupleSchema = new ArrayList<JsonSchema>(); |
| 44 | + for (JsonNode s : schemaNode) { |
| 45 | + tupleSchema.add(new JsonSchema(validationContext, getValidatorType().getValue(), s, parentSchema)); |
| 46 | + } |
| 47 | + |
| 48 | + JsonNode addItemNode = getParentSchema().getSchemaNode().get(PROPERTY_ADDITIONAL_ITEMS); |
| 49 | + if (addItemNode != null) { |
| 50 | + if (addItemNode.isBoolean()) { |
| 51 | + additionalItems = addItemNode.asBoolean(); |
| 52 | + } else if (addItemNode.isObject()) { |
| 53 | + additionalSchema = new JsonSchema(validationContext, addItemNode); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + parseErrorCode(getValidatorType().getErrorCodeKey()); |
| 59 | + } |
| 60 | + |
| 61 | + public Set<ValidationMessage> validate(JsonNode node, JsonNode rootNode, String at) { |
| 62 | + debug(logger, node, rootNode, at); |
| 63 | + |
| 64 | + Set<ValidationMessage> errors = new LinkedHashSet<ValidationMessage>(); |
| 65 | + |
| 66 | + if (!node.isArray() && !config.isTypeLoose()) { |
| 67 | + // ignores non-arrays |
| 68 | + return errors; |
| 69 | + } |
| 70 | + if (node.isArray()) { |
| 71 | + int i = 0; |
| 72 | + for (JsonNode n : node) { |
| 73 | + doValidate(errors, i, n, rootNode, at); |
| 74 | + i++; |
| 75 | + } |
| 76 | + } else { |
| 77 | + doValidate(errors, 0, node, rootNode, at); |
| 78 | + } |
| 79 | + return Collections.unmodifiableSet(errors); |
| 80 | + } |
| 81 | + |
| 82 | + private void doValidate(Set<ValidationMessage> errors, int i, JsonNode node, JsonNode rootNode, String at) { |
| 83 | + if (schema != null) { |
| 84 | + // validate with item schema (the whole array has the same item |
| 85 | + // schema) |
| 86 | + errors.addAll(schema.validate(node, rootNode, at + "[" + i + "]")); |
| 87 | + } |
| 88 | + |
| 89 | + if (tupleSchema != null) { |
| 90 | + if (i < tupleSchema.size()) { |
| 91 | + // validate against tuple schema |
| 92 | + errors.addAll(tupleSchema.get(i).validate(node, rootNode, at + "[" + i + "]")); |
| 93 | + } else { |
| 94 | + if (additionalSchema != null) { |
| 95 | + // validate against additional item schema |
| 96 | + errors.addAll(additionalSchema.validate(node, rootNode, at + "[" + i + "]")); |
| 97 | + } else if (!additionalItems) { |
| 98 | + // no additional item allowed, return error |
| 99 | + errors.add(buildValidationMessage(at, "" + i)); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments