Skip to content

Commit

Permalink
Adds schema properties getter methods to JsonSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
David Belmez committed Jun 6, 2016
1 parent fd781ef commit 5037720
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ apply(plugin: "idea");
apply(plugin: "eclipse");

group = "com.github.fge";
version = "2.2.6";
version = "2.2.7";
sourceCompatibility = "1.6";
targetCompatibility = "1.6"; // defaults to sourceCompatibility

Expand Down
121 changes: 117 additions & 4 deletions src/main/java/com/github/fge/jsonschema/main/JsonSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.processing.ProcessingResult;
import com.github.fge.jsonschema.core.processing.Processor;
import com.github.fge.jsonschema.core.report.ListProcessingReport;
import com.github.fge.jsonschema.core.report.MessageProvider;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.core.report.ReportProvider;
import com.github.fge.jsonschema.core.tree.SchemaTree;
import com.github.fge.jsonschema.core.tree.SimpleJsonTree;
import com.github.fge.jsonschema.processors.data.FullData;
import com.github.fge.jsonschema.processors.validation.ValidationProcessor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.annotation.concurrent.Immutable;

/**
Expand Down Expand Up @@ -142,8 +144,8 @@ public ProcessingReport validate(final JsonNode instance)
* thrown during processing)
*
*
* @see ProcessingResult#uncheckedResult(Processor, ProcessingReport,
* MessageProvider)
* @see ProcessingResult#uncheckedResult(com.github.fge.jsonschema.core.processing.Processor, ProcessingReport,
* com.github.fge.jsonschema.core.report.MessageProvider)
* @see JsonValidator#validate(JsonNode, JsonNode, boolean)
*
* @since 2.1.8
Expand Down Expand Up @@ -196,4 +198,115 @@ public boolean validInstanceUnchecked(final JsonNode instance)
{
return doValidateUnchecked(instance, false).isSuccess();
}

/**
* Method to retrieve all JSON Schema property names.
*
* @return An iterator with all property names
*/
public Iterator<String> getPropertyNames() {
return getProperties().fieldNames();
}

/**
* Method to retrieve a JSON Schema attribute enum values.
* If no matching attribute is found, returns null.
*
* @param name Name of attribute to look for
*
* @return List of the enum values of the attribute, if is enum type; empty if it is not
*/

public List<String> getPropertyEnum(final String name) {
final JsonNode node = getProperty(name);
if (node != null) {
return getElementsAsText(node.get("enum"));
}
return Collections.emptyList();
}

/**
* Method to retrieve a JSON Schema attribute enum values.
* If no matching attribute is found, returns null.
*
* @param name Name of attribute to look for
*
* @return List of the enum values of the attribute, if is enum type; empty if it is not
*/

public String getPropertyType(final String name) {
final JsonNode node = getProperty(name);
if (node == null) {
return null;
}
return node.get("type").asText();
}

/**
* Method for checking if a JSON Schema attribute with specified name is required.
* If no matching attribute is found, returns null.
*
* @param name Name of attribute to look for
*
* @return true if it is required, false if not
*/
public boolean isRequired(final String name) {
final JsonNode requiredNode = schema.getNode().findValue("required");
if (requiredNode != null) {
final Iterator<JsonNode> it = requiredNode.elements();
while (it.hasNext()) {
if (name.equals(it.next().asText())) {
return true;
}
}
}
return false;
}

/*
/**********************************************************
/* Internal methods
/**********************************************************
*/

/**
* Method to retrieve all JSON Schema attributes.
*
* @return Node of the attributes
*/
private JsonNode getProperties() {
return schema.getNode().findValue("properties");
}

/**
* Method to finding a JSON Schema attribute with specified name and returning the node.
* If no matching attribute is found, returns null.
*
* @param name Name of attribute to look for
*
* @return Node of the attribute, if any; null if none
*/
private JsonNode getProperty(final String name) {
return getProperties().get(name);
}

/**
* Method to retrieve a JsonNode elements as text.
*
* @param node Node to look for
*
* @return List of the elements of the node
*/

private List<String> getElementsAsText(final JsonNode node) {
if (node == null) {
return Collections.emptyList();
}
final List nodeNames = new ArrayList<String>();
final Iterator<JsonNode> it = node.elements();
while (it.hasNext()) {
nodeNames.add(it.next().asText());
}
return nodeNames;
}
}

0 comments on commit 5037720

Please sign in to comment.