{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://mirafive.io/schema/v1/batch.schema.json",
  "title": "MIRA FIVE event batch, protocol v1",
  "description": "PROTOCOL.md §3 and §4: what a conforming SDK sends. The server may accept more (null for an optional field) and checks what a schema cannot express: event ids unique within the batch, properties ≤ 32 KB encoded with ≤ 64 leaf values and depth ≤ 5, a body ≤ 1 MiB. Unknown fields are allowed and ignored.",
  "type": "object",
  "required": ["v", "batch", "mode", "events"],
  "properties": {
    "v": {
      "const": 1
    },
    "batch": {
      "$ref": "#/$defs/uuid"
    },
    "mode": {
      "enum": ["consentless", "full"]
    },
    "sentAt": {
      "type": "integer",
      "minimum": 0
    },
    "context": {
      "$ref": "#/$defs/context"
    },
    "events": {
      "type": "array",
      "minItems": 1,
      "maxItems": 1000,
      "items": {
        "$ref": "#/$defs/event"
      }
    }
  },
  "if": {
    "type": "object",
    "required": ["mode"],
    "properties": {
      "mode": {
        "const": "consentless"
      }
    }
  },
  "then": {
    "type": "object",
    "properties": {
      "context": {
        "type": "object",
        "properties": {
          "locale": false,
          "timezone": false,
          "screen": false
        }
      },
      "events": {
        "type": "array",
        "items": {
          "type": "object",
          "properties": {
            "anonymousId": false,
            "userId": false,
            "sessionId": false
          }
        }
      }
    }
  },
  "$defs": {
    "uuid": {
      "type": "string",
      "pattern": "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
    },
    "identifier": {
      "type": "string",
      "minLength": 1,
      "maxLength": 256,
      "pattern": "\\S"
    },
    "context": {
      "type": "object",
      "properties": {
        "sdk": {
          "description": "name/version; name ≤ 64, version ≤ 32 characters",
          "type": "string",
          "pattern": "^[^/]{1,64}/.{1,32}$"
        },
        "locale": {
          "type": "string",
          "minLength": 2,
          "maxLength": 35
        },
        "timezone": {
          "type": "string",
          "minLength": 1,
          "maxLength": 64
        },
        "screen": {
          "type": "array",
          "prefixItems": [
            {
              "type": "integer",
              "minimum": 0,
              "maximum": 32768
            },
            {
              "type": "integer",
              "minimum": 0,
              "maximum": 32768
            }
          ],
          "minItems": 2,
          "items": false
        }
      }
    },
    "event": {
      "type": "object",
      "required": ["name"],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128,
          "pattern": "^\\S(?:[\\s\\S]*\\S)?$",
          "anyOf": [
            {
              "pattern": "^[^$]"
            },
            {
              "enum": ["$pageview", "$autocapture", "$identify", "$search", "$exposure", "$install_check"]
            }
          ]
        },
        "time": {
          "type": "integer",
          "minimum": 0
        },
        "id": {
          "$ref": "#/$defs/uuid"
        },
        "page": {
          "type": "object",
          "properties": {
            "url": {
              "type": "string",
              "maxLength": 2048
            },
            "title": {
              "type": "string",
              "maxLength": 512
            },
            "referrer": {
              "type": "string",
              "maxLength": 2048
            }
          }
        },
        "properties": {
          "type": "object",
          "maxProperties": 64,
          "propertyNames": {
            "maxLength": 128
          }
        },
        "anonymousId": {
          "$ref": "#/$defs/identifier"
        },
        "userId": {
          "$ref": "#/$defs/identifier"
        },
        "sessionId": {
          "$ref": "#/$defs/uuid"
        }
      }
    }
  }
}
