Search in sources :

Example 1 with JSONValidationInfo

use of org.eclipse.wst.json.core.internal.validation.JSONValidationInfo 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 JSONValidationInfo

use of org.eclipse.wst.json.core.internal.validation.JSONValidationInfo in project webtools.sourceediting by eclipse.

the class Validator method validate.

@Override
public ValidationReport validate(String uri, InputStream inputstream, NestedValidatorContext context, ValidationResult result) {
    JSONValidator validator = JSONValidator.getInstance();
    JSONValidationConfiguration configuration = new JSONValidationConfiguration();
    JSONValidationReport valreport = validator.validate(uri, inputstream, configuration, result, context);
    String prefs = JSONCorePlugin.getDefault().getBundle().getSymbolicName();
    IEclipsePreferences modelPreferences = InstanceScope.INSTANCE.getNode(prefs);
    boolean validateSchema = modelPreferences.getBoolean(JSONCorePreferenceNames.SCHEMA_VALIDATION, false);
    if (validateSchema) {
        IJSONModel model = null;
        try {
            IStructuredModel temp = getModel(uri);
            if (!(temp instanceof IJSONModel)) {
                return valreport;
            }
            model = (IJSONModel) temp;
            IJSONSchemaDocument schemaDocument = SchemaProcessorRegistryReader.getInstance().getSchemaDocument(model);
            if (schemaDocument != null) {
                JSONValidationInfo valinfo = null;
                if (valreport instanceof JSONValidationInfo) {
                    valinfo = (JSONValidationInfo) valreport;
                } else {
                    valinfo = new JSONValidationInfo(uri);
                }
                validate(model, schemaDocument, valinfo);
                // valreport.getValidationMessages();
                return valreport;
            }
        } catch (IOException e) {
            logWarning(e);
            return valreport;
        } finally {
            if (model != null) {
                model.releaseFromRead();
            }
        }
    }
    return valreport;
}
Also used : JSONValidationReport(org.eclipse.wst.json.core.internal.validation.JSONValidationReport) JSONValidationConfiguration(org.eclipse.wst.json.core.internal.validation.JSONValidationConfiguration) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) IJSONModel(org.eclipse.wst.json.core.document.IJSONModel) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IJSONSchemaDocument(org.eclipse.json.schema.IJSONSchemaDocument) IOException(java.io.IOException) JSONValidationInfo(org.eclipse.wst.json.core.internal.validation.JSONValidationInfo)

Aggregations

JSONValidationInfo (org.eclipse.wst.json.core.internal.validation.JSONValidationInfo)2 IOException (java.io.IOException)1 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)1 JsonArray (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray)1 JsonObject (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)1 JsonValue (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue)1 IJSONSchemaDocument (org.eclipse.json.schema.IJSONSchemaDocument)1 JSONSchemaType (org.eclipse.json.schema.JSONSchemaType)1 IJSONModel (org.eclipse.wst.json.core.document.IJSONModel)1 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)1 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)1 JSONValidationConfiguration (org.eclipse.wst.json.core.internal.validation.JSONValidationConfiguration)1 JSONValidationReport (org.eclipse.wst.json.core.internal.validation.JSONValidationReport)1 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)1