Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds schema properties getter methods to JsonSchema #184

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/main/java/com/github/fge/jsonschema/main/JsonSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
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 java.util.Iterator;
import java.util.List;

/**
* Single-schema instance validator
Expand Down Expand Up @@ -122,4 +124,54 @@ boolean validInstance(JsonNode instance)
* @return true if the instance is valid
*/
boolean validInstanceUnchecked(JsonNode instance);

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

/**
* 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
*/

List<String> getPropertyEnum(final String name);

/**
* Method to retrieve a JSON Schema property type.
* If no matching attribute is found, returns null.
*
* @param name Name of property to look for
*
* @return a JSON Schema property type as text
*/

String getPropertyType(final String name);

/**
* Method to retrieve a JSON Schema property description.
* If no matching attribute is found, returns null.
*
* @param name Name of property to look for
*
* @return a JSON Schema property description as text
*/

String getPropertyDescription(final String name);

/**
* 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
*/
boolean isRequired(final String name);
}
130 changes: 126 additions & 4 deletions src/main/java/com/github/fge/jsonschema/main/JsonSchemaImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
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 @@ -147,4 +147,126 @@ public boolean validInstanceUnchecked(final JsonNode instance)
{
return doValidateUnchecked(instance, false).isSuccess();
}

/**
* {@inheritDoc}
*/
@Override
public Iterator<String> getPropertyNames() {
return getProperties().fieldNames();
}

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

/**
* {@inheritDoc}
*/
@Override
public String getPropertyType(final String name) {
return getPropertyElementAsText(name, "type");
}

/**
* {@inheritDoc}
*/
@Override
public String getPropertyDescription(final String name) {
return getPropertyElementAsText(name, "description");
}

/**
* {@inheritDoc}
*/
@Override
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 JSON Schema property element as text.
* If no matching attribute is found, returns null.
*
* @param name Name of property to look for
* @param element Name of the element of the property
*
* @return a JSON Schema property element as text; null if it is not exist
*/

private String getPropertyElementAsText(final String name, final String element) {
final JsonNode node = getProperty(name);
if (node == null) {
return null;
}
final JsonNode nodeElement = node.get(element);
if (nodeElement == null) {
return null;
}
return nodeElement.asText();
}

/**
* 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<String> nodeNames = new ArrayList<String>();
final Iterator<JsonNode> it = node.elements();
while (it.hasNext()) {
nodeNames.add(it.next().asText());
}
return nodeNames;
}
}