Class JsonSchemaValidator


  • public class JsonSchemaValidator
    extends Object
    Helper class for validating Json objects against their Json schema. Supported version of Json schema is http://json-schema.org/draft-07/schema.
    Author:
    laurent
    • Constructor Detail

      • JsonSchemaValidator

        public JsonSchemaValidator()
    • Method Detail

      • isJsonValid

        public static boolean isJsonValid​(String schemaText,
                                          String jsonText)
                                   throws IOException
        Check if a Json object is valid against the given Json schema specification.
        Parameters:
        schemaText - The Json schema specification as a string
        jsonText - The Json object as a string
        Returns:
        True if Json object is valid, false otherwise
        Throws:
        IOException - if json string representations cannot be parsed
      • validateJson

        public static List<String> validateJson​(String schemaText,
                                                String jsonText)
                                         throws IOException,
                                                com.github.fge.jsonschema.core.exceptions.ProcessingException
        Validate a Json object representing by its text against a schema object representing byt its text too. Validation is a deep one: its pursue checking children nodes on a failed parent. Validation is respectful of Json schema spec semantics regarding additional or unknown attributes: schema must explicitely set additionalProperties to false if you want to consider unknown attributes as validation errors. It returns a list of validation error messages.
        Parameters:
        schemaText - The Json schema specification as a string
        jsonText - The Json object as a string
        Returns:
        The list of validation failures. If empty, json object is valid !
        Throws:
        IOException - if json string representations cannot be parsed
        com.github.fge.jsonschema.core.exceptions.ProcessingException - if json node does not represent valid Schema
      • validateJson

        public static List<String> validateJson​(com.fasterxml.jackson.databind.JsonNode schemaNode,
                                                com.fasterxml.jackson.databind.JsonNode jsonNode)
                                         throws com.github.fge.jsonschema.core.exceptions.ProcessingException
        Validate a Json object representing by its text against a schema object representing byt its text too. Validation is a deep one: its pursue checking children nodes on a failed parent. Validation is respectful of Json schema spec semantics regarding additional or unknown attributes: schema must explicitely set additionalProperties to false if you want to consider unknown attributes as validation errors. It returns a list of validation error messages.
        Parameters:
        schemaNode - The Json schema specification as a Jackson node
        jsonNode - The Json object as a Jackson node
        Returns:
        The list of validation failures. If empty, json object is valid !
        Throws:
        com.github.fge.jsonschema.core.exceptions.ProcessingException - if json node does not represent valid Schema
      • getJsonNode

        public static com.fasterxml.jackson.databind.JsonNode getJsonNode​(String jsonText)
                                                                   throws IOException
        Get a Jackson JsonNode representation for Json object.
        Parameters:
        jsonText - The Json object as a string
        Returns:
        The Jackson JsonNode corresponding to json object string
        Throws:
        IOException - if json string representation cannot be parsed
      • getSchemaNode

        public static com.github.fge.jsonschema.main.JsonSchema getSchemaNode​(String schemaText)
                                                                       throws IOException,
                                                                              com.github.fge.jsonschema.core.exceptions.ProcessingException
        Get a Jackson JsonNode representation for Json schema.
        Parameters:
        schemaText - The Json schema specification as a string
        Returns:
        The Jackson JsonSchema corresponding to json schema string
        Throws:
        IOException - if json string representation cannot be parsed
        com.github.fge.jsonschema.core.exceptions.ProcessingException - if json node does not represent valid Schema