|
22 | 22 | import com.fasterxml.jackson.databind.JsonNode;
|
23 | 23 | import com.github.fge.jsonschema.core.exceptions.ProcessingException;
|
24 | 24 | import com.github.fge.jsonschema.core.processing.ProcessingResult;
|
25 |
| -import com.github.fge.jsonschema.core.processing.Processor; |
26 | 25 | import com.github.fge.jsonschema.core.report.ListProcessingReport;
|
27 |
| -import com.github.fge.jsonschema.core.report.MessageProvider; |
28 | 26 | import com.github.fge.jsonschema.core.report.ProcessingReport;
|
29 | 27 | import com.github.fge.jsonschema.core.report.ReportProvider;
|
30 | 28 | import com.github.fge.jsonschema.core.tree.SchemaTree;
|
31 | 29 | import com.github.fge.jsonschema.core.tree.SimpleJsonTree;
|
32 | 30 | import com.github.fge.jsonschema.processors.data.FullData;
|
33 | 31 | import com.github.fge.jsonschema.processors.validation.ValidationProcessor;
|
34 | 32 |
|
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.Collections; |
| 35 | +import java.util.Iterator; |
| 36 | +import java.util.List; |
35 | 37 | import javax.annotation.concurrent.Immutable;
|
36 | 38 |
|
37 | 39 | /**
|
@@ -142,8 +144,8 @@ public ProcessingReport validate(final JsonNode instance)
|
142 | 144 | * thrown during processing)
|
143 | 145 | *
|
144 | 146 | *
|
145 |
| - * @see ProcessingResult#uncheckedResult(Processor, ProcessingReport, |
146 |
| - * MessageProvider) |
| 147 | + * @see ProcessingResult#uncheckedResult(com.github.fge.jsonschema.core.processing.Processor, ProcessingReport, |
| 148 | + * com.github.fge.jsonschema.core.report.MessageProvider) |
147 | 149 | * @see JsonValidator#validate(JsonNode, JsonNode, boolean)
|
148 | 150 | *
|
149 | 151 | * @since 2.1.8
|
@@ -196,4 +198,115 @@ public boolean validInstanceUnchecked(final JsonNode instance)
|
196 | 198 | {
|
197 | 199 | return doValidateUnchecked(instance, false).isSuccess();
|
198 | 200 | }
|
| 201 | + |
| 202 | + /** |
| 203 | + * Method to retrieve all JSON Schema property names. |
| 204 | + * |
| 205 | + * @return An iterator with all property names |
| 206 | + */ |
| 207 | + public Iterator<String> getPropertyNames() { |
| 208 | + return getProperties().fieldNames(); |
| 209 | + } |
| 210 | + |
| 211 | + /** |
| 212 | + * Method to retrieve a JSON Schema attribute enum values. |
| 213 | + * If no matching attribute is found, returns null. |
| 214 | + * |
| 215 | + * @param name Name of attribute to look for |
| 216 | + * |
| 217 | + * @return List of the enum values of the attribute, if is enum type; empty if it is not |
| 218 | + */ |
| 219 | + |
| 220 | + public List<String> getPropertyEnum(final String name) { |
| 221 | + final JsonNode node = getProperty(name); |
| 222 | + if (node != null) { |
| 223 | + return getElementsAsText(node.get("enum")); |
| 224 | + } |
| 225 | + return Collections.emptyList(); |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Method to retrieve a JSON Schema attribute enum values. |
| 230 | + * If no matching attribute is found, returns null. |
| 231 | + * |
| 232 | + * @param name Name of attribute to look for |
| 233 | + * |
| 234 | + * @return List of the enum values of the attribute, if is enum type; empty if it is not |
| 235 | + */ |
| 236 | + |
| 237 | + public String getPropertyType(final String name) { |
| 238 | + final JsonNode node = getProperty(name); |
| 239 | + if (node == null) { |
| 240 | + return null; |
| 241 | + } |
| 242 | + return node.get("type").asText(); |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * Method for checking if a JSON Schema attribute with specified name is required. |
| 247 | + * If no matching attribute is found, returns null. |
| 248 | + * |
| 249 | + * @param name Name of attribute to look for |
| 250 | + * |
| 251 | + * @return true if it is required, false if not |
| 252 | + */ |
| 253 | + public boolean isRequired(final String name) { |
| 254 | + final JsonNode requiredNode = schema.getNode().findValue("required"); |
| 255 | + if (requiredNode != null) { |
| 256 | + final Iterator<JsonNode> it = requiredNode.elements(); |
| 257 | + while (it.hasNext()) { |
| 258 | + if (name.equals(it.next().asText())) { |
| 259 | + return true; |
| 260 | + } |
| 261 | + } |
| 262 | + } |
| 263 | + return false; |
| 264 | + } |
| 265 | + |
| 266 | + /* |
| 267 | + /********************************************************** |
| 268 | + /* Internal methods |
| 269 | + /********************************************************** |
| 270 | + */ |
| 271 | + |
| 272 | + /** |
| 273 | + * Method to retrieve all JSON Schema attributes. |
| 274 | + * |
| 275 | + * @return Node of the attributes |
| 276 | + */ |
| 277 | + private JsonNode getProperties() { |
| 278 | + return schema.getNode().findValue("properties"); |
| 279 | + } |
| 280 | + |
| 281 | + /** |
| 282 | + * Method to finding a JSON Schema attribute with specified name and returning the node. |
| 283 | + * If no matching attribute is found, returns null. |
| 284 | + * |
| 285 | + * @param name Name of attribute to look for |
| 286 | + * |
| 287 | + * @return Node of the attribute, if any; null if none |
| 288 | + */ |
| 289 | + private JsonNode getProperty(final String name) { |
| 290 | + return getProperties().get(name); |
| 291 | + } |
| 292 | + |
| 293 | + /** |
| 294 | + * Method to retrieve a JsonNode elements as text. |
| 295 | + * |
| 296 | + * @param node Node to look for |
| 297 | + * |
| 298 | + * @return List of the elements of the node |
| 299 | + */ |
| 300 | + |
| 301 | + private List<String> getElementsAsText(final JsonNode node) { |
| 302 | + if (node == null) { |
| 303 | + return Collections.emptyList(); |
| 304 | + } |
| 305 | + final List nodeNames = new ArrayList<String>(); |
| 306 | + final Iterator<JsonNode> it = node.elements(); |
| 307 | + while (it.hasNext()) { |
| 308 | + nodeNames.add(it.next().asText()); |
| 309 | + } |
| 310 | + return nodeNames; |
| 311 | + } |
199 | 312 | }
|
0 commit comments