use of com.github.fge.jsonschema.core.report.ProcessingReport in project connectors-workspace-one by vmware.
the class JsonSchemaValidator method validate.
public boolean validate(JsonNode docNode) throws ProcessingException {
ProcessingReport report = schema.validate(docNode);
boolean valid = report.isSuccess();
if (!valid) {
logger.error("Processing report: {}", report);
}
return valid;
}
use of com.github.fge.jsonschema.core.report.ProcessingReport in project carina by qaprosoft.
the class JsonValidator method validateJsonAgainstSchema.
public static void validateJsonAgainstSchema(String jsonSchema, String jsonData) {
// create the Json nodes for schema and data
JsonNode schemaNode;
JsonNode data;
try {
schemaNode = JsonLoader.fromString(jsonSchema);
} catch (IOException e) {
throw new RuntimeException("Can't read schema from String: " + e.getMessage(), e);
}
try {
data = JsonLoader.fromString(jsonData);
} catch (IOException e) {
throw new RuntimeException("Can't read json from String: " + e.getMessage(), e);
}
JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
// load the schema and validate
JsonSchema schema;
try {
schema = factory.getJsonSchema(schemaNode);
} catch (ProcessingException e) {
throw new RuntimeException("Can't process shema", e);
}
ProcessingReport report;
try {
report = schema.validate(data, true);
} catch (ProcessingException e) {
throw new RuntimeException("Exception during processing Json", e);
}
if (report.isSuccess()) {
LOGGER.info("Validation against Json schema successfully passed");
} else {
StringBuffer result = new StringBuffer("Validation against Json schema failed: \n");
Iterator<ProcessingMessage> itr = report.iterator();
while (itr.hasNext()) {
ProcessingMessage message = (ProcessingMessage) itr.next();
JsonNode json = message.asJson();
String instance = json.get("instance").get("pointer").asText();
String errorMsg = json.get("message").asText();
result.append("[");
result.append(instance);
result.append("]: ");
result.append(errorMsg);
result.append("\n");
}
throw new RuntimeException(result.toString());
}
}
use of com.github.fge.jsonschema.core.report.ProcessingReport in project ocvn by devgateway.
the class OcdsSchemaValidatorService method validate.
/**
* Validates the incoming {@link JsonNode} against OCDS schema
*
* @param jsonNode
* @return
*/
public ProcessingReportWithNode validate(final JsonNode jsonNode) {
try {
ProcessingReport processingReport = schema.validate(jsonNode);
ProcessingReportWithNode processingReportWithNode = null;
try {
processingReportWithNode = new ProcessingReportWithNode(processingReport, processingReport.isSuccess() ? null : jacksonObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonNode.get("ocid")));
} catch (JsonProcessingException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
return processingReportWithNode;
} catch (ProcessingException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
return null;
}
use of com.github.fge.jsonschema.core.report.ProcessingReport in project apex-malhar by apache.
the class JsonParser method processTuple.
@Override
public void processTuple(byte[] tuple) {
if (tuple == null) {
if (err.isConnected()) {
err.emit(new KeyValPair<String, String>(null, "null tuple"));
}
errorTupleCount++;
return;
}
String incomingString = new String(tuple);
try {
if (schema != null) {
ProcessingReport report = null;
JsonNode data = JsonLoader.fromString(incomingString);
report = schema.validate(data);
if (report != null && !report.isSuccess()) {
Iterator<ProcessingMessage> iter = report.iterator();
StringBuilder s = new StringBuilder();
while (iter.hasNext()) {
ProcessingMessage pm = iter.next();
s.append(pm.asJson().get("instance").findValue("pointer")).append(":").append(pm.asJson().get("message")).append(",");
}
s.setLength(s.length() - 1);
errorTupleCount++;
if (err.isConnected()) {
err.emit(new KeyValPair<String, String>(incomingString, s.toString()));
}
return;
}
}
if (parsedOutput.isConnected()) {
parsedOutput.emit(new JSONObject(incomingString));
parsedOutputCount++;
}
if (out.isConnected()) {
out.emit(objMapper.readValue(tuple, clazz));
emittedObjectCount++;
}
} catch (JSONException | ProcessingException | IOException e) {
errorTupleCount++;
if (err.isConnected()) {
err.emit(new KeyValPair<String, String>(incomingString, e.getMessage()));
}
logger.error("Failed to parse json tuple {}, Exception = {} ", tuple, e);
}
}
use of com.github.fge.jsonschema.core.report.ProcessingReport in project metron by apache.
the class LEEFParserTest method validateJsonData.
protected boolean validateJsonData(final String jsonSchema, final String jsonData) throws Exception {
final JsonNode d = JsonLoader.fromString(jsonData);
final JsonNode s = JsonLoader.fromString(jsonSchema);
final JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
JsonValidator v = factory.getValidator();
ProcessingReport report = v.validate(s, d);
return report.toString().contains("success");
}
Aggregations