Package io.github.microcks.util.asyncapi
Class AsyncAPISchemaValidator
java.lang.Object
io.github.microcks.util.asyncapi.AsyncAPISchemaValidator
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 -
Method Summary
Modifier and TypeMethodDescriptionstatic com.fasterxml.jackson.databind.JsonNodegetJsonNode(String jsonText) Get a Jackson JsonNode representation for Json object.static com.fasterxml.jackson.databind.JsonNodegetJsonNodeForSchema(String schemaText) Get a Jackson JsonNode representation for AsyncAPI schema text.static booleanisJsonValid(String schemaText, String jsonText) Check if a Json object is valid against the given AsyncAPI schema specification.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).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).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.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.validateJson(String schemaText, String jsonText) Validate a Json object representing by its text against a schema object representing byt its text too.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).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).
-
Field Details
-
ASYNC_SCHEMA_PAYLOAD_ELEMENT
- See Also:
-
-
Method Details
-
isJsonValid
Check if a Json object is valid against the given AsyncAPI schema specification.- Parameters:
schemaText- The AsyncAPI schema specification as a stringjsonText- The Json object as a string- Returns:
- True if Json object is valid, false otherwise
- Throws:
IOException- if string representations cannot be parsed
-
validateJson
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 setadditionalPropertiesto 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 stringjsonText- 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 setadditionalPropertiesto 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 nodejsonNode- 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 setadditionalPropertiesto 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 nodejsonNode- The Json object as a Jackson nodenamespace- 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 formessagePathPointerto 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 setadditionalPropertiesto 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 nodejsonNode- The Json object representing actual message as a Jackson nodemessagePathPointer- 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 formessagePathPointerto 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 setadditionalPropertiesto 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 nodejsonNode- The Json object representing actual message as a Jackson nodemessagePathPointer- A JSON Pointer for accessing expected message definition within specnamespace- 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 formessagePathPointerwithin 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 nodeavroBinary- The avro binary representing actual messagemessagePathPointer- A JSON Pointer for accessing expected message definition within specschemaMap- 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 formessagePathPointerwithin 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 nodeavroRecord- The avro record representing actual messagemessagePathPointer- A JSON Pointer for accessing expected message definition within specschemaMap- 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
-