Search in sources :

Example 1 with JSONSchemaType

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);
            }
        }
    }
}
Also used : JsonArray(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) JsonObject(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) JSONValidationInfo(org.eclipse.wst.json.core.internal.validation.JSONValidationInfo) JSONSchemaType(org.eclipse.json.schema.JSONSchemaType)

Example 2 with JSONSchemaType

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);
}
Also used : JsonArray(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray) ArrayList(java.util.ArrayList) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) JSONSchemaType(org.eclipse.json.schema.JSONSchemaType)

Aggregations

JsonArray (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray)2 JsonValue (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue)2 JSONSchemaType (org.eclipse.json.schema.JSONSchemaType)2 ArrayList (java.util.ArrayList)1 JsonObject (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)1 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)1 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)1 JSONValidationInfo (org.eclipse.wst.json.core.internal.validation.JSONValidationInfo)1