All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.swagger.inflector.schema.SchemaValidator Maven / Gradle / Ivy

There is a newer version: 2.0.12
Show newest version
package io.swagger.inflector.schema;

import com.fasterxml.jackson.databind.JsonNode;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import io.swagger.util.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class SchemaValidator {
    static Map SCHEMA_CACHE = new HashMap();
    private static final Logger LOGGER = LoggerFactory.getLogger(SchemaValidator.class);

    public enum Direction{
        INPUT,
        OUTPUT
    }

    public static boolean validate(Object o, String schema, Direction direction) {
        try {
            JsonNode schemaObject = Json.mapper().readTree(schema);
            JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
            JsonNode content = Json.mapper().convertValue(o, JsonNode.class);
            com.github.fge.jsonschema.main.JsonSchema jsonSchema = factory.getJsonSchema(schemaObject);

            ProcessingReport report = jsonSchema.validate(content);
            if(!report.isSuccess()) {
                if(direction.equals(Direction.INPUT)) {
                    LOGGER.warn("input: " + content.toString() + "\n" + "does not match schema: \n" + schema);
                }
                else {
                    LOGGER.warn("response: " + content.toString() + "\n" + "does not match schema: \n" + schema);
                }
            }
            return report.isSuccess();
        }
        catch (Exception e) {
            LOGGER.error("can't validate model against schema", e);
        }

        return true;
    }

    public static com.github.fge.jsonschema.main.JsonSchema getValidationSchema(String schema) throws IOException, ProcessingException {
        schema = schema.trim();

        JsonSchema output = SCHEMA_CACHE.get(schema);

        if(output == null) {
            JsonNode schemaObject = Json.mapper().readTree(schema);
            JsonSchemaFactory factory = JsonSchemaFactory.byDefault();
            com.github.fge.jsonschema.main.JsonSchema jsonSchema = factory.getJsonSchema(schemaObject);
            SCHEMA_CACHE.put(schema, jsonSchema);
            output = jsonSchema;
        }
        return output;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy