Prompt
How do I validate API schemas and block unexpected requests?
Latest observation
Oct 29, 2026
OpenAI APIWeb search: off
To validate API schemas and block unexpected requests, you typically want request validation at your API boundary plus strict schema enforcement.
Core approach
1) Define an explicit schema
Use a schema format such as:
- OpenAPI for REST APIs
- JSON Schema for request/response bodies
- GraphQL schema for GraphQL APIs
- Protobuf / Avro for RPC/event-based APIs
Your schema should specify:
- required fields
- types
- enums
- formats/patterns
- min/max lengths and ranges
- allowed additional properties: ideally
falsefor strictness
Example JSON Schema idea:
{
"type": "object",
"required": ["name", "email"],
"properties": {
"name": { "type": "string", "minLength": 1 },
"email": { "type": "string", "format": "email" }
},
"additionalProperties": false
}
2) Validate at the API gateway or server middleware
Block requests before business logic runs.
Common places:
- API gateway: Kong, Apigee, AWS API Gateway, NGINX with validation plugins
- Backend middleware: Express, FastAPI, Spring, Django, etc.
- Service mesh / proxy: Envoy, Istio with ext_authz/WASM filters
Validation should cover:
- URL/path params
- query params
- headers
- request body
- content type
- size limits
3) Reject unknown fields and invalid types
To block unexpected requests:
- set
additionalProperties: false - reject unknown query/body parameters
- fail on type coercion when possible
- require exact enum values
- validate nested objects recursively
This helps prevent:
- accidental client bugs
- mass assignment vulnerabilities
- hidden/unexpected input being processed
4) Validate content type and method
Only accept what you expect:
Content-Type: application/json- correct HTTP methods per route
- reject
text/plain,multipart/form-data, etc. unless needed
5) Enforce strict response schemas too
Validate responses as well, especially in microservices or public APIs:
- catches accidental contract drift
- prevents leaking fields
- supports consumer-driven contracts
6) Log and monitor rejections
When rejecting requests:
- return
400 Bad Requestor422 Unprocessable Entity - log schema violations
- include route, validation error, and request metadata
- rate-limit repeated invalid attempts if needed
7) Use tooling
Depending on your stack:
Node.js
ajvzodjoiexpress-openapi-validator
Python
pydantic/ FastAPI validationjsonschema
Java
- Bean Validation (
jakarta.validation) - Spring
@Valid - OpenAPI validators
Go
go-playground/validator- OpenAPI middleware
API gateways
- Kong schema validation plugins
- AWS API Gateway models/validators
- Apigee policies
8) Prefer “deny by default”
A good rule:
- only accept documented endpoints
- only accept documented fields
- only accept documented methods
- reject everything else
Example: express + JSON Schema
import express from "express";
import Ajv from "ajv";
const app = express();
app.use(express.json());
const ajv = new Ajv({ allErrors: true, removeAdditional: false });
const schema = {
type: "object",
required: ["name", "email"],
additionalProperties: false,
properties: {
name: { type: "string" },
email: { type: "string", format: "email" }
}
};
const validate = ajv.compile(schema);
app.post("/users", (req, res) => {
const valid = validate(req.body);
if (!valid) {
return res.status(400).json({ errors: validate.errors });
}
res.json({ ok: true });
});
Best practice checklist
- Define schemas for every route
- Reject unknown fields
- Validate all input sources
- Enforce content type and size limits
- Validate before business logic
- Return clear 4xx errors
- Log violations
- Keep schemas versioned and tested
If you want, I can show you how to do this in a specific framework like FastAPI, Express, Spring Boot, or AWS API Gateway.