HTTP 422 Unprocessable Entity
error. This error code can be customized if required.
When using Tyk OAS APIs, request validation is performed by the Validate Request
middleware which can be enabled per-endpoint. The schema against which requests are compared is defined in the OpenAPI description of the endpoint. All elements of the request can have a schema
defined in the OpenAPI description so requests to Tyk OAS APIs can be validated for headers, path/query parameters and body (payload).
When using the legacy Tyk Classic APIs, request validation is performed by the Validate JSON
middleware which can be enabled per-endpoint. The schema against which requests are compared is defined in the middleware configuration and is limited to the request body (payload). Request headers and path/query parameters cannot be validated when using Tyk Classic APIs.
If you’re using Tyk OAS APIs, then you can find details and examples of how to configure the request validation middleware here.
If you’re using Tyk Classic APIs, then you can find details and examples of how to configure the request validation middleware here.
HTTP 422 Unprocessable Entity
response. Tyk can be configured to return a different HTTP status code if required.
The middleware is configured in the Tyk OAS API Definition. You can do this via the Tyk Dashboard API or in the API Designer.
If you’re using the legacy Tyk Classic APIs, then check out the Tyk Classic page.
parameters
field in the OpenAPI description is an array of parameter objects that each describe one variable element in the request. Each parameter
has two mandatory fields:
in
: the location of the parameter (path
, query
, header
)name
: a unique identifier within that location (i.e. no duplicate header names for a given operation/endpoint)description
and required
fields.
For each parameter, a schema can be declared that defines the type
of data that can be stored (e.g. boolean
, string
) and any example
or default
values.
GET /users
. Operation, or endpoint-level parameters can be defined in the OpenAPI description and will apply only to that operation within the API. These can be added or modified within Tyk Dashboard’s API designer.
requestBody
field in the OpenAPI description is a Request Body Object. This has two optional fields (description
and required
) plus the content
section which allows you to define a schema for the expected payload. Different schemas can be declared for different media types that are identified by content-type (e.g. application/json
, application/xml
and text/plain
).
HTTP 422 Unprocessable Entity
unless otherwise configured.
POST /apis/oas/import
endpoint in the Tyk Dashboard API or Tyk Gateway API then you can do this by setting the validateRequest=true
query parameteroperationId
defined in the OpenAPI Document that declares both the path and method for which the middleware should be added. Endpoint paths
entries (and the associated operationId
) can contain wildcards in the form of any string bracketed by curly braces, for example /status/{code}
. These wildcards are so they are human readable and do not translate to variable names. Under the hood, a wildcard translates to the “match everything” regex of: (.*)
.
The request validation middleware (validateRequest
) can be added to the operations
section of the Tyk OAS Extension (x-tyk-api-gateway
) in your Tyk OAS API Definition for the appropriate operationId
. The operationId
for an endpoint can be found within the paths
section of your OpenAPI specification.
The validateRequest
object has the following configuration:
enabled
: enable the middleware for the endpointerrorResponseCode
: [optional] the HTTP status code to be returned if validation fails (this defaults to HTTP 422 Unprocessable Entity
if not set)GET /anything
endpoint. The middleware will check for the existence of a header named X-Security
and the request body will be validated against the declared schema. If there is no match, the request will be rejected and Tyk will return HTTP 400
(as configured in errorResponseCode
).
The configuration above is a complete and valid Tyk OAS API Definition that you can import into Tyk to try out the request validation middleware.
Validate JSON
middleware which can be enabled per-endpoint. The schema against which requests are compared is defined in the middleware configuration and is limited to the request body (payload). Request headers and path/query parameters cannot be validated when using Tyk Classic APIs.
This middleware is configured in the Tyk Classic API Definition. You can do this via the Tyk Dashboard API or in the API Designer.
If you’re using the newer Tyk OAS APIs, then check out the Tyk OAS page.
If you’re using Tyk Operator then check out the configuring the middleware in Tyk Operator section below.
validate_json
object to the extended_paths
section of your API definition.
The validate_json
object has the following configuration:
path
: the endpoint pathmethod
: the endpoint HTTP methodschema
: the JSON schema against which the request body will be comparederror_response_code
: the HTTP status code that will be returned if validation fails (defaults to 422 Unprocessable Entity
)POST /register
endpoint. For any call made to this endpoint, Tyk will compare the request body with the schema and, if it does not match, the request will be rejected with the error code HTTP 422 Unprocessable Entity
.
$schema
field in your schema definition. This field specifies the version of the JSON schema standard to be followed.
From Tyk 5.8 onwards, supported versions are draft-04, draft-06 and draft-07.
In previous versions of Tyk, only draft-04 is supported. Please be careful if downgrading from Tyk 5.8 to an earlier version that your JSON is valid as you might experience unexpected behaviour if using features from newer drafts of the JSON schema.
$schema
field is present, the Gateway strictly follows the rules of the specified version.$schema
field is missing or the version is not specified, the Gateway uses a hybrid mode that combines features from multiple schema versions. This mode ensures that the validation will still work, but may not enforce the exact rules of a specific version.$schema
field in your schema definition. For example:
$schema
, the validator can operate in strict mode, ensuring that the rules for your chosen schema version are followed exactly.
422 Unprocessable Entity
) and enter your JSON schema in the editor.
validate_json
object to the extended_paths
section of your API definition, for example:
The example API Definition below configures an API to listen on path /httpbin
and forwards requests upstream to http://httpbin.org.
In this example, the Validate JSON middleware has been configured for requests to the GET /get
endpoint. For any call made to this endpoint, Tyk will compare the request body with the schema and, if it does not match, the request will be rejected with the error code HTTP 422 Unprocessable Entity
.