News KrakenD 2023: Milestones and New Horizons

Enterprise Documentation

Recent changes

You are viewing a previous version of KrakenD Enterprise Edition (v2.3) , go to the latest version

Validating the responses with JSON Schema

Document updated on Oct 25, 2022

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
        }
      }
    }
  }
}
error

object
In case the validation fails, the error definition containing body and status.
Example: {"body":"We couldn't process you request, try again later.","status":401}
body

string
The error message you want to show when the validation fails. Set it to an empty string "" to show the JSON-schema validation error.
Defaults to ""
status

integer
The HTTP status code you want to set back in the response.
Defaults to 500
schema  *

object
Write your JSON schema directly in this field, with any number of fields or validations you need.

Schema: https://www.krakend.io/schema/v2.3/plugin/response-schema-validator.json
* indicates a required field.

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
        }
      }
    }
  }
}
Scarf

Unresolved issues?

The documentation is only a piece of the help you can get! Whether you are looking for Open Source or Enterprise support, see more support channels that can help you.

We use cookies to understand how you use our site and to improve your overall experience. By continuing to use our site, you accept our Privacy Policy. More information