JSON Validation#
JSON validation schemes are used to validate incoming requests to the webhook server. Validation schemes are drawn up according to the API methods and check incoming webhooks for the presence of required fields and their types. Validation schemes are updated as the API evolves and new webhooks are developed. You can also expand the list of validation schemes if needed.
JSON schemas for webhooks validation are placed in jsonSchema
directory and copied to build directory while running build script. You could add any .json
files to build/bin/jsonSchema
, they will be loaded into a program while it starting.
JSON schemas have this structure:
{
"$id": "schemas",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"yourNameOfObject": {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"sampleField": {
"type": "string"
},
"sampleRef_Field": {
"$ref": "#/properties/commonSchemaComponents/properties/senderData"
}
},
"required": [
"typeWebhook",
],
"additionalProperties": true
},
"yourOtherObject": {
...
}
}
}
To make your .json
file working, any .json
file should contain only one object with "properties"
. All to-be-validated objects should be on "properties"
object. Otherwise, your json file will be ignored.
When the program starts, it combines all JSON files from the jsonSchema
directory into one object, combining the properties
structure (shown above) with the properties
fields of all files from the directory.
For webhook validation, JSON is compared as follows:
-
Get JSON from the request body
{...}
; -
Create a new JSON object of type
"Name of the webhook to check" = {<JSON from the received request body>}
; -
Validate the received JSON with the created JSON.
Example:
1.Webhook validation with type incomingMessageReceived
is specified in file jsonSchema/incomingMessage.json
:
{
"$id": "schemas",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"incomingMessageReceived": {
...
}
}
2.The server receives a webhook with the type incomingMessageReceived
:
{
"typeWebhook": "incomingMessageReceived",
...
}
3.The validator creates a new JSON object, naming it based on the type of webhook received and filling it with the data received in the request body:
"incomingMessageReceived": {
{
"typeWebhook": "incomingMessageReceived",
...
}
}
4.The validator compares the generated JSON with all JSON from jsonSchema
.