{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:openbooks:schema:v1:output",
  "title": "OpenBooksDocument",
  "type": "object",
  "required": [
    "schema_version",
    "status",
    "file",
    "data",
    "evidence",
    "assertions"
  ],
  "properties": {
    "schema_version": {
      "const": "openbooks.v1"
    },
    "status": {
      "const": "ok"
    },
    "file": {
      "type": "object",
      "required": [
        "sha256",
        "name",
        "detected_format",
        "layout_version",
        "method"
      ],
      "properties": {
        "sha256": {
          "type": "string",
          "pattern": "^[0-9a-f]{64}$"
        },
        "model": {
          "type": "object",
          "required": [
            "weights_sha256",
            "calibration_sha256"
          ],
          "additionalProperties": false,
          "properties": {
            "weights_sha256": {
              "type": "string",
              "pattern": "^[0-9a-f]{64}$"
            },
            "calibration_sha256": {
              "type": "string",
              "pattern": "^[0-9a-f]{64}$"
            }
          }
        },
        "name": {
          "type": "string"
        },
        "detected_format": {
          "type": "string"
        },
        "layout_version": {
          "type": "string"
        },
        "method": {
          "type": "string"
        }
      }
    },
    "data": {
      "type": "object",
      "required": [
        "accounts",
        "parties",
        "batches",
        "transactions",
        "claims"
      ],
      "properties": {
        "accounts": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/account"
          }
        },
        "parties": {
          "type": "array",
          "items": {
            "type": "object"
          }
        },
        "batches": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/batch"
          }
        },
        "transactions": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/transaction"
          }
        },
        "claims": {
          "type": "array",
          "items": {
            "type": "object"
          }
        }
      }
    },
    "evidence": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "required": [
          "page",
          "bbox",
          "row",
          "raw_text",
          "method"
        ],
        "properties": {
          "page": {
            "type": [
              "integer",
              "null"
            ],
            "minimum": 1
          },
          "bbox": {
            "type": [
              "array",
              "null"
            ],
            "items": {
              "type": "number"
            },
            "minItems": 4,
            "maxItems": 4
          },
          "row": {
            "type": [
              "integer",
              "null"
            ]
          },
          "raw_text": {
            "type": "string"
          },
          "method": {
            "type": "string",
            "pattern": "^(parser|pdf_text|ocr)(\\+model)?$"
          },
          "confidence": {
            "type": "number",
            "minimum": 0,
            "maximum": 1
          }
        },
        "if": {
          "properties": {
            "method": {
              "not": {
                "const": "parser"
              }
            }
          }
        },
        "then": {
          "properties": {
            "page": {
              "type": "integer"
            },
            "bbox": {
              "type": "array"
            }
          }
        },
        "title": "Evidence"
      }
    },
    "assertions": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": [
          "id",
          "rule",
          "expected",
          "computed",
          "passed",
          "independent"
        ],
        "properties": {
          "id": {
            "type": "string"
          },
          "rule": {
            "type": "string"
          },
          "expected": {
            "type": "string"
          },
          "computed": {
            "type": "string"
          },
          "passed": {
            "type": "boolean"
          },
          "independent": {
            "type": "boolean"
          }
        },
        "title": "Assertion"
      }
    }
  },
  "$defs": {
    "money": {
      "type": "object",
      "required": [
        "amount",
        "currency"
      ],
      "additionalProperties": false,
      "properties": {
        "amount": {
          "type": "string",
          "pattern": "^-?\\d+(\\.\\d+)?$"
        },
        "currency": {
          "type": "string",
          "pattern": "^[A-Z]{3}$"
        }
      },
      "title": "Money"
    },
    "account": {
      "type": "object",
      "required": [
        "id",
        "currency"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "number": {
          "type": [
            "string",
            "null"
          ]
        },
        "currency": {
          "type": "string",
          "pattern": "^[A-Z]{3}$"
        }
      },
      "title": "Account"
    },
    "batch": {
      "type": "object",
      "required": [
        "id",
        "kind",
        "currency"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "kind": {
          "type": "string"
        },
        "currency": {
          "type": "string",
          "pattern": "^[A-Z]{3}$"
        },
        "account_id": {
          "type": "string"
        },
        "period_start": {
          "type": "string",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
        },
        "period_end": {
          "type": "string",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
        },
        "opening": {
          "$ref": "#/$defs/money"
        },
        "closing": {
          "$ref": "#/$defs/money"
        },
        "stated_closing": {
          "$ref": "#/$defs/money"
        },
        "payout": {
          "oneOf": [
            {
              "$ref": "#/$defs/money"
            },
            {
              "type": "null"
            }
          ]
        },
        "opening_derived": {
          "type": "boolean"
        },
        "source_method": {
          "type": "string"
        },
        "totals_are_formulas": {
          "type": "boolean"
        },
        "no_movement": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "after",
              "balance"
            ],
            "properties": {
              "after": {
                "type": "string"
              },
              "balance": {
                "$ref": "#/$defs/money"
              }
            }
          }
        }
      },
      "title": "Batch"
    },
    "transaction": {
      "type": "object",
      "required": [
        "id",
        "batch_id"
      ],
      "properties": {
        "id": {
          "type": "string"
        },
        "batch_id": {
          "type": "string"
        },
        "account_id": {
          "type": "string"
        },
        "date": {
          "type": "string",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
        },
        "direction": {
          "enum": [
            "credit",
            "debit"
          ]
        },
        "amount": {
          "$ref": "#/$defs/money"
        },
        "balance_after": {
          "$ref": "#/$defs/money"
        },
        "gross": {
          "$ref": "#/$defs/money"
        },
        "fee": {
          "$ref": "#/$defs/money"
        },
        "tax": {
          "$ref": "#/$defs/money"
        },
        "net": {
          "$ref": "#/$defs/money"
        },
        "kind": {
          "type": "string"
        },
        "description": {
          "type": "string"
        },
        "reference": {
          "type": [
            "string",
            "null"
          ]
        }
      },
      "title": "Transaction"
    }
  }
}
