Skip to content

Commit 5037720

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

File tree

2 files changed

+118
-5
lines changed

2 files changed

+118
-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

+117-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,115 @@ 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 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+
}
199312
}

0 commit comments

Comments
 (0)