{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://trooth.co/schemas/network-changes.v1.schema.json",
  "title": "Trooth Network change feed page, version 1",
  "description": "A page of change events.",
  "type": "object",
  "required": [
    "format",
    "events",
    "next",
    "has_more",
    "stream",
    "guarantees"
  ],
  "additionalProperties": false,
  "properties": {
    "format": {
      "const": "trooth.network-changes.v1",
      "description": "Names this shape."
    },
    "events": {
      "type": "array",
      "description": "The events, oldest first.",
      "items": {
        "$ref": "#/$defs/event"
      }
    },
    "next": {
      "type": "string",
      "minLength": 1,
      "description": "Send as after to continue. Also the id of the last event, so the stream can resume from it."
    },
    "has_more": {
      "type": "boolean",
      "description": "True when more events follow at once."
    },
    "stream": {
      "type": "string",
      "description": "The Server-Sent Events address for the same feed."
    },
    "guarantees": {
      "type": "object",
      "description": "What the feed promises and does not.",
      "required": [
        "order",
        "delivery",
        "withdrawal",
        "retention_days",
        "body"
      ],
      "additionalProperties": false,
      "properties": {
        "order": {
          "type": "string",
          "description": "Ordering rule."
        },
        "delivery": {
          "type": "string",
          "description": "Delivery rule."
        },
        "withdrawal": {
          "type": "string",
          "description": "What a withdrawal does."
        },
        "retention_days": {
          "type": "integer",
          "description": "Days of history kept.",
          "minimum": 0
        },
        "body": {
          "type": "string",
          "description": "What an event leaves out."
        }
      }
    }
  },
  "$defs": {
    "event": {
      "type": "object",
      "description": "One recorded version of one company's published record.",
      "required": [
        "id",
        "type",
        "event_id",
        "kind",
        "subject",
        "version",
        "recorded_at",
        "digest",
        "previous_digest",
        "corrects_version",
        "record_url",
        "history_url"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1,
          "description": "The event's cursor: send as after, or as Last-Event-ID to the stream."
        },
        "type": {
          "const": "record.version",
          "description": "The event type."
        },
        "event_id": {
          "type": "string",
          "pattern": "^rv_[0-9a-f]{32}$",
          "description": "The version's event identity; drop repeats by it."
        },
        "kind": {
          "enum": [
            "published",
            "withdrawn"
          ],
          "description": "What happened to the record."
        },
        "subject": {
          "type": "object",
          "description": "The company.",
          "required": [
            "slug",
            "domain"
          ],
          "additionalProperties": false,
          "properties": {
            "slug": {
              "type": "string",
              "description": "Profile slug."
            },
            "domain": {
              "description": "Domain, or null.",
              "oneOf": [
                {
                  "type": "null"
                },
                {
                  "type": "string",
                  "description": "Domain."
                }
              ]
            }
          }
        },
        "version": {
          "type": "integer",
          "description": "The record version, per company.",
          "minimum": 1
        },
        "recorded_at": {
          "type": "string",
          "description": "When the version was recorded, ISO 8601."
        },
        "digest": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$",
          "description": "A SHA-256 digest, 64 lowercase hex characters."
        },
        "previous_digest": {
          "description": "The previous version's digest, or null for the first.",
          "oneOf": [
            {
              "type": "null"
            },
            {
              "type": "string",
              "pattern": "^[0-9a-f]{64}$",
              "description": "A SHA-256 digest, 64 lowercase hex characters."
            }
          ]
        },
        "corrects_version": {
          "description": "The version this one corrects, or null.",
          "oneOf": [
            {
              "type": "null"
            },
            {
              "type": "integer",
              "description": "Version.",
              "minimum": 1
            }
          ]
        },
        "record_url": {
          "type": "string",
          "description": "Where to read the record now."
        },
        "history_url": {
          "type": "string",
          "description": "The company's own change feed."
        }
      }
    }
  }
}
