Skip to content

Commit 160c323

Browse files
author
David Belmez
committed
[schema-info]: Adds schema properties getter methods to JsonSchema
1 parent 728b3da commit 160c323

File tree

2 files changed

+178
-4
lines changed

2 files changed

+178
-4
lines changed

Diff for: src/main/java/com/github/fge/jsonschema/main/JsonSchema.java

+52
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import com.github.fge.jsonschema.core.report.ListProcessingReport;
2727
import com.github.fge.jsonschema.core.report.MessageProvider;
2828
import com.github.fge.jsonschema.core.report.ProcessingReport;
29+
import java.util.Iterator;
30+
import java.util.List;
2931

3032
/**
3133
* Single-schema instance validator
@@ -122,4 +124,54 @@ boolean validInstance(JsonNode instance)
122124
* @return true if the instance is valid
123125
*/
124126
boolean validInstanceUnchecked(JsonNode instance);
127+
128+
/**
129+
* Method to retrieve all JSON Schema property names.
130+
*
131+
* @return An iterator with all property names
132+
*/
133+
Iterator<String> getPropertyNames();
134+
135+
/**
136+
* Method to retrieve a JSON Schema attribute enum values.
137+
* If no matching attribute is found, returns null.
138+
*
139+
* @param name Name of attribute to look for
140+
*
141+
* @return List of the enum values of the attribute, if is enum type; empty if it is not
142+
*/
143+
144+
List<String> getPropertyEnum(final String name);
145+
146+
/**
147+
* Method to retrieve a JSON Schema property type.
148+
* If no matching attribute is found, returns null.
149+
*
150+
* @param name Name of property to look for
151+
*
152+
* @return a JSON Schema property type as text
153+
*/
154+
155+
String getPropertyType(final String name);
156+
157+
/**
158+
* Method to retrieve a JSON Schema property description.
159+
* If no matching attribute is found, returns null.
160+
*
161+
* @param name Name of property to look for
162+
*
163+
* @return a JSON Schema property description as text
164+
*/
165+
166+
String getPropertyDescription(final String name);
167+
168+
/**
169+
* Method for checking if a JSON Schema attribute with specified name is required.
170+
* If no matching attribute is found, returns null.
171+
*
172+
* @param name Name of attribute to look for
173+
*
174+
* @return true if it is required, false if not
175+
*/
176+
boolean isRequired(final String name);
125177
}

Diff for: src/main/java/com/github/fge/jsonschema/main/JsonSchemaImpl.java

+126-4
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
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;
26-
import com.github.fge.jsonschema.core.report.ListProcessingReport;
27-
import com.github.fge.jsonschema.core.report.MessageProvider;
2825
import com.github.fge.jsonschema.core.report.ProcessingReport;
2926
import com.github.fge.jsonschema.core.report.ReportProvider;
3027
import com.github.fge.jsonschema.core.tree.SchemaTree;
3128
import com.github.fge.jsonschema.core.tree.SimpleJsonTree;
3229
import com.github.fge.jsonschema.processors.data.FullData;
3330
import com.github.fge.jsonschema.processors.validation.ValidationProcessor;
34-
31+
import java.util.ArrayList;
32+
import java.util.Collections;
33+
import java.util.Iterator;
34+
import java.util.List;
3535
import javax.annotation.concurrent.Immutable;
3636

3737
/**
@@ -147,4 +147,126 @@ public boolean validInstanceUnchecked(final JsonNode instance)
147147
{
148148
return doValidateUnchecked(instance, false).isSuccess();
149149
}
150+
151+
/**
152+
* {@inheritDoc}
153+
*/
154+
@Override
155+
public Iterator<String> getPropertyNames() {
156+
return getProperties().fieldNames();
157+
}
158+
159+
/**
160+
* {@inheritDoc}
161+
*/
162+
@Override
163+
public List<String> getPropertyEnum(final String name) {
164+
final JsonNode node = getProperty(name);
165+
if (node != null) {
166+
return getElementsAsText(node.get("enum"));
167+
}
168+
return Collections.emptyList();
169+
}
170+
171+
/**
172+
* {@inheritDoc}
173+
*/
174+
@Override
175+
public String getPropertyType(final String name) {
176+
return getPropertyElementAsText(name, "type");
177+
}
178+
179+
/**
180+
* {@inheritDoc}
181+
*/
182+
@Override
183+
public String getPropertyDescription(final String name) {
184+
return getPropertyElementAsText(name, "description");
185+
}
186+
187+
/**
188+
* {@inheritDoc}
189+
*/
190+
@Override
191+
public boolean isRequired(final String name) {
192+
final JsonNode requiredNode = schema.getNode().findValue("required");
193+
if (requiredNode != null) {
194+
final Iterator<JsonNode> it = requiredNode.elements();
195+
while (it.hasNext()) {
196+
if (name.equals(it.next().asText())) {
197+
return true;
198+
}
199+
}
200+
}
201+
return false;
202+
}
203+
204+
/*
205+
/**********************************************************
206+
/* Internal methods
207+
/**********************************************************
208+
*/
209+
210+
/**
211+
* Method to retrieve all JSON Schema attributes.
212+
*
213+
* @return Node of the attributes
214+
*/
215+
private JsonNode getProperties() {
216+
return schema.getNode().findValue("properties");
217+
}
218+
219+
/**
220+
* Method to finding a JSON Schema attribute with specified name and returning the node.
221+
* If no matching attribute is found, returns null.
222+
*
223+
* @param name Name of attribute to look for
224+
*
225+
* @return Node of the attribute, if any; null if none
226+
*/
227+
private JsonNode getProperty(final String name) {
228+
return getProperties().get(name);
229+
}
230+
231+
/**
232+
* Method to retrieve a JSON Schema property element as text.
233+
* If no matching attribute is found, returns null.
234+
*
235+
* @param name Name of property to look for
236+
* @param element Name of the element of the property
237+
*
238+
* @return a JSON Schema property element as text; null if it is not exist
239+
*/
240+
241+
private String getPropertyElementAsText(final String name, final String element) {
242+
final JsonNode node = getProperty(name);
243+
if (node == null) {
244+
return null;
245+
}
246+
final JsonNode nodeElement = node.get(element);
247+
if (nodeElement == null) {
248+
return null;
249+
}
250+
return nodeElement.asText();
251+
}
252+
253+
/**
254+
* Method to retrieve a JsonNode elements as text.
255+
*
256+
* @param node Node to look for
257+
*
258+
* @return List of the elements of the node
259+
*/
260+
261+
private List<String> getElementsAsText(final JsonNode node) {
262+
if (node == null) {
263+
return Collections.emptyList();
264+
}
265+
final List<String> nodeNames = new ArrayList<String>();
266+
final Iterator<JsonNode> it = node.elements();
267+
while (it.hasNext()) {
268+
nodeNames.add(it.next().asText());
269+
}
270+
return nodeNames;
271+
}
150272
}

0 commit comments

Comments
 (0)