News New Look, Same Vision: KrakenD’s Website Redesign

Document updated on Oct 25, 2022

Response Schema Validation for API Endpoints

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.

Plugins require a plugin section
As all functionality delivered as a plugin, in addition to the response schema validator section, you need to add the plugin section at the service level.

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
* required fields

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.

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.

See all support channels