{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://context-object-spec.github.io/specification/schema/v0.2/context-object.schema.json",
  "title": "Context Object Specification v0.2",
  "type": "object",
  "additionalProperties": false,
  "required": ["version", "source", "selection", "context"],
  "properties": {
    "version": { "const": "0.2" },
    "source": { "$ref": "#/$defs/source" },
    "selection": { "$ref": "#/$defs/selection" },
    "context": { "$ref": "#/$defs/context" },
    "meta": { "$ref": "#/$defs/meta" },
    "extensions": { "type": "array", "items": { "$ref": "#/$defs/extension" } }
  },
  "$defs": {
    "source": {
      "type": "object", "additionalProperties": false, "required": ["type"],
      "properties": {
        "type": { "enum": ["webpage", "pdf", "markdown", "editor", "richtext", "unknown"], "description": "Unlisted source environments use unknown and an owner-qualified Extension." },
        "id": { "type": "string" }, "uri": { "type": "string" }, "title": { "type": "string" },
        "app": { "type": "string" }, "language": { "type": "string" }
      }
    },
    "selection": {
      "type": "object", "additionalProperties": false, "required": ["text"],
      "properties": {
        "text": { "type": "string", "minLength": 1 },
        "range": {
          "type": "object", "additionalProperties": false, "required": ["startOffset", "endOffset"],
          "properties": {
            "startOffset": { "type": "integer", "minimum": 0 }, "endOffset": { "type": "integer", "minimum": 0 },
            "startNodeId": { "type": "string" }, "endNodeId": { "type": "string" },
            "direction": { "enum": ["forward", "backward", "unknown"] }
          }
        }
      }
    },
    "relation": {
      "type": "object", "additionalProperties": false, "required": ["type"],
      "properties": {
        "type": { "enum": ["container", "section", "row_header", "column_header", "label", "list_item"] },
        "label": { "type": "string" }, "value": { "type": "string" }, "targetId": { "type": "string" }
      }
    },
    "segment": {
      "type": "object", "additionalProperties": false, "required": ["type", "text"],
      "properties": {
        "id": { "type": "string" },
        "type": { "enum": ["text", "paragraph", "heading", "list", "table", "code", "quote", "formula", "unknown"] },
        "text": { "type": "string", "minLength": 1 }, "selectedText": { "type": "string", "minLength": 1 },
        "beforeText": { "type": "string" }, "afterText": { "type": "string" },
        "role": { "enum": ["definition", "reference", "output", "code_snippet"] },
        "relations": { "type": "array", "items": { "$ref": "#/$defs/relation" } }
      }
    },
    "context": {
      "type": "object", "additionalProperties": false, "required": ["scope", "segments"],
      "properties": {
        "scope": { "enum": ["inline", "container", "multi-container"] },
        "segments": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/segment" } },
        "relations": { "type": "array", "items": { "$ref": "#/$defs/relation" } }
      }
    },
    "meta": {
      "type": "object", "additionalProperties": false,
      "properties": {
        "createdAt": { "type": "integer", "minimum": 0, "description": "Milliseconds since the Unix epoch." }, "adapter": { "type": "string" },
        "stages": { "type": "array", "items": { "type": "string" }, "uniqueItems": true }
      }
    },
    "extension": {
      "type": "object", "additionalProperties": false, "required": ["namespace", "version", "payload"],
      "properties": {
        "namespace": { "type": "string", "pattern": "^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\\.[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)+$" },
        "version": { "type": "string", "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(?:-[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?(?:\\+[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?$" },
        "payload": { "type": "object" }
      }
    }
  }
}
