Search in sources :

Example 1 with ValidationMessage

use of com.networknt.schema.ValidationMessage in project vscode-nextgenas by BowlerHatLLC.

the class ASConfigProjectConfigStrategy method getOptions.

public ProjectOptions getOptions() {
    changed = false;
    if (asconfigPath == null) {
        return null;
    }
    File asconfigFile = asconfigPath.toFile();
    if (!asconfigFile.exists()) {
        return null;
    }
    Path projectRoot = asconfigPath.getParent();
    ProjectType type = ProjectType.APP;
    String config = "flex";
    String[] files = null;
    String additionalOptions = null;
    CompilerOptions compilerOptions = new CompilerOptions();
    try (InputStream schemaInputStream = getClass().getResourceAsStream("/schemas/asconfig.schema.json")) {
        JsonSchemaFactory factory = new JsonSchemaFactory();
        JsonSchema schema = factory.getSchema(schemaInputStream);
        String contents = FileUtils.readFileToString(asconfigFile);
        ObjectMapper mapper = new ObjectMapper();
        //VSCode allows comments, so we should too
        mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
        JsonNode json = mapper.readTree(contents);
        Set<ValidationMessage> errors = schema.validate(json);
        if (!errors.isEmpty()) {
            System.err.println("Failed to validate asconfig.json.");
            for (ValidationMessage error : errors) {
                System.err.println(error.toString());
            }
            return null;
        } else {
            if (//optional, defaults to "app"
            json.has(ProjectOptions.TYPE)) {
                String typeString = json.get(ProjectOptions.TYPE).asText();
                type = ProjectType.fromToken(typeString);
            }
            if (//optional, defaults to "flex"
            json.has(ProjectOptions.CONFIG)) {
                config = json.get(ProjectOptions.CONFIG).asText();
            }
            if (//optional
            json.has(ProjectOptions.FILES)) {
                JsonNode jsonFiles = json.get(ProjectOptions.FILES);
                int fileCount = jsonFiles.size();
                files = new String[fileCount];
                for (int i = 0; i < fileCount; i++) {
                    String pathString = jsonFiles.get(i).asText();
                    Path filePath = projectRoot.resolve(pathString);
                    files[i] = filePath.toString();
                }
            }
            if (//optional
            json.has(ProjectOptions.COMPILER_OPTIONS)) {
                JsonNode jsonCompilerOptions = json.get(ProjectOptions.COMPILER_OPTIONS);
                if (jsonCompilerOptions.has(CompilerOptions.DEBUG)) {
                    compilerOptions.debug = jsonCompilerOptions.get(CompilerOptions.DEBUG).asBoolean();
                }
                if (jsonCompilerOptions.has(CompilerOptions.DEFINE)) {
                    HashMap<String, String> defines = new HashMap<>();
                    JsonNode jsonDefine = jsonCompilerOptions.get(CompilerOptions.DEFINE);
                    for (int i = 0, count = jsonDefine.size(); i < count; i++) {
                        JsonNode jsonNamespace = jsonDefine.get(i);
                        String name = jsonNamespace.get(CompilerOptions.DEFINE_NAME).asText();
                        JsonNode jsonValue = jsonNamespace.get(CompilerOptions.DEFINE_VALUE);
                        String value = jsonValue.asText();
                        if (jsonValue.isTextual()) {
                            value = "\"" + value + "\"";
                        }
                        defines.put(name, value.toString());
                    }
                    compilerOptions.defines = defines;
                }
                if (jsonCompilerOptions.has(CompilerOptions.EXTERNAL_LIBRARY_PATH)) {
                    JsonNode jsonExternalLibraryPath = jsonCompilerOptions.get(CompilerOptions.EXTERNAL_LIBRARY_PATH);
                    compilerOptions.externalLibraryPath = jsonPathsToList(jsonExternalLibraryPath, projectRoot);
                }
                if (jsonCompilerOptions.has(CompilerOptions.JS_EXTERNAL_LIBRARY_PATH)) {
                    JsonNode jsonExternalLibraryPath = jsonCompilerOptions.get(CompilerOptions.JS_EXTERNAL_LIBRARY_PATH);
                    compilerOptions.jsExternalLibraryPath = jsonPathsToList(jsonExternalLibraryPath, projectRoot);
                }
                if (jsonCompilerOptions.has(CompilerOptions.SWF_EXTERNAL_LIBRARY_PATH)) {
                    JsonNode jsonExternalLibraryPath = jsonCompilerOptions.get(CompilerOptions.SWF_EXTERNAL_LIBRARY_PATH);
                    compilerOptions.swfExternalLibraryPath = jsonPathsToList(jsonExternalLibraryPath, projectRoot);
                }
                if (jsonCompilerOptions.has(CompilerOptions.INCLUDE_CLASSES)) {
                    JsonNode jsonIncludeClasses = jsonCompilerOptions.get(CompilerOptions.INCLUDE_CLASSES);
                    ArrayList<String> includeClasses = new ArrayList<>();
                    for (int i = 0, count = jsonIncludeClasses.size(); i < count; i++) {
                        String qualifiedName = jsonIncludeClasses.get(i).asText();
                        includeClasses.add(qualifiedName);
                    }
                    compilerOptions.includeClasses = includeClasses;
                }
                if (jsonCompilerOptions.has(CompilerOptions.INCLUDE_NAMESPACES)) {
                    JsonNode jsonIncludeNamespaces = jsonCompilerOptions.get(CompilerOptions.INCLUDE_NAMESPACES);
                    ArrayList<String> includeNamespaces = new ArrayList<>();
                    for (int i = 0, count = jsonIncludeNamespaces.size(); i < count; i++) {
                        String namespaceURI = jsonIncludeNamespaces.get(i).asText();
                        includeNamespaces.add(namespaceURI);
                    }
                    compilerOptions.includeNamespaces = includeNamespaces;
                }
                if (jsonCompilerOptions.has(CompilerOptions.INCLUDE_SOURCES)) {
                    JsonNode jsonIncludeSources = jsonCompilerOptions.get(CompilerOptions.INCLUDE_SOURCES);
                    compilerOptions.includeSources = jsonPathsToList(jsonIncludeSources, projectRoot);
                }
                if (jsonCompilerOptions.has(CompilerOptions.JS_OUTPUT_TYPE)) {
                    String jsonJSOutputType = jsonCompilerOptions.get(CompilerOptions.JS_OUTPUT_TYPE).asText();
                    compilerOptions.jsOutputType = jsonJSOutputType;
                }
                if (jsonCompilerOptions.has(CompilerOptions.NAMESPACE)) {
                    JsonNode jsonLibraryPath = jsonCompilerOptions.get(CompilerOptions.NAMESPACE);
                    ArrayList<MXMLNamespaceMapping> namespaceMappings = new ArrayList<>();
                    for (int i = 0, count = jsonLibraryPath.size(); i < count; i++) {
                        JsonNode jsonNamespace = jsonLibraryPath.get(i);
                        String uri = jsonNamespace.get(CompilerOptions.NAMESPACE_URI).asText();
                        String manifest = jsonNamespace.get(CompilerOptions.NAMESPACE_MANIFEST).asText();
                        MXMLNamespaceMapping mapping = new MXMLNamespaceMapping(uri, manifest);
                        namespaceMappings.add(mapping);
                    }
                    compilerOptions.namespaceMappings = namespaceMappings;
                }
                if (jsonCompilerOptions.has(CompilerOptions.LIBRARY_PATH)) {
                    JsonNode jsonLibraryPath = jsonCompilerOptions.get(CompilerOptions.LIBRARY_PATH);
                    compilerOptions.libraryPath = jsonPathsToList(jsonLibraryPath, projectRoot);
                }
                if (jsonCompilerOptions.has(CompilerOptions.JS_LIBRARY_PATH)) {
                    JsonNode jsonLibraryPath = jsonCompilerOptions.get(CompilerOptions.JS_LIBRARY_PATH);
                    compilerOptions.jsLibraryPath = jsonPathsToList(jsonLibraryPath, projectRoot);
                }
                if (jsonCompilerOptions.has(CompilerOptions.SWF_LIBRARY_PATH)) {
                    JsonNode jsonLibraryPath = jsonCompilerOptions.get(CompilerOptions.SWF_LIBRARY_PATH);
                    compilerOptions.swfLibraryPath = jsonPathsToList(jsonLibraryPath, projectRoot);
                }
                if (jsonCompilerOptions.has(CompilerOptions.SOURCE_PATH)) {
                    JsonNode jsonSourcePath = jsonCompilerOptions.get(CompilerOptions.SOURCE_PATH);
                    compilerOptions.sourcePath = jsonPathsToList(jsonSourcePath, projectRoot);
                }
                if (jsonCompilerOptions.has(CompilerOptions.TARGETS)) {
                    JsonNode jsonTargets = jsonCompilerOptions.get(CompilerOptions.TARGETS);
                    ArrayList<String> targets = new ArrayList<>();
                    for (int i = 0, count = jsonTargets.size(); i < count; i++) {
                        String target = jsonTargets.get(i).asText();
                        targets.add(target);
                    }
                    compilerOptions.targets = targets;
                }
                if (jsonCompilerOptions.has(CompilerOptions.WARNINGS)) {
                    compilerOptions.warnings = jsonCompilerOptions.get(CompilerOptions.WARNINGS).asBoolean();
                }
            }
            //these options are formatted as if sent in through the command line
            if (//optional
            json.has(ProjectOptions.ADDITIONAL_OPTIONS)) {
                additionalOptions = json.get(ProjectOptions.ADDITIONAL_OPTIONS).asText();
            }
        }
    } catch (Exception e) {
        System.err.println("Failed to parse asconfig.json: " + e);
        e.printStackTrace();
        return null;
    }
    //include-sources compiler option
    if (type == ProjectType.LIB && files != null) {
        if (compilerOptions.includeSources == null) {
            compilerOptions.includeSources = new ArrayList<>();
        }
        for (int i = 0, count = files.length; i < count; i++) {
            String filePath = files[i];
            compilerOptions.includeSources.add(new File(filePath));
        }
        files = null;
    }
    ProjectOptions options = new ProjectOptions();
    options.type = type;
    options.config = config;
    options.files = files;
    options.compilerOptions = compilerOptions;
    options.additionalOptions = additionalOptions;
    return options;
}
Also used : Path(java.nio.file.Path) ValidationMessage(com.networknt.schema.ValidationMessage) HashMap(java.util.HashMap) InputStream(java.io.InputStream) JsonSchema(com.networknt.schema.JsonSchema) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonSchemaFactory(com.networknt.schema.JsonSchemaFactory) MXMLNamespaceMapping(org.apache.flex.compiler.internal.mxml.MXMLNamespaceMapping) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with ValidationMessage

