{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://xcim.org/schemas/0.1/batch-manifest.schema.json",
  "title": "XCIM 0.1 Batch Manifest Payload",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "xcim_version", "object_type", "issuer_id", "batch_sequence", "created_at",
    "previous_tree_size", "tree_size", "batch_event_count", "first_sequence", "last_sequence",
    "previous_event_root", "event_root", "state_root", "previous_anchor_hash", "proof_bundle_set_hash"
  ],
  "properties": {
    "xcim_version": { "const": "0.1" },
    "object_type": { "const": "batch_manifest" },
    "issuer_id": { "type": "string", "pattern": "^urn:xcim:issuer:[A-Za-z0-9._~:-]+$" },
    "batch_sequence": { "$ref": "#/$defs/nonnegative" },
    "created_at": { "type": "string", "format": "date-time" },
    "previous_tree_size": { "$ref": "#/$defs/nonnegative" },
    "tree_size": { "$ref": "#/$defs/nonnegative" },
    "batch_event_count": { "$ref": "#/$defs/nonnegative" },
    "first_sequence": { "oneOf": [{ "$ref": "#/$defs/positive" }, { "type": "null" }] },
    "last_sequence": { "oneOf": [{ "$ref": "#/$defs/positive" }, { "type": "null" }] },
    "previous_event_root": { "$ref": "#/$defs/hash" },
    "event_root": { "$ref": "#/$defs/hash" },
    "state_root": { "$ref": "#/$defs/hash" },
    "previous_anchor_hash": { "$ref": "#/$defs/hash" },
    "proof_bundle_set_hash": { "$ref": "#/$defs/hash" },
    "consistency_proof_uri": { "type": "string", "format": "uri" }
  },
  "allOf": [
    {
      "if": { "properties": { "batch_event_count": { "const": 0 } }, "required": ["batch_event_count"] },
      "then": {
        "properties": {
          "first_sequence": { "type": "null" },
          "last_sequence": { "type": "null" }
        }
      },
      "else": {
        "properties": {
          "first_sequence": { "$ref": "#/$defs/positive" },
          "last_sequence": { "$ref": "#/$defs/positive" }
        }
      }
    }
  ],
  "$defs": {
    "nonnegative": { "type": "integer", "minimum": 0, "maximum": 9007199254740991 },
    "positive": { "type": "integer", "minimum": 1, "maximum": 9007199254740991 },
    "hash": { "type": "string", "minLength": 43, "maxLength": 43, "pattern": "^[A-Za-z0-9_-]+$" }
  }
}
