Search in sources :

Example 1 with ProblemHandler

use of org.leadpony.justify.api.ProblemHandler in project che-server by eclipse-che.

the class DevfileSchemaValidator method validate.

public void validate(JsonNode contentNode) throws DevfileFormatException {
    try {
        List<Problem> validationErrors = new ArrayList<>();
        ProblemHandler handler = ProblemHandler.collectingTo(validationErrors);
        String devfileVersion = devfileVersionDetector.devfileVersion(contentNode);
        if (!schemasByVersion.containsKey(devfileVersion)) {
            throw new DevfileFormatException(String.format("Version '%s' of the devfile is not supported. Supported versions are '%s'.", devfileVersion, SUPPORTED_VERSIONS));
        }
        JsonSchema schema = schemasByVersion.get(devfileVersion);
        try (JsonReader reader = service.createReader(new StringReader(jsonMapper.writeValueAsString(contentNode)), schema, handler)) {
            reader.read();
        }
        if (!validationErrors.isEmpty()) {
            String error = errorMessageComposer.extractMessages(validationErrors, new StringBuilder());
            throw new DevfileFormatException(error);
        }
    } catch (DevfileException dfe) {
        throw new DevfileFormatException(format("Devfile schema validation failed. Error: %s", dfe.getMessage()));
    } catch (IOException e) {
        throw new DevfileFormatException("Unable to validate Devfile. Error: " + e.getMessage());
    }
}
Also used : ProblemHandler(org.leadpony.justify.api.ProblemHandler) JsonSchema(org.leadpony.justify.api.JsonSchema) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) JsonReader(jakarta.json.JsonReader) Problem(org.leadpony.justify.api.Problem) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) IOException(java.io.IOException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)

Example 2 with ProblemHandler

use of org.leadpony.justify.api.ProblemHandler in project devspaces-images by redhat-developer.

the class DevfileSchemaValidator method validate.

public void validate(JsonNode contentNode) throws DevfileFormatException {
    try {
        List<Problem> validationErrors = new ArrayList<>();
        ProblemHandler handler = ProblemHandler.collectingTo(validationErrors);
        String devfileVersion = devfileVersionDetector.devfileVersion(contentNode);
        if (!schemasByVersion.containsKey(devfileVersion)) {
            throw new DevfileFormatException(String.format("Version '%s' of the devfile is not supported. Supported versions are '%s'.", devfileVersion, SUPPORTED_VERSIONS));
        }
        JsonSchema schema = schemasByVersion.get(devfileVersion);
        try (JsonReader reader = service.createReader(new StringReader(jsonMapper.writeValueAsString(contentNode)), schema, handler)) {
            reader.read();
        }
        if (!validationErrors.isEmpty()) {
            String error = errorMessageComposer.extractMessages(validationErrors, new StringBuilder());
            throw new DevfileFormatException(error);
        }
    } catch (DevfileException dfe) {
        throw new DevfileFormatException(format("Devfile schema validation failed. Error: %s", dfe.getMessage()));
    } catch (IOException e) {
        throw new DevfileFormatException("Unable to validate Devfile. Error: " + e.getMessage());
    }
}
Also used : ProblemHandler(org.leadpony.justify.api.ProblemHandler) JsonSchema(org.leadpony.justify.api.JsonSchema) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) JsonReader(jakarta.json.JsonReader) Problem(org.leadpony.justify.api.Problem) DevfileFormatException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException) IOException(java.io.IOException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)

Example 3 with ProblemHandler

use of org.leadpony.justify.api.ProblemHandler in project selenium_java by sergueik.

the class Validate method readSchemaCatalogAt.

private void readSchemaCatalogAt(Location location) {
    console.print(READ_CATALOG, location);
    JsonSchema schema = readSchemaFromResource("catalog.schema.json");
    List<Problem> problems = new ArrayList<>();
    ProblemHandler handler = createProblemHandler(problems);
    try (JsonParser parser = service.createParser(openCatalog(location), schema, handler)) {
        parseCatalog(parser, location);
        if (!problems.isEmpty()) {
            console.withColor(Color.DANGER).print(CATALOG_INVALID, location, Problems.countLeast(problems));
            throw new CommandException(CATALOG_FAILED);
        }
    } catch (JsonParsingException e) {
        console.withColor(Color.DANGER).print(CATALOG_MALFORMED, e);
        throw new CommandException(CATALOG_FAILED);
    } catch (JsonException e) {
        throw new CommandException(e);
    }
}
Also used : JsonException(javax.json.JsonException) ProblemHandler(org.leadpony.justify.api.ProblemHandler) JsonSchema(org.leadpony.justify.api.JsonSchema) ArrayList(java.util.ArrayList) Problem(org.leadpony.justify.api.Problem) JsonParser(javax.json.stream.JsonParser) JsonParsingException(javax.json.stream.JsonParsingException)

