Skip to content

Commit 8525531

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

File tree

2 files changed

+37
-5
lines changed

2 files changed

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

+36-4
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,15 @@
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.Iterator;
3534
import javax.annotation.concurrent.Immutable;
3635

3736
/**
@@ -142,8 +141,8 @@ public ProcessingReport validate(final JsonNode instance)
142141
* thrown during processing)
143142
*
144143
*
145-
* @see ProcessingResult#uncheckedResult(Processor, ProcessingReport,
146-
* MessageProvider)
144+
* @see ProcessingResult#uncheckedResult(com.github.fge.jsonschema.core.processing.Processor, ProcessingReport,
145+
* com.github.fge.jsonschema.core.report.MessageProvider)
147146
* @see JsonValidator#validate(JsonNode, JsonNode, boolean)
148147
*
149148
* @since 2.1.8
@@ -196,4 +195,37 @@ public boolean validInstanceUnchecked(final JsonNode instance)
196195
{
197196
return doValidateUnchecked(instance, false).isSuccess();
198197
}
198+
199+
/**
200+
* Method to finding a JSON Schema attribute with specified name and returning the node.
201+
* If no matching attribute is found, returns null.
202+
*
203+
* @param name Name of attribute to look for
204+
*
205+
* @return Node of the attribute, if any; null if none
206+
*/
207+
public JsonNode getProperty(final String name) {
208+
return schema.getNode().findValue("properties").get(name);
209+
}
210+
211+
/**
212+
* Method for checking if a JSON Schema attribute with specified name is required.
213+
* If no matching attribute is found, returns null.
214+
*
215+
* @param name Name of attribute to look for
216+
*
217+
* @return true if it is required, false if not
218+
*/
219+
public boolean isRequired(final String name) {
220+
final JsonNode requiredNode = schema.getNode().findValue("required");
221+
if (requiredNode != null) {
222+
final Iterator<JsonNode> requiredElements = requiredNode.elements();
223+
while (requiredElements.hasNext()) {
224+
if (name.equals(requiredElements.next().asText())) {
225+
return true;
226+
}
227+
}
228+
}
229+
return false;
230+
}
199231
}

0 commit comments

Comments
 (0)