Event Schemas & Validation
Infer Draft-07 JSON Schemas and template variables from sample payloads, manage /v1/schemas contracts, and enforce DISABLED, WARN, or STRICT Ajv validation.
Event Schemas (event_schemas) let workspace operators attach Draft-07 JSON Schema contracts to event names (such as order.paid), extract nested dot-path variables for Handlebars autocomplete and MCP agents, and guard the ingestion pipeline with Ajv + ajv-formats.
Inferring a Schema from a Sample Payload (POST /v1/schemas/infer)
Pass any representative JSON object to POST /v1/schemas/infer (OWNER, ADMIN, or VIEWER) to generate a Draft-07 JSON Schema and a sorted list of dot-path variables without persisting anything:
curl -s -X POST http://localhost:3000/v1/schemas/infer \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "X-Workspace-Id: ${WORKSPACE_ID}" \
-H "Content-Type: application/json" \
-d '{
"samplePayload": {
"orderId": "ord_9001",
"amount": 149.99,
"customer": {
"email": "ava@acme.io"
}
}
}'{
"jsonSchema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"orderId": { "type": "string" },
"amount": { "type": "number" },
"customer": {
"type": "object",
"properties": {
"email": { "type": "string", "format": "email" }
},
"required": ["email"]
}
},
"required": ["orderId", "amount", "customer"]
},
"variables": [
"amount",
"customer",
"customer.email",
"orderId"
]
}Registering & Updating Schemas (/v1/schemas)
POST /v1/schemas upserts a schema by (workspaceId, eventName). If jsonSchema is omitted, KestrelMQ automatically infers it from samplePayload, verifies that Ajv can compile it, and caches compiled validators in memory (500-entry LRU keyed by ${schemaId}:${updatedAt}):
curl -s -X POST http://localhost:3000/v1/schemas \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "X-Workspace-Id: ${WORKSPACE_ID}" \
-H "Content-Type: application/json" \
-d '{
"eventName": "order.paid",
"samplePayload": {
"orderId": "ord_9001",
"amount": 149.99
},
"validationMode": "STRICT"
}'| Method | Path | Roles | Status Codes | Description |
|---|---|---|---|---|
POST | /v1/schemas/infer | OWNER, ADMIN, VIEWER | 200, 400 | Infer Draft-07 JSON Schema + dot-path variables from samplePayload. |
POST | /v1/schemas | OWNER, ADMIN | 201, 400 | Create or upsert schema for eventName (validationMode defaults to DISABLED). |
GET | /v1/schemas | OWNER, ADMIN, VIEWER | 200 | List all workspace schemas ordered by eventName. |
PATCH | /v1/schemas/:id | OWNER, ADMIN | 200, 400, 404 | Update samplePayload, jsonSchema, or validationMode. |
DELETE | /v1/schemas/:id | OWNER, ADMIN | 204, 404 | Remove schema contract for the event. |
Validation Modes (DISABLED, WARN, STRICT)
During POST /v1/events, POST /v1/events/bulk, and MCP send_event, EventSchemasService.validatePayload enforces the configured SchemaValidationMode:
| Mode | Ingestion Outcome on Mismatch | Details |
|---|---|---|
DISABLED (default) | 202 Accepted | Skips Ajv validation completely while still advertising variables to template editors and kestrelmq://schemas. |
WARN | 202 Accepted | Enqueues deliveries normally, but records Ajv issues ({ instancePath, schemaPath, keyword, message }[]) in events.schema_warnings and returns them in schemaWarnings. |
STRICT | 400 Bad Request | Rejects the event before PostgreSQL/BullMQ persistence with RFC 9457 application/problem+json (type: "https://errors.kestrelmq.dev/bad-request", errors: string[]). |
Safe Idempotency-Key recovery in STRICT mode
When STRICT validation rejects an event on POST /v1/events, IngestionService automatically releases the in-flight Redis idempotency:{workspaceId}:{key} lock (compareAndDel) so your service can fix the payload and retry with the same Idempotency-Key. In POST /v1/events/bulk, invalid STRICT rows increment failedRows without aborting valid rows in the CSV stream.