Skip to content

Commit aa8080e

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

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
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

src/main/java/com/github/fge/jsonschema/main/JsonSchema.java

Lines changed: 45 additions & 4 deletions
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,46 @@ public boolean validInstanceUnchecked(final JsonNode instance)
196195
{
197196
return doValidateUnchecked(instance, false).isSuccess();
198197
}
198+
199+
/**
200+
* Method to retrieve all JSON Schema attributes.
201+
*
202+
* @return Node of the attributes
203+
*/
204+
public JsonNode getProperties() {
205+
return schema.getNode().findValue("properties");
206+
}
207+
208+
/**
209+
* Method to finding a JSON Schema attribute with specified name and returning the node.
210+
* If no matching attribute is found, returns null.
211+
*
212+
* @param name Name of attribute to look for
213+
*
214+
* @return Node of the attribute, if any; null if none
215+
*/
216+
public JsonNode getProperty(final String name) {
217+
return getProperties().get(name);
218+
}
219+
220+
/**
221+
* Method for checking if a JSON Schema attribute with specified name is required.
222+
* If no matching attribute is found, returns null.
223+
*
224+
* @param name Name of attribute to look for
225+
*
226+
* @return true if it is required, false if not
227+
*/
228+
public boolean isRequired(final String name) {
229+
final JsonNode requiredNode = schema.getNode().findValue("required");
230+
if (requiredNode != null) {
231+
final Iterator<JsonNode> requiredElements = requiredNode.elements();
232+
while (requiredElements.hasNext()) {
233+
if (name.equals(requiredElements.next().asText())) {
234+
return true;
235+
}
236+
}
237+
}
238+
return false;
239+
}
199240
}

0 commit comments

Comments
 (0)