|
16 | 16 |
|
17 | 17 | package com.networknt.schema.keyword; |
18 | 18 |
|
| 19 | +import java.util.ArrayDeque; |
| 20 | +import java.util.Iterator; |
19 | 21 | import java.util.function.Consumer; |
20 | 22 |
|
21 | 23 | import com.fasterxml.jackson.databind.JsonNode; |
22 | 24 | import com.networknt.schema.ExecutionContext; |
| 25 | +import com.networknt.schema.Schema; |
23 | 26 | import com.networknt.schema.SchemaLocation; |
24 | 27 | import com.networknt.schema.annotation.Annotation; |
| 28 | +import com.networknt.schema.path.EvaluationPath; |
25 | 29 | import com.networknt.schema.path.NodePath; |
26 | 30 |
|
27 | 31 | /** |
@@ -122,4 +126,58 @@ protected void putAnnotation(ExecutionContext executionContext, Consumer<Annotat |
122 | 126 | customizer.accept(builder); |
123 | 127 | executionContext.getAnnotations().put(builder.build()); |
124 | 128 | } |
| 129 | + |
| 130 | + |
| 131 | + /** |
| 132 | + * Determines if the keyword exists adjacent in the evaluation path. |
| 133 | + * <p> |
| 134 | + * This does not check if the keyword exists in the current meta schema as this |
| 135 | + * can be a cross-draft case where the properties keyword is in a Draft 7 schema |
| 136 | + * and the unevaluatedProperties keyword is in an outer Draft 2020-12 schema. |
| 137 | + * <p> |
| 138 | + * The fact that the validator exists in the evaluation path implies that the |
| 139 | + * keyword was valid in whatever meta schema for that schema it was created for. |
| 140 | + * |
| 141 | + * @param keyword the keyword to check |
| 142 | + * @return true if found |
| 143 | + */ |
| 144 | + protected boolean hasAdjacentKeywordInEvaluationPath(ExecutionContext executionContext, String keyword) { |
| 145 | + Iterator<Object> evaluationSchemaPathIterator = executionContext.getEvaluationSchemaPath().descendingIterator(); |
| 146 | + Iterator<Schema> evaluationSchemaIterator = executionContext.getEvaluationSchema().descendingIterator(); |
| 147 | + |
| 148 | + ArrayDeque<Object> current = executionContext.getEvaluationSchemaPath().clone(); |
| 149 | + |
| 150 | + // Skip the first as this is the path pointing to the current keyword eg. properties eg /$ref/properties |
| 151 | + // What is needed is the evaluationPath pointing to the current evaluationSchema eg /$ref |
| 152 | + if (evaluationSchemaPathIterator.hasNext()) { |
| 153 | + evaluationSchemaPathIterator.next(); |
| 154 | + |
| 155 | + current.removeLast(); |
| 156 | + } |
| 157 | + |
| 158 | + while (evaluationSchemaIterator.hasNext()) { |
| 159 | + Schema schema = evaluationSchemaIterator.next(); |
| 160 | + for (KeywordValidator validator : schema.getValidators()) { |
| 161 | + if (keyword.equals(validator.getKeyword())) { |
| 162 | + return true; |
| 163 | + } |
| 164 | + } |
| 165 | + String newPath = new EvaluationPath(current).toString(); |
| 166 | + String oldPath = schema.getEvaluationPath().toString(); |
| 167 | + if (!oldPath.equals(newPath)) { |
| 168 | + System.out.println("OLD: "+oldPath); |
| 169 | + System.out.println("NEW: "+newPath); |
| 170 | + } |
| 171 | + |
| 172 | + if (evaluationSchemaPathIterator.hasNext()) { |
| 173 | + Object evaluationPath = evaluationSchemaPathIterator.next(); |
| 174 | + current.removeLast(); |
| 175 | + if ("properties".equals(evaluationPath) || "items".equals(evaluationPath)) { |
| 176 | + // If there is a change in instance location then return false |
| 177 | + return false; |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + return false; |
| 182 | + } |
125 | 183 | } |
0 commit comments