Package io.github.microcks.util.openapi
Class OpenAPISchemaValidator
java.lang.Object
io.github.microcks.util.openapi.OpenAPISchemaValidator
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
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic 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 boolean
isJsonValid
(String schemaText, String jsonText, String namespace) Check if a Json object is valid against the given OpenAPI schema specification.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.validateJson
(String schemaText, String jsonText, String namespace) 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, 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).validateJsonMessage
(com.fasterxml.jackson.databind.JsonNode specificationNode, com.fasterxml.jackson.databind.JsonNode jsonNode, String messagePathPointer, String contentType, String namespace) 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 Details
-
OpenAPISchemaValidator
public OpenAPISchemaValidator()
-
-
Method Details
-
isJsonValid
Check if a Json object is valid against the given OpenAPI schema specification.- Parameters:
schemaText
- The OpenAPI 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
-
isJsonValid
public static boolean isJsonValid(String schemaText, String jsonText, String namespace) throws IOException Check if a Json object is valid against the given OpenAPI schema specification.- Parameters:
schemaText
- The OpenAPI schema specification as a stringjsonText
- The Json object as a stringnamespace
- Namespace definition to resolve relative dependencies in Json schema- 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 OpenAPI schema spec semantics regarding additional or unknown attributes: schema must explicitely setadditionalProperties
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 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(String schemaText, String jsonText, String namespace) 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 setadditionalProperties
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 stringjsonText
- The Json object as a stringnamespace
- Namespace definition to resolve relative dependencies in Json schema- 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 setadditionalProperties
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 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 OpenAPI schema spec semantics regarding additional or unknown attributes: schema must explicitely setadditionalProperties
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 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, 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 formessagePathPointer
within specification and acontentType
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 setadditionalProperties
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 nodejsonNode
- The Json object representing actual message as a Jackson nodemessagePathPointer
- A JSON Pointer for accessing expected message definition within speccontentType
- The Content-Type of the message to valid- 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, String namespace) 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 formessagePathPointer
within specification and acontentType
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 setadditionalProperties
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 nodejsonNode
- The Json object representing actual message as a Jackson nodemessagePathPointer
- A JSON Pointer for accessing expected message definition within speccontentType
- The Content-Type of the message to validnamespace
- Namespace definition to resolve relative dependencies in OpenAPI schema- 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
-