use of com.github.fge.jsonschema.core.report.ProcessingReport in project syndesis by syndesisio.
the class SwaggerHelper method validateJSonSchema.
private static SwaggerModelInfo validateJSonSchema(final String specification, final Swagger model) {
try {
final JsonNode specRoot = convertToJson(specification);
final ProcessingReport report = SWAGGER_2_0_SCHEMA.validate(specRoot);
final List<Violation> errors = new ArrayList<>();
final List<Violation> warnings = new ArrayList<>();
for (final ProcessingMessage message : report) {
final boolean added = append(errors, message, Optional.of("error"));
if (!added) {
append(warnings, message, Optional.empty());
}
}
return new SwaggerModelInfo.Builder().errors(errors).warnings(warnings).model(model).resolvedSpecification(specification).build();
} catch (IOException | ProcessingException ex) {
LOG.error("Unable to load the schema file embedded in the artifact", ex);
return new SwaggerModelInfo.Builder().addError(new Violation.Builder().error("error").property("").message("Unable to load the swagger schema file embedded in the artifact").build()).build();
}
}
use of com.github.fge.jsonschema.core.report.ProcessingReport in project syndesis by syndesisio.
the class ExtensionSchemaValidationTest method addSchemaVersionInPublicModelExtensionTest.
@Test
public void addSchemaVersionInPublicModelExtensionTest() throws ProcessingException, IOException {
String syndesisExtensionSchema = "/syndesis/syndesis-extension-definition-schema.json";
JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema("resource:" + syndesisExtensionSchema);
ExtensionConverter converter = new DefaultExtensionConverter();
ObjectNode tree = OBJECT_MAPPER.createObjectNode().put("extensionId", "my-extension").put("name", "Name").put("description", "Description").put("version", "1.0.0");
ProcessingReport report = schema.validate(tree);
assertFalse(report.toString(), report.iterator().hasNext());
Extension extension = converter.toInternalExtension(tree);
assertEquals(ExtensionConverter.getCurrentSchemaVersion(), extension.getSchemaVersion());
}
use of com.github.fge.jsonschema.core.report.ProcessingReport in project syndesis by syndesisio.
the class ExtensionSchemaValidationTest method upgradePublicModelExtensionTest.
@Test
public void upgradePublicModelExtensionTest() throws ProcessingException, IOException {
String syndesisExtensionSchema = "/syndesis/syndesis-extension-definition-schema.json";
JsonSchema schema = JsonSchemaFactory.byDefault().getJsonSchema("resource:" + syndesisExtensionSchema);
ExtensionConverter converter = new DefaultExtensionConverter();
Extension extension = new Extension.Builder().extensionId("my-extension").name("Name").description("Description").version("1.0.0").schemaVersion("old-V0.1").extensionType(Extension.Type.Steps).build();
JsonNode tree = converter.toPublicExtension(extension);
ProcessingReport report = schema.validate(tree);
assertFalse(report.toString(), report.iterator().hasNext());
Extension extensionClone = converter.toInternalExtension(tree);
assertNotEquals(extensionClone, extension);
assertEquals(ExtensionConverter.getCurrentSchemaVersion(), extensionClone.getSchemaVersion());
}
use of com.github.fge.jsonschema.core.report.ProcessingReport in project bender by Nextdoor.
the class BenderConfig method validate.
public static boolean validate(String data, ObjectMapper objectMapper, BenderSchema benderSchema) throws ConfigurationException {
ProcessingReport report;
try {
/*
* Create object
*/
JsonNode node = objectMapper.readTree(data);
/*
* Create JSON schema
*/
JsonNode jsonSchema = benderSchema.getSchema();
/*
* Validate
*/
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
final JsonSchema schema = factory.getJsonSchema(jsonSchema);
report = schema.validate(node);
} catch (IOException | ProcessingException ioe) {
throw new ConfigurationException("unable to validate config", ioe);
}
if (report.isSuccess()) {
return true;
} else {
throw new ConfigurationException("invalid config file", report.iterator().next().asException());
}
}
use of com.github.fge.jsonschema.core.report.ProcessingReport in project service-proxy by membrane.
the class JSONValidator method validateMessage.
public Outcome validateMessage(Exchange exc, InputStream body, Charset charset, String source) throws Exception {
List<String> errors;
boolean success = true;
try {
JsonNode node = JsonLoader.fromReader(new InputStreamReader(body, charset));
ProcessingReport report = schema.validateUnchecked(node);
success = report.isSuccess();
errors = new ArrayList<String>();
for (ProcessingMessage message : report) errors.add(message.getMessage());
} catch (JsonParseException e) {
success = false;
errors = new ArrayList<String>();
errors.add(e.getMessage());
}
if (success) {
valid.incrementAndGet();
return Outcome.CONTINUE;
}
if (failureHandler == FailureHandler.VOID) {
StringBuilder message = new StringBuilder();
message.append(source);
message.append(": ");
for (String error : errors) {
message.append(error);
message.append(";");
}
exc.setProperty("error", message.toString());
invalid.incrementAndGet();
return Outcome.ABORT;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonGenerator jg = new JsonFactory().createGenerator(baos);
jg.writeStartObject();
jg.writeStringField("source", source);
jg.writeArrayFieldStart("errors");
for (String message : errors) jg.writeString(message);
jg.close();
if (failureHandler != null) {
failureHandler.handleFailure(new String(baos.toByteArray(), UTF8), exc);
exc.setResponse(Response.badRequest().contentType("application/json;charset=utf-8").body("{\"error\":\"error\"}".getBytes(UTF8)).build());
} else {
exc.setResponse(Response.badRequest().contentType("application/json;charset=utf-8").body(baos.toByteArray()).build());
}
invalid.incrementAndGet();
return Outcome.ABORT;
}
Aggregations