{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://kaladan.dev/contracts/webhooks/v1/catalog.schema.json",
  "title": "Webhook topic catalogue",
  "description": "Every topic an app can subscribe to. Machine-readable so a developer declaring subscriptions chooses from a list, a coding agent generates a handler against a known shape, and a topic added without its metadata fails the check rather than shipping undocumented.",
  "type": "object",
  "properties": {
    "version": {
      "type": "string",
      "pattern": "^\\d{4}-\\d{2}$"
    },
    "topics": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/Topic"
      }
    }
  },
  "required": [
    "version",
    "topics"
  ],
  "additionalProperties": false,
  "$defs": {
    "Topic": {
      "type": "object",
      "properties": {
        "topic": {
          "type": "string",
          "pattern": "^[a-z_]+/[a-z_]+$",
          "description": "`resource/action`. Permanent once published: it appears in every app's declaration."
        },
        "description": {
          "type": "string",
          "minLength": 10
        },
        "requiredScope": {
          "type": [
            "string",
            "null"
          ],
          "description": "The scope an app must hold to subscribe, from the shared vocabulary. Null only for the compliance topics, which every app receives regardless of what it can read."
        },
        "mandatory": {
          "type": "boolean",
          "description": "Whether every listed app must subscribe. True for the three compliance topics, which cannot be unsubscribed while an app is installed: an app that cannot honour an erasure request must not hold merchant data at all."
        },
        "synchronous": {
          "enum": [
            "NEVER",
            "OPTIONAL",
            "ALWAYS"
          ],
          "description": "Whether the platform can wait for the app's answer and use it. `OPTIONAL` topics may be subscribed either way, and the two carry opposite guarantees — see delivery.md."
        },
        "failureMode": {
          "enum": [
            "FAIL_OPEN",
            "FAIL_CLOSED",
            null
          ],
          "description": "What happens when a synchronous handler times out or answers invalidly. Stated per topic because neither is safe in general: a pricing hook that fails open sells at the wrong price, and a validation hook that fails closed stops a shop selling when one app is slow. Null for topics that are never synchronous."
        },
        "timeoutMs": {
          "type": [
            "integer",
            "null"
          ],
          "minimum": 100,
          "maximum": 3000,
          "description": "How long a synchronous handler has. Short, because a buyer is waiting."
        },
        "payloadFields": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/PayloadField"
          },
          "description": "Every field the payload can carry, each with the scope that gates it. A subscription asking for a field beyond the app's scopes is refused at submission rather than trimmed at delivery."
        },
        "responseSchema": {
          "description": "What a synchronous handler must answer with. Absent for asynchronous-only topics."
        },
        "since": {
          "type": "string",
          "pattern": "^\\d{4}-\\d{2}$",
          "description": "The API version this topic appeared in."
        },
        "deprecatedSince": {
          "type": [
            "string",
            "null"
          ]
        },
        "replacedBy": {
          "type": [
            "string",
            "null"
          ]
        },
        "offered": {
          "type": "boolean",
          "description": "False for a topic the catalogue publishes and the platform does not send yet: a version declaring it is refused, and it is listed so a developer can plan against it. Absent means offered."
        },
        "chainTimeoutMs": {
          "type": [
            "integer",
            "null"
          ],
          "description": "Total milliseconds allowed for ALL subscribed apps on a synchronous topic, not per app. Stacked per-app timeouts are how a checkout with three apps installed becomes seconds slower than one with none."
        }
      },
      "required": [
        "topic",
        "description",
        "requiredScope",
        "mandatory",
        "synchronous",
        "failureMode",
        "payloadFields",
        "since"
      ],
      "additionalProperties": false
    },
    "PayloadField": {
      "type": "object",
      "properties": {
        "path": {
          "type": "string",
          "description": "Dotted path within `payload` — `order.id`, `customer.phone`."
        },
        "type": {
          "enum": [
            "string",
            "integer",
            "number",
            "boolean",
            "object",
            "array",
            "money",
            "timestamp",
            "id"
          ]
        },
        "scope": {
          "type": [
            "string",
            "null"
          ],
          "description": "The scope gating this field. Null means every subscriber gets it. A field the app cannot read is absent from the payload, not null."
        },
        "always": {
          "type": "boolean",
          "description": "Whether it is sent even when not named in `includeFields`. True for identifiers, which are what a thin payload is for."
        },
        "description": {
          "type": "string"
        }
      },
      "required": [
        "path",
        "type",
        "scope",
        "always"
      ],
      "additionalProperties": false
    }
  }
}
