{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://interchange.stellarview.net/schemas/common.schema.json",
  "title": "Parts every Interchange message shares",
  "$defs": {
    "uuid": {
      "type": "string",
      "format": "uuid"
    },
    "time": {
      "type": "string",
      "format": "date-time"
    },
    "event_type_id": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]*\\.[a-z][a-z0-9_]*\\.[a-z][a-z0-9_]*$",
      "description": "An event type id in MissionSight's catalogue (vendor/missionsight/catalogue)."
    },
    "asset_class_id": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]*\\.[a-z][a-z0-9_]*$",
      "description": "An asset class id in MissionSight's catalogue."
    },
    "severity": {
      "enum": [
        "info",
        "notice",
        "minor",
        "major",
        "critical"
      ]
    },
    "source": {
      "type": "object",
      "description": "Where the message came from. Every message carries one.",
      "required": [
        "system"
      ],
      "additionalProperties": false,
      "properties": {
        "system": {
          "type": "string",
          "minLength": 1,
          "description": "A source id in the Collector's registry, an adapter id, or an analysis rule id."
        },
        "item_id": {
          "$ref": "#/$defs/uuid",
          "description": "The Collector item this came from."
        },
        "sha256": {
          "type": "string",
          "pattern": "^[a-f0-9]{64}$",
          "description": "Hash of the stored original."
        },
        "rule": {
          "type": "string",
          "description": "The classification or analysis rule that made it."
        }
      }
    },
    "asset_ref": {
      "description": "The asset a message is about. Mapped, with how the mapping was got, or plainly not mapped.",
      "oneOf": [
        {
          "type": "object",
          "required": [
            "asset_id",
            "basis"
          ],
          "additionalProperties": false,
          "properties": {
            "asset_id": {
              "type": "string",
              "minLength": 1
            },
            "name": {
              "type": "string"
            },
            "asset_class": {
              "$ref": "#/$defs/asset_class_id"
            },
            "basis": {
              "enum": [
                "loaded",
                "read",
                "generated",
                "guessed"
              ],
              "description": "How the binding was got."
            },
            "basis_note": {
              "type": "string"
            },
            "device": {
              "type": "string"
            },
            "codes": {
              "type": "array",
              "items": {
                "type": "object"
              }
            },
            "location": {
              "type": "object"
            },
            "criticality": {
              "type": "string"
            },
            "maintenance_owner": {
              "type": "string"
            },
            "snapshot_at": {
              "$ref": "#/$defs/time",
              "description": "When the facts above were copied from the asset system."
            },
            "snapshot_age_s": {
              "type": "integer",
              "minimum": 0,
              "description": "How old the snapshot was when the event was made. A stale binding shows here."
            }
          }
        },
        {
          "type": "object",
          "required": [
            "state",
            "device"
          ],
          "additionalProperties": false,
          "properties": {
            "state": {
              "const": "not_mapped"
            },
            "device": {
              "type": "string"
            }
          }
        },
        {
          "type": "object",
          "required": [
            "state"
          ],
          "additionalProperties": false,
          "properties": {
            "state": {
              "const": "no_asset"
            },
            "why": {
              "type": "string"
            }
          }
        }
      ]
    },
    "catalogue": {
      "type": "object",
      "required": [
        "commit"
      ],
      "additionalProperties": false,
      "description": "The MissionSight catalogue commit the message was checked against.",
      "properties": {
        "commit": {
          "type": "string",
          "pattern": "^[a-f0-9]{40}$"
        }
      }
    },
    "provenance": {
      "type": "object",
      "required": [
        "source",
        "read_at",
        "validated_at"
      ],
      "properties": {
        "source": {
          "$ref": "#/$defs/source"
        },
        "read_at": {
          "$ref": "#/$defs/time",
          "description": "When the Collector or the adapter read it."
        },
        "validated_at": {
          "description": "When it last passed a check, or not_validated.",
          "oneOf": [
            {
              "$ref": "#/$defs/time"
            },
            {
              "const": "not_validated"
            }
          ]
        }
      }
    },
    "source_sensor": {
      "description": "The sensor asset the event came from. A sensor is an asset; its own faults name it as the asset too.",
      "oneOf": [
        {
          "type": "object",
          "required": [
            "asset_id",
            "sensor_type"
          ],
          "additionalProperties": false,
          "properties": {
            "asset_id": {
              "type": "string"
            },
            "sensor_type": {
              "type": "string",
              "pattern": "^sensor\\.[a-z][a-z0-9_]*$"
            },
            "point_name": {
              "type": "string"
            },
            "state": {
              "enum": [
                "reported",
                "needs sensor",
                "to confirm"
              ]
            }
          }
        },
        {
          "type": "object",
          "required": [
            "state"
          ],
          "additionalProperties": false,
          "properties": {
            "state": {
              "enum": [
                "not_bound",
                "no_sensor"
              ]
            },
            "device": {
              "type": "string"
            },
            "why": {
              "type": "string"
            }
          }
        }
      ]
    }
  }
}