Class AsyncAPISchemaValidator

java.lang.Object
io.github.microcks.util.asyncapi.AsyncAPISchemaValidator

public class AsyncAPISchemaValidator extends Object
Helper class for validating Json objects against their AsyncAPI schema. Supported version of AsyncAPI schema is https://www.asyncapi.com/docs/specifications/2.0.0/.
Author:
laurent
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
  • Method Summary

    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
    Get a Jackson JsonNode representation for AsyncAPI schema text.
    static boolean
    isJsonValid(String schemaText, String jsonText)
    Check if a Json object is valid against the given AsyncAPI schema specification.
    static List<String>
    validateAvroMessage(com.fasterxml.jackson.databind.JsonNode specificationNode, byte[] avroBinary, String messagePathPointer, SchemaMap schemaMap)
    Validate an Avro binary representing an AsyncAPI message against a node representing a full AsyncAPI specification (and not just a schema node).
    static List<String>
    validateAvroMessage(com.fasterxml.jackson.databind.JsonNode specificationNode, org.apache.avro.generic.GenericRecord avroRecord, String messagePathPointer, SchemaMap schemaMap)
    Validate an Avro binary representing an AsyncAPI message against a node representing a full AsyncAPI specification (and not just a schema node).
    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(com.fasterxml.jackson.databind.JsonNode schemaNode, com.fasterxml.jackson.databind.JsonNode jsonNode, String namespace)
    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)
    Validate a Json object representing an AsyncAPI message against a node representing a full AsyncAPI specification (and not just a schema node).
    static List<String>
    validateJsonMessage(com.fasterxml.jackson.databind.JsonNode specificationNode, com.fasterxml.jackson.databind.JsonNode jsonNode, String messagePathPointer, String namespace)
    Validate a Json object representing an AsyncAPI message against a node representing a full AsyncAPI specification (and not just a schema node).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • isJsonValid

      public static boolean isJsonValid(String schemaText, String jsonText) throws IOException
      Check if a Json object is valid against the given AsyncAPI schema specification.
      Parameters:
      schemaText - The AsyncAPI 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 AsyncAPI 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 AsyncAPI 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 AsyncAPI 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 AsyncAPI 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 !
    • validateJson

      public static List<String> validateJson(com.fasterxml.jackson.databind.JsonNode schemaNode, com.fasterxml.jackson.databind.JsonNode jsonNode, String namespace)
      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 AsyncAPI 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 AsyncAPI schema specification as a Jackson node
      jsonNode - The Json object as a Jackson node
      namespace - Namespace definition to resolve relative dependencies in Json schema
      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)
      Validate a Json object representing an AsyncAPI message against a node representing a full AsyncAPI specification (and not just a schema node). Specify the message by providing a valid JSON pointer for messagePathPointer 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 AsyncAPI 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 AsyncAPI 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
      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 namespace)
      Validate a Json object representing an AsyncAPI message against a node representing a full AsyncAPI specification (and not just a schema node). Specify the message by providing a valid JSON pointer for messagePathPointer 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 AsyncAPI 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 AsyncAPI 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
      namespace - Namespace definition to resolve relative dependencies in OpenAPI schema
      Returns:
      The list of validation failures. If empty, json object is valid !
    • validateAvroMessage

      public static List<String> validateAvroMessage(com.fasterxml.jackson.databind.JsonNode specificationNode, byte[] avroBinary, String messagePathPointer, SchemaMap schemaMap)
      Validate an Avro binary representing an AsyncAPI message against a node representing a full AsyncAPI specification (and not just a schema node). Specify the message by providing a valid JSON pointer for messagePathPointer within specification to allow finding the correct schema information. Validation with avro binary is a shallow one: because we do not have the schema used for writing the bytes, we can only check the given bytes are fitting into the read schema from AsyncAPI document. It returns a list of validation error messages.
      Parameters:
      specificationNode - The AsyncAPI full specification as a Jackson node
      avroBinary - The avro binary representing actual message
      messagePathPointer - A JSON Pointer for accessing expected message definition within spec
      schemaMap - An optional local Schema registry snapshot for resolving Avro schemas
      Returns:
      The list of validation failures. If empty, avro binary is valid !
    • validateAvroMessage

      public static List<String> validateAvroMessage(com.fasterxml.jackson.databind.JsonNode specificationNode, org.apache.avro.generic.GenericRecord avroRecord, String messagePathPointer, SchemaMap schemaMap)
      Validate an Avro binary representing an AsyncAPI message against a node representing a full AsyncAPI specification (and not just a schema node). Specify the message by providing a valid JSON pointer for messagePathPointer within specification to allow finding the correct schema information. Validation with avro binary is a deep one: each element of the reading schema from AsyncAPI spec is checked in terms of type compatibility, name and required/optional property. It returns a list of validation error messages.
      Parameters:
      specificationNode - The AsyncAPI full specification as a Jackson node
      avroRecord - The avro record representing actual message
      messagePathPointer - A JSON Pointer for accessing expected message definition within spec
      schemaMap - An optional local Schema registry snapshot for resolving Avro schemas
      Returns:
      The list of validation failures. If empty, avro record 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 AsyncAPI schema text. This handles the fact that AsyncAPI spec may be formatted in YAML. In that case, it handles the conversion.
      Parameters:
      schemaText - The JSON or YAML string for AsyncAPI schema
      Returns:
      The Jackson JsonNode corresponding to AsyncAPI schema string
      Throws:
      IOException - if schema string representation cannot be parsed