use of com.networknt.schema.ValidationMessage in project fabric8-maven-plugin by fabric8io.

the class ResourceValidator method validate.

/**
 * Validates the resource descriptors as per JSON schema. If any resource is invalid it throws @{@link ConstraintViolationException} with
 * all violated constraints
 *
 * @return number of resources processed
 * @throws ConstraintViolationException
 * @throws IOException
 */
public int validate() throws ConstraintViolationException, IOException {
    for (File resource : resources) {
        if (resource.isFile() && resource.exists()) {
            try {
                log.info("validating %s resource", resource.toString());
                JsonNode inputSpecNode = geFileContent(resource);
                String kind = inputSpecNode.get("kind").toString();
                JsonSchema schema = getJsonSchema(prepareSchemaUrl(SCHEMA_JSON), kind);
                Set<ValidationMessage> errors = schema.validate(inputSpecNode);
                processErrors(errors, resource);
            } catch (JSONException e) {
                throw new ConstraintViolationException(e.getMessage(), new HashSet<ConstraintViolationImpl>());
            } catch (URISyntaxException e) {
                throw new IOException(e);
            }
        }
    }
    return resources.length;
}
Also used : ValidationMessage(com.networknt.schema.ValidationMessage) JsonSchema(com.networknt.schema.JsonSchema) JSONException(org.json.JSONException) ConstraintViolationException(javax.validation.ConstraintViolationException) JsonNode(com.fasterxml.jackson.databind.JsonNode) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) File(java.io.File)

