use of org.leadpony.justify.api.Problem 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());
}
}
use of org.leadpony.justify.api.Problem 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());
}
}
use of org.leadpony.justify.api.Problem 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);
}
}
use of org.leadpony.justify.api.Problem 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);
}
}
use of org.leadpony.justify.api.Problem in project che-server by eclipse-che.
the class ErrorMessageComposer method extractMessages.
/**
* Parses {@link Problem} list into an error string. Each problem is recursively parsed to extract
* nested errors if any.
*
* @param validationErrors Schema validation problems list
* @return composite error string
*/
public String extractMessages(List<Problem> validationErrors, StringBuilder messageBuilder) {
for (Problem problem : validationErrors) {
int branchCount = problem.countBranches();
if (branchCount == 0) {
messageBuilder.append(getMessage(problem));
} else {
messageBuilder.append(problem.getMessage()).append(": [");
for (int i = 0; i < branchCount; i++) {
extractMessages(problem.getBranch(i), messageBuilder);
}
messageBuilder.append("]");
}
}
return messageBuilder.toString();
}
Aggregations