Docs / Core Concepts

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.

Updated 2026-11-28~4 min readAjv Contracts

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:

infer-schema.sh
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"
      }
    }
  }'
200 OK · InferSchemaResponse
{
  "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}):

upsert-schema.sh
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"
  }'
MethodPathRolesStatus CodesDescription
POST/v1/schemas/inferOWNER, ADMIN, VIEWER200, 400Infer Draft-07 JSON Schema + dot-path variables from samplePayload.
POST/v1/schemasOWNER, ADMIN201, 400Create or upsert schema for eventName (validationMode defaults to DISABLED).
GET/v1/schemasOWNER, ADMIN, VIEWER200List all workspace schemas ordered by eventName.
PATCH/v1/schemas/:idOWNER, ADMIN200, 400, 404Update samplePayload, jsonSchema, or validationMode.
DELETE/v1/schemas/:idOWNER, ADMIN204, 404Remove 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:

ModeIngestion Outcome on MismatchDetails
DISABLED (default)202 AcceptedSkips Ajv validation completely while still advertising variables to template editors and kestrelmq://schemas.
WARN202 AcceptedEnqueues deliveries normally, but records Ajv issues ({ instancePath, schemaPath, keyword, message }[]) in events.schema_warnings and returns them in schemaWarnings.
STRICT400 Bad RequestRejects 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.