Search in sources :

Example 16 with ProcessingReport

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;
}
Also used : ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport)

Example 17 with ProcessingReport

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());
    }
}
Also used : ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) ProcessingMessage(com.github.fge.jsonschema.core.report.ProcessingMessage) JsonSchema(com.github.fge.jsonschema.main.JsonSchema) JsonSchemaFactory(com.github.fge.jsonschema.main.JsonSchemaFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException)

Example 18 with ProcessingReport

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;
}
Also used : ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 19 with ProcessingReport

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);
    }
}
Also used : ProcessingMessage(com.github.fge.jsonschema.core.report.ProcessingMessage) JSONException(org.codehaus.jettison.json.JSONException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) JSONObject(org.codehaus.jettison.json.JSONObject) KeyValPair(org.apache.apex.malhar.lib.util.KeyValPair) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException)

Example 20 with ProcessingReport

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");
}
Also used : ProcessingReport(com.github.fge.jsonschema.core.report.ProcessingReport) JsonSchemaFactory(com.github.fge.jsonschema.main.JsonSchemaFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonValidator(com.github.fge.jsonschema.main.JsonValidator)

Aggregations

ProcessingReport (com.github.fge.jsonschema.core.report.ProcessingReport)24 JsonNode (com.fasterxml.jackson.databind.JsonNode)14 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)11 JsonSchema (com.github.fge.jsonschema.main.JsonSchema)8 JsonSchemaFactory (com.github.fge.jsonschema.main.JsonSchemaFactory)7 IOException (java.io.IOException)7 ProcessingMessage (com.github.fge.jsonschema.core.report.ProcessingMessage)5 JsonValidator (com.github.fge.jsonschema.main.JsonValidator)4 JsonParseException (com.fasterxml.jackson.core.JsonParseException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 Extension (io.syndesis.common.model.extension.Extension)3 Test (org.junit.Test)3 TestExecReport (de.ipk_gatersleben.bit.bi.bridge.brapicomp.testing.reports.TestExecReport)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 ConnectionClosedException (org.apache.http.ConnectionClosedException)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Violation (io.syndesis.common.model.Violation)1