Search in sources :

Example 6 with JsonSchema

use of com.networknt.schema.JsonSchema in project SONG by overture-stack.

the class JsonSchemaUtils method getJsonSchemaFromClasspath.

@SneakyThrows
public static JsonSchema getJsonSchemaFromClasspath(String fileName) {
    JsonSchemaFactory factory = new JsonSchemaFactory();
    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
    JsonSchema schema = factory.getSchema(is);
    return schema;
}
Also used : InputStream(java.io.InputStream) JsonSchema(com.networknt.schema.JsonSchema) JsonSchemaFactory(com.networknt.schema.JsonSchemaFactory) SneakyThrows(lombok.SneakyThrows)

Example 7 with JsonSchema

use of com.networknt.schema.JsonSchema in project SONG by overture-stack.

the class JsonSchemaUtils method getJsonSchema.

@SneakyThrows
public static JsonSchema getJsonSchema(JsonNode node) {
    JsonSchemaFactory factory = new JsonSchemaFactory();
    JsonSchema schema = factory.getSchema(node);
    return schema;
}
Also used : JsonSchema(com.networknt.schema.JsonSchema) JsonSchemaFactory(com.networknt.schema.JsonSchemaFactory) SneakyThrows(lombok.SneakyThrows)

Example 8 with JsonSchema

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

the class SchemaValidator method doValidate.

private Status doValidate(final Object value, final JsonNode schema) {
    requireNonNull(schema, "A schema is required");
    Status status = null;
    Set<ValidationMessage> processingReport = null;
    try {
        JsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schema);
        final JsonNode content = Config.getInstance().getMapper().valueToTree(value);
        processingReport = jsonSchema.validate(content);
    } 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) JsonSchema(com.networknt.schema.JsonSchema) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 9 with JsonSchema

use of com.networknt.schema.JsonSchema 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

JsonSchema (com.networknt.schema.JsonSchema)9 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ValidationMessage (com.networknt.schema.ValidationMessage)5 JsonSchemaFactory (com.networknt.schema.JsonSchemaFactory)4 JsonParseException (com.fasterxml.jackson.core.JsonParseException)3 Status (com.networknt.status.Status)3 InputStream (java.io.InputStream)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 File (java.io.File)2 SneakyThrows (lombok.SneakyThrows)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 StringProperty (io.swagger.models.properties.StringProperty)1 IOException (java.io.IOException)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 lombok.val (lombok.val)1 MXMLNamespaceMapping (org.apache.flex.compiler.internal.mxml.MXMLNamespaceMapping)1