Search in sources :

Example 6 with ValidationMessage

use of com.networknt.schema.ValidationMessage in project light-rest-4j by networknt.

the class SchemaValidator method doValidate.

private Status doValidate(final Object value, final Object schema) {
    requireNonNull(schema, "A schema is required");
    Status status = null;
    Set<ValidationMessage> processingReport = null;
    try {
        final JsonNode schemaObject = Json.mapper().readTree(Json.pretty(schema));
        if (api != null) {
            if (this.definitions == null) {
                this.definitions = Json.mapper().readTree(Json.pretty(api.getDefinitions()));
            }
            ((ObjectNode) schemaObject).set(DEFINITIONS_FIELD, this.definitions);
        }
        JsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schemaObject);
        final JsonNode content = Json.mapper().valueToTree(value);
        processingReport = jsonSchema.validate(content);
    } catch (JsonParseException e) {
        return new Status(VALIDATOR_SCHEMA_INVALID_JSON, e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (processingReport != null && processingReport.size() > 0) {
        ValidationMessage vm = processingReport.iterator().next();
        status = new Status(VALIDATOR_SCHEMA, vm.getMessage());
    }
    return status;
}
Also used : Status(com.networknt.status.Status) ValidationMessage(com.networknt.schema.ValidationMessage) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonSchema(com.networknt.schema.JsonSchema) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ValidationMessage (com.networknt.schema.ValidationMessage)6 JsonSchema (com.networknt.schema.JsonSchema)5 JsonParseException (com.fasterxml.jackson.core.JsonParseException)3 Status (com.networknt.status.Status)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 File (java.io.File)2 IOException (java.io.IOException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonSchemaFactory (com.networknt.schema.JsonSchemaFactory)1 StringProperty (io.swagger.models.properties.StringProperty)1 JsonObject (io.vertx.core.json.JsonObject)1 InputStream (java.io.InputStream)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 MXMLNamespaceMapping (org.apache.flex.compiler.internal.mxml.MXMLNamespaceMapping)1 JSONException (org.json.JSONException)1