Skip to content

Commit 9157116

Browse files
author
David Belmez
committed
Adds schema properties getter methods to JsonSchema
1 parent fd781ef commit 9157116

File tree

2 files changed

+149
-5
lines changed

2 files changed

+149
-5
lines changed

Diff for: build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ apply(plugin: "idea");
4444
apply(plugin: "eclipse");
4545

4646
group = "com.github.fge";
47-
version = "2.2.6";
47+
version = "2.2.7";
4848
sourceCompatibility = "1.6";
4949
targetCompatibility = "1.6"; // defaults to sourceCompatibility
5050

Diff for: src/main/java/com/github/fge/jsonschema/main/JsonSchema.java

+148-4
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@
2222
import com.fasterxml.jackson.databind.JsonNode;
2323
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
2424
import com.github.fge.jsonschema.core.processing.ProcessingResult;
25-
import com.github.fge.jsonschema.core.processing.Processor;
2625
import com.github.fge.jsonschema.core.report.ListProcessingReport;
27-
import com.github.fge.jsonschema.core.report.MessageProvider;
2826
import com.github.fge.jsonschema.core.report.ProcessingReport;
2927
import com.github.fge.jsonschema.core.report.ReportProvider;
3028
import com.github.fge.jsonschema.core.tree.SchemaTree;
3129
import com.github.fge.jsonschema.core.tree.SimpleJsonTree;
3230
import com.github.fge.jsonschema.processors.data.FullData;
3331
import com.github.fge.jsonschema.processors.validation.ValidationProcessor;
3432

33+
import java.util.ArrayList;
34+
import java.util.Collections;
35+
import java.util.Iterator;
36+
import java.util.List;
3537
import javax.annotation.concurrent.Immutable;
3638

3739
/**
@@ -142,8 +144,8 @@ public ProcessingReport validate(final JsonNode instance)
142144
* thrown during processing)
143145
*
144146
*
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)
147149
* @see JsonValidator#validate(JsonNode, JsonNode, boolean)
148150
*
149151
* @since 2.1.8
@@ -196,4 +198,146 @@ public boolean validInstanceUnchecked(final JsonNode instance)
196198
{
197199
return doValidateUnchecked(instance, false).isSuccess();
198200
}
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+
}
199343
}

0 commit comments

Comments
 (0)