|  | 
| 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,146 @@ 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 property type. | 
|  | 230 | +     * If no matching attribute is found, returns null. | 
|  | 231 | +     * | 
|  | 232 | +     * @param name Name of property to look for | 
|  | 233 | +     * | 
|  | 234 | +     * @return a JSON Schema property type as text | 
|  | 235 | +     */ | 
|  | 236 | + | 
|  | 237 | +    public String getPropertyType(final String name) { | 
|  | 238 | +        return getPropertyElementAsText(name, "type"); | 
|  | 239 | +    } | 
|  | 240 | + | 
|  | 241 | +    /** | 
|  | 242 | +     * Method to retrieve a JSON Schema property description. | 
|  | 243 | +     * If no matching attribute is found, returns null. | 
|  | 244 | +     * | 
|  | 245 | +     * @param name Name of property to look for | 
|  | 246 | +     * | 
|  | 247 | +     * @return a JSON Schema property description as text | 
|  | 248 | +     */ | 
|  | 249 | + | 
|  | 250 | +    public String getPropertyDescription(final String name) { | 
|  | 251 | +        return getPropertyElementAsText(name, "description"); | 
|  | 252 | +    } | 
|  | 253 | + | 
|  | 254 | +    /** | 
|  | 255 | +     * Method for checking if a JSON Schema attribute with specified name is required. | 
|  | 256 | +     * If no matching attribute is found, returns null. | 
|  | 257 | +     * | 
|  | 258 | +     * @param name Name of attribute to look for | 
|  | 259 | +     * | 
|  | 260 | +     * @return true if it is required, false if not | 
|  | 261 | +     */ | 
|  | 262 | +    public boolean isRequired(final String name) { | 
|  | 263 | +        final JsonNode requiredNode = schema.getNode().findValue("required"); | 
|  | 264 | +        if (requiredNode != null) { | 
|  | 265 | +            final Iterator<JsonNode> it = requiredNode.elements(); | 
|  | 266 | +            while (it.hasNext()) { | 
|  | 267 | +                if (name.equals(it.next().asText())) { | 
|  | 268 | +                    return true; | 
|  | 269 | +                } | 
|  | 270 | +            } | 
|  | 271 | +        } | 
|  | 272 | +        return false; | 
|  | 273 | +    } | 
|  | 274 | + | 
|  | 275 | +    /* | 
|  | 276 | +    /********************************************************** | 
|  | 277 | +    /* Internal methods | 
|  | 278 | +    /********************************************************** | 
|  | 279 | +     */ | 
|  | 280 | + | 
|  | 281 | +    /** | 
|  | 282 | +     * Method to retrieve all JSON Schema attributes. | 
|  | 283 | +     * | 
|  | 284 | +     * @return Node of the attributes | 
|  | 285 | +     */ | 
|  | 286 | +    private JsonNode getProperties() { | 
|  | 287 | +        return schema.getNode().findValue("properties"); | 
|  | 288 | +    } | 
|  | 289 | + | 
|  | 290 | +    /** | 
|  | 291 | +     * Method to finding a JSON Schema attribute with specified name and returning the node. | 
|  | 292 | +     * If no matching attribute is found, returns null. | 
|  | 293 | +     * | 
|  | 294 | +     * @param name Name of attribute to look for | 
|  | 295 | +     * | 
|  | 296 | +     * @return Node of the attribute, if any; null if none | 
|  | 297 | +     */ | 
|  | 298 | +    private JsonNode getProperty(final String name) { | 
|  | 299 | +        return getProperties().get(name); | 
|  | 300 | +    } | 
|  | 301 | + | 
|  | 302 | +    /** | 
|  | 303 | +     * Method to retrieve a JSON Schema property element as text. | 
|  | 304 | +     * If no matching attribute is found, returns null. | 
|  | 305 | +     * | 
|  | 306 | +     * @param name Name of property to look for | 
|  | 307 | +     * @param element Name of the element of the property | 
|  | 308 | +     * | 
|  | 309 | +     * @return a JSON Schema property element as text; null if it is not exist | 
|  | 310 | +     */ | 
|  | 311 | + | 
|  | 312 | +    private String getPropertyElementAsText(final String name, String element) { | 
|  | 313 | +        final JsonNode node = getProperty(name); | 
|  | 314 | +        if (node == null) { | 
|  | 315 | +            return null; | 
|  | 316 | +        } | 
|  | 317 | +        final JsonNode nodeElement = node.get(element); | 
|  | 318 | +        if (nodeElement == null) { | 
|  | 319 | +            return null; | 
|  | 320 | +        } | 
|  | 321 | +        return nodeElement.asText(); | 
|  | 322 | +    } | 
|  | 323 | + | 
|  | 324 | +    /** | 
|  | 325 | +     * Method to retrieve a JsonNode elements as text. | 
|  | 326 | +     * | 
|  | 327 | +     * @param node Node to look for | 
|  | 328 | +     * | 
|  | 329 | +     * @return List of the elements of the node | 
|  | 330 | +     */ | 
|  | 331 | + | 
|  | 332 | +    private List<String> getElementsAsText(final JsonNode node) { | 
|  | 333 | +        if (node == null) { | 
|  | 334 | +            return Collections.emptyList(); | 
|  | 335 | +        } | 
|  | 336 | +        final List nodeNames = new ArrayList<String>(); | 
|  | 337 | +        final Iterator<JsonNode> it = node.elements(); | 
|  | 338 | +        while (it.hasNext()) { | 
|  | 339 | +            nodeNames.add(it.next().asText()); | 
|  | 340 | +        } | 
|  | 341 | +        return nodeNames; | 
|  | 342 | +    } | 
| 199 | 343 | } | 
0 commit comments