{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://pipelinexlab.com/contracts/document-analysis.v1.schema.json",
  "title": "Immutable PDF source analysis and exact citations",
  "description": "Core additionally validates exact decimal bounds, rectangle ordering/containment, segment uniqueness, language syntax and citation target/order. This schema does not certify source bytes or authorize access.",
  "oneOf": [
    {
      "$ref": "#/$defs/analysis"
    },
    {
      "$ref": "#/$defs/citation"
    }
  ],
  "$defs": {
    "digest": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "key": {
      "type": "string",
      "maxLength": 128,
      "pattern": "^[a-z][a-z0-9]*(?:_[a-z0-9]+)*$"
    },
    "decimal": {
      "type": "string",
      "pattern": "^-?(?:0|[1-9][0-9]*)(?:\\.[0-9]*[1-9])?$",
      "not": {
        "const": "-0"
      },
      "maxLength": 40
    },
    "unit": {
      "type": "string",
      "pattern": "^(?:0(?:\\.[0-9]{0,17}[1-9])?|1)$"
    },
    "box": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "x0",
        "y0",
        "x1",
        "y1"
      ],
      "properties": {
        "x0": {
          "$ref": "#/$defs/decimal"
        },
        "y0": {
          "$ref": "#/$defs/decimal"
        },
        "x1": {
          "$ref": "#/$defs/decimal"
        },
        "y1": {
          "$ref": "#/$defs/decimal"
        }
      }
    },
    "region": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "x0",
        "y0",
        "x1",
        "y1"
      ],
      "properties": {
        "x0": {
          "$ref": "#/$defs/unit"
        },
        "y0": {
          "$ref": "#/$defs/unit"
        },
        "x1": {
          "$ref": "#/$defs/unit"
        },
        "y1": {
          "$ref": "#/$defs/unit"
        }
      }
    },
    "segment": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "key",
        "pageIndex",
        "regions",
        "text",
        "method"
      ],
      "properties": {
        "key": {
          "$ref": "#/$defs/key"
        },
        "pageIndex": {
          "type": "integer",
          "minimum": 0,
          "maximum": 4294967295
        },
        "regions": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/region"
          }
        },
        "text": {
          "type": "string"
        },
        "method": {
          "enum": [
            "text-layer",
            "ocr"
          ]
        },
        "confidence": {
          "anyOf": [
            {
              "type": "null"
            },
            {
              "$ref": "#/$defs/unit"
            }
          ]
        },
        "language": {
          "type": [
            "string",
            "null"
          ],
          "description": "Well-formed RFC 5646 syntax; Core normalizes casing, independently of installed language support."
        }
      }
    },
    "analysis": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schemaVersion",
        "kind",
        "coordinateSpace",
        "source",
        "processor",
        "segments"
      ],
      "properties": {
        "schemaVersion": {
          "const": 1
        },
        "kind": {
          "const": "document-analysis"
        },
        "coordinateSpace": {
          "const": "crop-normalized-top-left-unrotated"
        },
        "source": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "contentRef",
            "size",
            "mediaType",
            "pages"
          ],
          "properties": {
            "contentRef": {
              "$ref": "#/$defs/digest"
            },
            "size": {
              "type": "integer",
              "minimum": 1,
              "maximum": 9007199254740991
            },
            "mediaType": {
              "const": "application/pdf"
            },
            "pages": {
              "type": "array",
              "minItems": 1,
              "items": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "mediaBox",
                  "cropBox",
                  "rotationDegrees"
                ],
                "properties": {
                  "mediaBox": {
                    "$ref": "#/$defs/box"
                  },
                  "cropBox": {
                    "$ref": "#/$defs/box"
                  },
                  "rotationDegrees": {
                    "enum": [
                      0,
                      90,
                      180,
                      270
                    ]
                  }
                }
              }
            }
          }
        },
        "processor": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "engineKey",
            "engineVersion",
            "modelRefs",
            "configurationRef"
          ],
          "properties": {
            "engineKey": {
              "$ref": "#/$defs/key"
            },
            "engineVersion": {
              "type": "string",
              "minLength": 1
            },
            "modelRefs": {
              "type": "array",
              "uniqueItems": true,
              "items": {
                "$ref": "#/$defs/digest"
              }
            },
            "configurationRef": {
              "$ref": "#/$defs/digest"
            }
          }
        },
        "segments": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/segment"
          }
        }
      }
    },
    "citation": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "schemaVersion",
        "kind",
        "analysisRef",
        "sourceContentRef",
        "segmentKeys"
      ],
      "properties": {
        "schemaVersion": {
          "const": 1
        },
        "kind": {
          "const": "document-citation"
        },
        "analysisRef": {
          "$ref": "#/$defs/digest"
        },
        "sourceContentRef": {
          "$ref": "#/$defs/digest"
        },
        "segmentKeys": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "$ref": "#/$defs/key"
          }
        }
      }
    }
  }
}