Example 4 with ProblemHandler

use of org.leadpony.justify.api.ProblemHandler in project mosaic by eclipse.

the class ObjectInstantiation method validateFile.

private void validateFile(InputStream input, InputStream schemaInput) throws InstantiationException {
    final JsonValidationService service = JsonValidationService.newInstance();
    final JsonSchema schema = service.readSchema(schemaInput);
    final List<String> problems = new ArrayList<>();
    final ProblemHandler handler = service.createProblemPrinter(problems::add);
    try (JsonParser parser = service.createParser(input, schema, handler)) {
        while (parser.hasNext()) {
            parser.next();
        // ignore, we let GSON do the parsing later.
        }
    }
    if (!problems.isEmpty()) {
        StringBuilder errorMessage = new StringBuilder();
        problems.forEach((p) -> {
            errorMessage.append(p);
            errorMessage.append(NEWLINE);
        });
        throw new InstantiationException("The " + clazz.getSimpleName() + " config is not valid: " + errorMessage);
    }
}
Also used : ProblemHandler(org.leadpony.justify.api.ProblemHandler) JsonValidationService(org.leadpony.justify.api.JsonValidationService) JsonSchema(org.leadpony.justify.api.JsonSchema) ArrayList(java.util.ArrayList) JsonParser(javax.json.stream.JsonParser)

Example 5 with ProblemHandler

use of org.leadpony.justify.api.ProblemHandler in project selenium_java by sergueik.

the class Validate method validateInstanceAt.

/**
 * Validates a JSON instance.
 *
 * @param location the location of the JSON instance to be validated.
 * @param schema   the JSON schema against which the instance to be validated.
 */
private void validateInstanceAt(Location location, JsonSchema schema) {
    console.print(VALIDATE_INSTANCE, location);
    List<Problem> problems = new ArrayList<>();
    ProblemHandler handler = createProblemHandler(problems);
    try (JsonReader reader = service.createReader(openInstance(location), schema, handler)) {
        reader.readValue();
    } catch (JsonParsingException e) {
        console.withColor(Color.DANGER).print(INSTANCE_MALFORMED, e);
        setStatus(Status.INVALID);
        return;
    } catch (JsonException e) {
        throw new CommandException(e);
    }
    if (problems.isEmpty()) {
        console.withColor(Color.SUCCESS).print(INSTANCE_VALID, location);
    } else {
        console.withColor(Color.DANGER).print(INSTANCE_INVALID, location, Problems.countLeast(problems));
        setStatus(Status.INVALID);
    }
}
Also used : JsonException(javax.json.JsonException) ProblemHandler(org.leadpony.justify.api.ProblemHandler) ArrayList(java.util.ArrayList) JsonReader(javax.json.JsonReader) Problem(org.leadpony.justify.api.Problem) JsonParsingException(javax.json.stream.JsonParsingException)

Aggregations

ProblemHandler (org.leadpony.justify.api.ProblemHandler)8 JsonSchema (org.leadpony.justify.api.JsonSchema)7 ArrayList (java.util.ArrayList)6 StringReader (java.io.StringReader)5 JsonReader (jakarta.json.JsonReader)4 JsonValidationService (org.leadpony.justify.api.JsonValidationService)4 InputStream (java.io.InputStream)3 Problem (org.leadpony.justify.api.Problem)3 JsonArray (jakarta.json.JsonArray)2 JsonException (jakarta.json.JsonException)2 JsonObject (jakarta.json.JsonObject)2 JsonPatch (jakarta.json.JsonPatch)2 JsonProvider (jakarta.json.spi.JsonProvider)2 JsonParser (jakarta.json.stream.JsonParser)2 IOException (java.io.IOException)2 List (java.util.List)2 JsonReader (javax.json.JsonReader)2 JsonParser (javax.json.stream.JsonParser)2 DevfileException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)2 DevfileFormatException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException)2