Example 3 with ValidationMessage

use of com.networknt.schema.ValidationMessage in project vertx-web by vert-x3.

the class JsonTypeValidator method isValid.

@Override
public RequestParameter isValid(String value) throws ValidationException {
    try {
        JsonNode node;
        if (value == null)
            throw ValidationException.ValidationExceptionFactory.generateNotParsableJsonBodyException("Json should not be null");
        else if (value.length() == 0)
            node = JsonNodeFactory.instance.textNode("");
        else
            node = Json.mapper.readTree(value);
        Set<ValidationMessage> errors = schema.validate(node);
        if (errors.size() == 0) {
            return RequestParameter.create(new JsonObject(value));
        } else {
            throw ValidationException.ValidationExceptionFactory.generateInvalidJsonBodyException(errors.iterator().next().toString());
        }
    } catch (IOException e) {
        throw ValidationException.ValidationExceptionFactory.generateNotParsableJsonBodyException(e.getMessage());
    }
}
Also used : ValidationMessage(com.networknt.schema.ValidationMessage) JsonObject(io.vertx.core.json.JsonObject) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 4 with ValidationMessage

use of com.networknt.schema.ValidationMessage in project light-rest-4j by networknt.

the class SchemaValidator method doValidate.

private Status doValidate(final String value, final Object schema) {
    requireNonNull(schema, "A schema is required");
    Status status = null;
    Set<ValidationMessage> processingReport = null;
    try {
        final JsonNode schemaObject = Json.mapper().readTree(Json.pretty(schema));
        if (api != null) {
            if (this.definitions == null) {
                this.definitions = Json.mapper().readTree(Json.pretty(api.getDefinitions()));
            }
            ((ObjectNode) schemaObject).set(DEFINITIONS_FIELD, this.definitions);
        }
        JsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schemaObject);
        String normalisedValue = value;
        if (schema instanceof StringProperty) {
            normalisedValue = Util.quote(value);
        }
        final JsonNode content = Json.mapper().readTree(normalisedValue);
        processingReport = jsonSchema.validate(content);
    } catch (JsonParseException e) {
        return new Status(VALIDATOR_SCHEMA_INVALID_JSON, e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (processingReport != null && processingReport.size() > 0) {
        ValidationMessage vm = processingReport.iterator().next();
        status = new Status(VALIDATOR_SCHEMA, vm.getMessage());
    }
    return status;
}
Also used : Status(com.networknt.status.Status) ValidationMessage(com.networknt.schema.ValidationMessage) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonSchema(com.networknt.schema.JsonSchema) StringProperty(io.swagger.models.properties.StringProperty) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 5 with ValidationMessage

use of com.networknt.schema.ValidationMessage in project light-rest-4j by networknt.

the class SchemaValidator method doValidate.

private Status doValidate(final Object value, final JsonNode schema) {
    requireNonNull(schema, "A schema is required");
    Status status = null;
    Set<ValidationMessage> processingReport = null;
    try {
        JsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schema);
        final JsonNode content = Config.getInstance().getMapper().valueToTree(value);
        processingReport = jsonSchema.validate(content);
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (processingReport != null && processingReport.size() > 0) {
        ValidationMessage vm = processingReport.iterator().next();
        status = new Status(VALIDATOR_SCHEMA, vm.getMessage());
    }
    return status;
}
Also used : Status(com.networknt.status.Status) ValidationMessage(com.networknt.schema.ValidationMessage) JsonSchema(com.networknt.schema.JsonSchema) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)7 ValidationMessage (com.networknt.schema.ValidationMessage)7 JsonSchema (com.networknt.schema.JsonSchema)5 JsonParseException (com.fasterxml.jackson.core.JsonParseException)3 Status (com.networknt.status.Status)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonSchemaFactory (com.networknt.schema.JsonSchemaFactory)1 StringProperty (io.swagger.models.properties.StringProperty)1 JsonObject (io.vertx.core.json.JsonObject)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 MXMLNamespaceMapping (org.apache.flex.compiler.internal.mxml.MXMLNamespaceMapping)1 JSONException (org.json.JSONException)1