Skip to main content

Request validation: JSON Schema and OpenAPI at the gateway

Refuse a malformed request before it reaches the backend: request-validation with a JSON Schema for body and headers, oas-validator with your OpenAPI document.

By Akif F. Güven Updated Security

What you will have at the end

Two Routes that refuse a request the contract does not allow, before the backend sees it. On catalog-api, request-validation holds a JSON Schema for the body and one for the headers: an order without an amount, with an amount written as text, or without the tenant header is answered 400 with a sentence you wrote. On orders-contract, oas-validator reads the OpenAPI document of the orders service: a path the document does not list, a query value outside its enum, a body missing a required property or with a wrong type is answered 400 with the engine’s own sentence naming the property, and a body that matches is created with 201. Every refusal is a request record in Traffic Explorer, answered by the gateway in 0 ms, and both Plugin additions are in the audit log.

Prerequisites

  • A Route that receives JSON. catalog-api forwards /catalog/* to an echo service, so the article can show what the backend received; it is the Route of Caching responses with proxy-cache.
  • A backend with an OpenAPI document at an address the gateway can reach. The orders service of this lab serves its own, openapi-gateway.json: GET /orders with a status query parameter limited to open, shipped and cancelled, POST /orders with a NewOrder body that requires customer and lines, and GET /orders/{orderId}. The Route orders-contract serves /orders and /orders/* for the host orders.lab from that service.
  • Traffic logging turned on under Traffic Explorer › Settings, and a role that may manage Routes. The screenshots below were taken as nora.becker.

Two Plugins, two sources of truth

request-validation takes a JSON Schema you write into the Route, one for the body and one for the headers, and refuses with a status and a message you choose. oas-validator takes the OpenAPI document your API already has, written into the Route or fetched from a URL, and checks path, method, query, headers and body against it. Use the first when the contract lives in the gateway, the second when it lives in the document your Developer Portal publishes.

request-validation on catalog-api

Open the Route, Plugins, Add plugin, request-validation. The form has two editors and the refusal settings; the note on top says at least one of the two schemas is required.

  • Body schema: a JSON Schema the body must match. The article’s one requires customer, a string of at least three characters, and amount, an integer of at least 1, and limits currency to EUR, USD and TRY. The editor checks the schema while you type and says so under it: valid JSON, matches the field’s schema.
  • Header schema, under More: a JSON Schema the headers are read against. The note under it is the one to read before writing one: a header reaches the schema as text, so a property typed as a number never matches, and a header sent twice arrives as a list. The article requires x-tenant with at least three characters.
  • Rejected code: 400. Rejected message: the whole body of the refusal, here The order does not match the contract: customer (3+ characters) and amount (a whole number of 1 or more) are required. The note says what an empty message means: the client gets the validator’s own sentence, which names the properties the schemas carry. One message serves every refusal, the header one included, so write it for both or leave it empty.
  • Request body limit: bytes of the body read into memory before the schema is applied; a longer body is refused with the code above.
  • The banner on top of the form, two settings edited as JSON, names the two schemas: they are the fields the editors draw, not free text.

The request-validation form on catalog-api: the Body schema editor holding the schema with customer, amount and currency, "Valid JSON · matches the field's schema" under it, and the Add plugin button The contract as JSON Schema, checked in the editor before it reaches the engine.

The saved request-validation Plugin opened for editing, scrolled to the Header schema editor holding the x-tenant schema with Valid JSON under it, the note that a header reaches the schema as text, Rejected code 400 and the Rejected message field The header contract and the refusal below it. One code and one message serve every refusal.

Select Add plugin. Five requests, the body of the first one matching:

### valid body, x-tenant: acme
HTTP/1.1 200 OK
backend echoed: {'amount': 1250, 'currency': 'EUR', 'customer': 'ACME GmbH'}
### amount missing
HTTP/1.1 400 Bad Request
The order does not match the contract: customer (3+ characters) and amount (a whole number of 1 or more) are required.
### amount as text, currency not in the list
HTTP/1.1 400 Bad Request
The order does not match the contract: customer (3+ characters) and amount (a whole number of 1 or more) are required.
### not JSON at all
HTTP/1.1 400 Bad Request
The order does not match the contract: customer (3+ characters) and amount (a whole number of 1 or more) are required.
### valid body, no x-tenant header
HTTP/1.1 400 Bad Request
The order does not match the contract: customer (3+ characters) and amount (a whole number of 1 or more) are required.

The backend echoed the one request that passed and never saw the other four.

Traffic Explorer filtered to the path /catalog/orders: four POST records with status 400, 0 ms and gateway as the Upstream node above a 200 record of 49 ms answered by the backend's address Four refusals in 0 ms by the gateway, one request handed to the backend. The Upstream node column tells them apart.

What the audit log keeps

Adding the Plugin is a Route update: who, when, which Route, and the request-validation block as the difference, with the whole body schema, the rejected code and the rejected message on the after side. The Plugin holds no secret, so nothing is masked.

The Change section of the audit record of the catalog-api Route update: the request-validation block added on the after side with the body schema, rejected_msg and rejected_code 400, next to the unchanged data-mask, http-logger, proxy-cache and proxy-rewrite blocks The schema as the engine received it. A later change to a property is a second record with the old and the new schema side by side.

oas-validator on orders-contract

Open the Route, Plugins, Add plugin, oas-validator. Under Basics, choose where the document comes from: Written here keeps the OpenAPI document in the Route’s configuration; Fetched from a URL reads it from an address the gateway can reach. The article fetches it: Document URL http://orders-svc:8080/openapi-gateway.json, the backend’s own copy. Headers sent with the fetch, further down, carries an Authorization for a document behind a login, stored on the engine and shown only to roles that manage the Route; Timeout and SSL verify apply to the fetch alone.

Under Behavior:

  • Refuse a request the document does not allow stays on. The note says what off means: the request is sent to the upstream anyway and the status below is never used, which turns the Plugin into a way of finding out what would be refused before you refuse it.
  • Rejection status code: 400 unless you set another, between 400 and 599.
  • Four Skip switches leave out body, header, query parameter or path parameter validation, all off here.
  • Say why the request was refused: on, so the refusal names the parameter or property that failed; off, it only says the request did not validate.

The saved oas-validator Plugin on orders-contract, opened for editing: Fetched from a URL selected with the Document URL http://orders-svc:8080/openapi-gateway.json?v=2 and the help line that it is cached for the time set in the plugin's metadata, Refuse a request the document does not allow on, Rejection status code 400, and the Skip switches off The document by address. The version query in the URL is explained below.

Select Add plugin. Two things about the document itself were measured on this install, and both matter before the first request:

  • The servers entry decides whether anything matches. The document the backend first served named its own host under servers, and every request was refused with no matching operation found, because the paths of the document were read under that server and the gateway’s requests arrive under another. A document whose servers is [{"url": "/"}], or one that names the gateway’s own address, matches; the copy the article uses says /.
  • The fetched document is cached per worker process, for the time set in the plugin’s metadata, as the field’s help line says. Workers that fetched the first version of the document kept answering from it half an hour after the backend had been corrected, while workers that fetched later held the new one, so the same request was refused by one worker and passed by the next. A new address is a new cache entry: the URL was changed to openapi-gateway.json?v=2, every worker fetched again at once, and from then on every worker answered alike. Change the version in the URL whenever you change the document.

Seven requests to orders.lab:

### GET /orders/ord-10421, a documented path with its path parameter
HTTP/1.1 200 OK
{"id":"ord-10421","customer":"cust-8831","status":"open","total":249.90,"currency":"EUR",…}
### GET /orders?status=open, the documented query parameter
HTTP/1.1 200 OK
[{"id":"ord-10421","customer":"cust-8831","status":"open",…
### GET /orders?status=everything, a value the enum does not allow
HTTP/1.1 400 Bad Request
{"message":"failed to validate request. query parameter 'status': matches none of the enum values"}
### GET /orders/ord-10421/history, a path the document does not list
HTTP/1.1 400 Bad Request
{"message":"failed to validate request. no matching operation found for GET /orders/ord-10421/history"}
### POST /orders without lines, which NewOrder requires
HTTP/1.1 400 Bad Request
{"message":"failed to validate request. body: property \"lines\" is required"}
### POST /orders with quantity as text
HTTP/1.1 400 Bad Request
{"message":"failed to validate request. body: property \"lines\" validation failed: failed to validate item 1: property \"quantity\" validation failed: wrong type: expected integer, got string"}
### POST /orders that matches NewOrder
HTTP/1.1 201 Created
{"id":"ord-10422","customer":"","status":"open","total":0,"currency":"EUR","created_at":"2026-10-13T09:12:00Z"}

Each refusal names what failed, because Say why is on; with it off, a client gets the status and a sentence that says only that the request did not validate, which is the setting for an API whose document is not public. A DELETE on the same path was answered 404 by the gateway before any Plugin ran, because the Route lists GET and POST only; a method the Route does not carry never reaches the document.

Traffic Explorer filtered to the Route orders-contract: a 201 POST /orders answered by the backend's address, two 400 POST /orders, a 400 GET /orders/ord-10421/history and a 400 GET /orders?status=everything all answered by gateway in 0 ms, and 200 records for /orders?status=open and /orders/ord-10421 with the backend's address The whole proof set as records: four refusals by the gateway in 0 ms, three requests handed to the backend.

Adding oas-validator and changing its URL are Route updates in the audit log, each with the Plugin block as the difference, like the record above.

What to do next

Written by
Akif F. Güven
Engineer, G8
Was this helpful?
Next step

See G8 in a demo

We’ll show you the console with APIs and AI traffic like yours, and answer the questions these articles leave open.

  1. 01Demo
  2. 02Proof of conceptOn Docker for a quick start, or on the infrastructure you plan to run G8 on.
  3. 03LicenseWe prepare a quote for your edition and nodes, and you receive a signed license file.