Document updated on Oct 25, 2022
Validating the responses with JSON Schema
The response schema validator plugin adds a schema validation before the gateway returns the response to the end-user or before it’s merged in the endpoint with the rest of the backends.
Before returning the response, you can define the minimum response fields and their characteristics through JSON schema syntax (drafts 04, 06, and 07 supported).
To validate requests using JSON schema see validation/json-schema
instead.
You can use this plugin in conjunction with other components and perform validations, and you can insert it either in the endpoint section or the backend section. For instance, it can be part of a sequential proxy call and decide that the response does not have enough data to move forward and cancel the cascading connection.
Response validation configuration
The configuration of the plugin is as follows:
{
"extra_config": {
"plugin/req-resp-modifier": {
"name": [
"response-schema-validator"
],
"response-schema-validator": {
"schema": {},
"error": {
"body": "",
"status": 500
}
}
}
}
}
Fields of Response Schema Validator
Response validation example
For instance, the schema example below makes sure that the response from the backend/endpoint merge contains:
{
"user": {
"username": "john",
"user_id": 178989,
"status": "registered"
}
}
The configuration:
{
"extra_config": {
"plugin/req-resp-modifier": {
"name": [
"response-schema-validator"
],
"response-schema-validator": {
"schema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"user": {
"type": "object",
"properties": {
"username": {
"type": "string"
},
"user_id": {
"type": "number"
},
"status": {
"type": "string",
"enum": ["registered", "banned"]
}
},
"required": [
"username",
"user_id"
]
}
},
"required": [
"user"
]
},
"error": {
"body": "We couldn't process you request, try again later.",
"status": 401
}
}
}
}
}