Skip to content

Validating requests

How-to guideThe steps for one task.

The validate policy checks a request against JSON Schema before the backend sees it: the JSON body, and the query, headers and path parameters. A request that does not match is answered 400 APX-PL-0037 with the violations, where and why, and never reaches the backend. The schemas come from an imported OpenAPI document, or you write them in the policy.

From an OpenAPI document

Import the document as before (Importing an OpenAPI document). The preview marks every operation whose document declares a request body or parameters with validates body, query, …, and the import adds a validate policy to that operation with the schemas of the document: the JSON request body, the path, query and header parameters, with components.schemas references kept as $defs (cycles included) and OpenAPI 3.0's nullable turned into a null type. Remove the policy from an operation in the editor to switch the check off there.

Written by hand

policies:
  - type: validate
    config:
      body:
        type: object
        required: [sku, quantity]
        properties:
          sku: { type: string, minLength: 1 }
          quantity: { type: integer, minimum: 1 }
        additionalProperties: false
      query:
        type: object
        required: [limit]
        properties:
          limit: { type: integer, maximum: 100 }
          tags: { type: array, items: { type: string }, maxItems: 3 }
      headers:
        type: object
        required: [X-Tenant]
        properties:
          X-Tenant: { type: string, pattern: "^[a-z]+$" }
      path:
        type: object
        properties:
          id: { type: integer }
      content_types: [application/json]   # the body schema applies to these media types
      require_body: false                 # true refuses a request without a body
      max_body_bytes: 1MB

In the console the four schemas are JSON code fields; a schema written in YAML in the code tab is the same thing.

Parameters. Query, headers and path are checked as objects whose properties are the parameters. A parameter arrives as text; the policy converts it to the type the schema declares before checking, so limit=10 satisfies type: integer and limit=ten does not. An array property takes a repeated parameter (tags=a&tags=b) or a comma-separated one (tags=a,b). Headers are matched by name as written in the schema, case-insensitively. Query parameters the schema does not name are kept as text, so additionalProperties: false refuses them.

Bodies. Only a body whose media type is in content_types (by prefix; application/json by default) is checked; another type passes untouched, and the trace says body of type … not checked. A body over max_body_bytes is refused with 413; one that is not JSON with 400. The body is read into memory once and forwarded as it came.

What the caller gets. 400 APX-PL-0037 with in (body, query, headers or path) and up to five violations, each a JSON pointer and the rule, such as /quantity: got string, want integer or /: missing property 'sku'. The request inspector shows the same in the policy step.

JSON Schema dialect. Draft 2020-12, with $ref into $defs. Formats (email, uuid, date-time) are annotations, not assertions, as the draft says; use pattern to enforce one.

Cost. A schema is compiled once when published. A request costs the parse of a body the gateway would read anyway and one walk of it: about 120 µs for a 4 KB body of fifty objects, nothing for a request the policy does not apply to. Responses are not validated.