# Validate API requests with JSON Schema at the edge

Gregale · Updated 2026-12-03

Roll out API request validation in observe, warn and block modes. Test malformed and valid JSON against one route on Gregale.

**Start validating API requests at the edge in observe mode, then warn, then block.** Gregale lets you try a JSON Schema on a single route before it starts rejecting existing clients. A schema-valid request still goes to the application; an invalid one in block mode gets a 422 with validation details.

| Mode      | What a client sending an invalid JSON body sees                                 | When to use it                                                         |
| --------- | ------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
| `observe` | The request still goes to the app. Gregale counts the validation failure.       | Check how current clients behave before enforcing a contract.          |
| `warn`    | The request still goes to the app, and the response has `X-Validation-Warning`. | Give clients a way to spot calls that will fail after rollout.         |
| `block`   | The gateway answers HTTP 422 `request_validation_failed`.                       | Enforce the contract after reviewing and correcting incoming requests. |

## Put one schema on one route

Our synthetic `POST /api/quotes` accepts a JSON object containing a positive integer quantity. It does not create records, make a real quote, or charge anyone.

```json
{
  "type": "object",
  "properties": { "quantity": { "type": "integer", "minimum": 1 } },
  "required": ["quantity"],
  "additionalProperties": false
}
```

Create a [validate rule](https://gregale.dev/docs/edge-rules.md#validate) for the intended hostname, path, and HTTP method. Set `validate_mode` to `observe`, put the schema in `action.schema`, and declare the content types the route accepts. Start with a narrow route instead of applying one schema across an entire API. A request with a content type outside the rule's list can get HTTP 415; test legitimate media types separately rather than assuming observe mode makes a new content-type restriction harmless.

## See what changes before you enforce it

[Download the stateless Python starter](https://gregale.dev/examples/edge-json-validation.zip). Use Bash, curl, jq, OpenSSL, Python 3, and the [Gregale CLI](https://gregale.dev/docs/install.md). In a disposable workspace, export a `GREGALE_TOKEN` with permission to create, deploy and delete the test app and manage its edge rules, then run this from the extracted directory:

```bash
bash run-demo.sh
```

The starter deploys one public synthetic echo app, creates an observe rule, and sends `{"quantity":"two"}`. It then changes the same rule to warn and to block, keeping the same route and schema. In observe and warn, the response identifies the app and echoes what reached it. In block, the gateway should return `request_validation_failed` and an error for the failed field instead of the app's response. The final `{"quantity":2}` request must still get an app-owned 200. Cleanup attempts to remove only the disposable app whose ID the script verified.

The [rule-write API](https://gregale.dev/docs/edge-rules.md#changing-rules) waits for serving gateways to adopt a change before it returns success. On a real API, review actual requests in observe mode, notify clients if you control them, and confirm valid clients still work before switching to block. If you need to stop rejecting a legitimate body, change that rule back to warn while you fix the schema or client.

## Keep business validation in the app

JSON Schema checks the shape of the request, not whether the caller can access an order, whether stock is available, or whether a price is up to date. Keep those checks and any non-edge entry points protected in the application. This guide is about runtime request validation, not checking whether a new OpenAPI document breaks existing clients; see [Gregale's OpenAPI deploy gate](https://gregale.dev/articles/openapi-breaking-change-deploy-gate) for that.

See also [edge rules and trace testing](https://gregale.dev/articles/edge-rules-rate-limiting-trace-testing) and the [edge rules API](https://gregale.dev/docs/api/edge-rules).

## Questions

### How do I add JSON Schema request validation without breaking existing clients?

Begin with an observe rule on one route and review existing traffic. Use warn to add a response header, then block after valid clients are verified.

### What happens when a request fails edge validation?

Observe and warn let the request reach the app, with a warning header in warn mode. Block returns HTTP 422 with request validation errors.

### Does edge JSON Schema validation replace application checks?

No. Keep authorization, business rules and validation for entry points not covered by the edge rule in the application.

Canonical URL: https://gregale.dev/articles/validate-api-requests-with-json-schema-at-the-edge

