Class OpenAPISchemaValidator


  • public class OpenAPISchemaValidator
    extends Object
    Helper class for validating Json objects against their OpenAPI schema. Supported version of OpenAPI schema is https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md.
    Author:
    laurent
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static com.fasterxml.jackson.databind.JsonNode getJsonNode​(String jsonText)
      Get a Jackson JsonNode representation for Json object.
      static com.fasterxml.jackson.databind.JsonNode getJsonNodeForSchema​(String schemaText)
      Get a Jackson JsonNode representation for OpenAPI schema text.
      static boolean isJsonValid​(String schemaText, String jsonText)
      Check if a Json object is valid against the given OpenAPI schema specification.
      static List<String> validateJson​(com.fasterxml.jackson.databind.JsonNode schemaNode, com.fasterxml.jackson.databind.JsonNode jsonNode)
      Validate a Json object representing by its text against a schema object representing byt its text too.
      static List<String> validateJson​(String schemaText, String jsonText)
      Validate a Json object representing by its text against a schema object representing byt its text too.
      static List<String> validateJsonMessage​(com.fasterxml.jackson.databind.JsonNode specificationNode, com.fasterxml.jackson.databind.JsonNode jsonNode, String messagePathPointer, String contentType)
      Validate a Json object representing an OpenAPI message (response or request) against a node representing a full OpenAPI specification (and not just a schema node).
    • Constructor Detail

      • OpenAPISchemaValidator

        public OpenAPISchemaValidator()
    • Method Detail

      • isJsonValid

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

        public static List<String> validateJson​(String schemaText,
                                                String jsonText)
                                         throws IOException
        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 OpenAPI 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 OpenAPI 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
      • validateJson

        public static List<String> validateJson​(com.fasterxml.jackson.databind.JsonNode schemaNode,
                                                com.fasterxml.jackson.databind.JsonNode jsonNode)
        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 OpenAPI 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 OpenAPI 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 !
      • validateJsonMessage

        public static List<String> validateJsonMessage​(com.fasterxml.jackson.databind.JsonNode specificationNode,
                                                       com.fasterxml.jackson.databind.JsonNode jsonNode,
                                                       String messagePathPointer,
                                                       String contentType)
        Validate a Json object representing an OpenAPI message (response or request) against a node representing a full OpenAPI specification (and not just a schema node). Specify the message by providing a valid JSON pointer for messagePathPointer within specification and a contentType to allow finding the correct schema information. Validation is a deep one: its pursue checking children nodes on a failed parent. Validation is respectful of OpenAPI schema spec semantics regarding additional or unknown attributes: schema must explicitly set additionalProperties to false if you want to consider unknown attributes as validation errors. It returns a list of validation error messages.
        Parameters:
        specificationNode - The OpenAPI full specification as a Jackson node
        jsonNode - The Json object representing actual message as a Jackson node
        messagePathPointer - A JSON Pointer for accessing expected message definition within spec
        contentType - The Content-Type of the message to valid
        Returns:
        The list of validation failures. If empty, json object is valid !
      • 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
      • getJsonNodeForSchema

        public static com.fasterxml.jackson.databind.JsonNode getJsonNodeForSchema​(String schemaText)
                                                                            throws IOException
        Get a Jackson JsonNode representation for OpenAPI schema text. This handles the fact that OpenAPI spec may be formatted in YAML. In that case, it handles the conversion.
        Parameters:
        schemaText - The JSON or YAML string for OpenAPI schema
        Returns:
        The Jackson JsonNode corresponding to OpenAPI schema string
        Throws:
        IOException - if schema string representation cannot be parsed