use of org.eclipse.json.schema.JSONSchemaType in project webtools.sourceediting by eclipse.
the class Validator method validate.
private void validate(IJSONNode node, JsonObject schema, Member member, JSONValidationInfo valinfo) {
if (IJSONSchemaNode.ALL_OF.equals(member.getName()) && member.getValue() instanceof JsonArray) {
JsonArray jsonArray = (JsonArray) member.getValue();
Iterator<JsonValue> iter = jsonArray.iterator();
while (iter.hasNext()) {
JsonValue value = iter.next();
if (value instanceof JsonObject) {
validate(node, (JsonObject) value, valinfo);
}
}
}
if (IJSONSchemaNode.ANY_OF.equals(member.getName()) && member.getValue() instanceof JsonArray) {
JsonArray jsonArray = (JsonArray) member.getValue();
Iterator<JsonValue> iter = jsonArray.iterator();
while (iter.hasNext()) {
JsonValue value = iter.next();
if (value instanceof JsonObject) {
JSONValidationInfo info = new JSONValidationInfo("");
validate(node, (JsonObject) value, info);
if (info.getValidationMessages() == null || info.getValidationMessages().length == 0) {
return;
}
}
}
int offset = node.getStartOffset();
int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
valinfo.addMessage("Matches a schema that is not allowed", line, 0, offset == 0 ? 1 : offset);
}
if (IJSONSchemaNode.ONE_OF.equals(member.getName()) && member.getValue() instanceof JsonArray) {
JsonArray jsonArray = (JsonArray) member.getValue();
Iterator<JsonValue> iter = jsonArray.iterator();
int count = 0;
while (iter.hasNext()) {
JsonValue value = iter.next();
if (value instanceof JsonObject) {
JSONValidationInfo info = new JSONValidationInfo("");
validate(node, (JsonObject) value, info);
if (info.getValidationMessages() == null || info.getValidationMessages().length == 0) {
count = count + 1;
}
}
}
if (count != 1) {
int offset = node.getStartOffset();
int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
valinfo.addMessage("Matches a schema that is not allowed", line, 0, offset == 0 ? 1 : offset);
}
}
if (IJSONSchemaNode.NOT.equals(member.getName()) && member.getValue() instanceof JsonObject) {
JsonObject json = (JsonObject) member.getValue();
JSONValidationInfo info = new JSONValidationInfo("");
validate(node, json, info);
if (info.getValidationMessages() == null || info.getValidationMessages().length == 0) {
int offset = node.getStartOffset();
int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
valinfo.addMessage("Matches a schema that is not allowed", line, 0, offset == 0 ? 1 : offset);
}
}
if (IJSONSchemaNode.TYPE.equals(member.getName())) {
validateType(node, member, valinfo);
}
if (IJSONSchemaNode.ENUM.equals(member.getName())) {
validateEnum(node, schema, valinfo);
}
if (node.getNodeType() == IJSONNode.OBJECT_NODE) {
if (IJSONSchemaNode.REQUIRED.equals(member.getName())) {
validateRequired(node, schema, valinfo);
}
if (IJSONSchemaNode.MAX_PROPERTIES.equals(member.getName())) {
validateMaxProperties(node, schema, valinfo);
}
if (IJSONSchemaNode.MIN_PROPERTIES.equals(member.getName())) {
validateMinProperties(node, schema, valinfo);
}
if (IJSONSchemaNode.ADDITIONAL_PROPERTIES.equals(member.getName())) {
validateAdditionalProperties(node, schema, member.getValue(), valinfo);
}
}
if (node.getNodeType() == IJSONNode.PAIR_NODE) {
IJSONValue value = ((IJSONPair) node).getValue();
JSONSchemaType[] types = JSONSchemaNode.getType(schema.get(IJSONSchemaNode.TYPE));
if (value != null) {
if (value.getNodeType() == IJSONNode.VALUE_STRING_NODE && isType(types, JSONSchemaType.String)) {
validateString(node, schema, member, valinfo, value);
}
if (value.getNodeType() == IJSONNode.VALUE_NUMBER_NODE && (isType(types, JSONSchemaType.Integer) || isType(types, JSONSchemaType.Number))) {
validateNumber(node, schema, member, valinfo, value);
}
if (value.getNodeType() == IJSONNode.ARRAY_NODE && isType(types, JSONSchemaType.Array)) {
validateArray(node, schema, member, valinfo, value);
}
}
}
}
use of org.eclipse.json.schema.JSONSchemaType in project webtools.sourceediting by eclipse.
the class JSONSchemaNode method getType.
public static JSONSchemaType[] getType(JsonValue value) {
if (value == null) {
return JSONSchemaType.EMPTY_TYPES;
}
JSONSchemaType t = null;
List<JSONSchemaType> types = new ArrayList<JSONSchemaType>();
if (value.isString()) {
t = JSONSchemaType.getType(value.asString());
if (t != null) {
types.add(t);
}
} else if (value.isArray()) {
JsonArray array = (JsonArray) value;
for (JsonValue item : array) {
t = JSONSchemaType.getType(item.asString());
if (t != null) {
types.add(t);
}
}
}
return types.toArray(JSONSchemaType.EMPTY_TYPES);
}
Aggregations