{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:oprp:experimental:0.1:subscription",
  "title": "OPRP experimental subscription and callback bodies",
  "$defs": {
    "subscribe": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "operation",
        "patient",
        "access_token",
        "subscription_id",
        "callback",
        "topics",
        "expires_at"
      ],
      "properties": {
        "operation": {
          "const": "subscribe"
        },
        "patient": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        },
        "access_token": {
          "type": "string",
          "maxLength": 14000
        },
        "subscription_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        },
        "callback": {
          "type": "string",
          "format": "uri",
          "maxLength": 2048
        },
        "topics": {
          "type": "array",
          "minItems": 1,
          "maxItems": 8,
          "uniqueItems": true,
          "items": {
            "enum": [
              "Patient",
              "Observation",
              "Condition",
              "AllergyIntolerance",
              "MedicationStatement",
              "Encounter",
              "Procedure",
              "Immunization"
            ]
          }
        },
        "expires_at": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "control": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "operation",
        "patient",
        "access_token",
        "subscription_id"
      ],
      "properties": {
        "operation": {
          "enum": [
            "subscription_status",
            "unsubscribe"
          ]
        },
        "patient": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        },
        "access_token": {
          "type": "string",
          "maxLength": 14000
        },
        "subscription_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        }
      }
    },
    "view": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status",
        "subscription_id",
        "state",
        "expires_at",
        "topics"
      ],
      "properties": {
        "status": {
          "const": "ok"
        },
        "subscription_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        },
        "state": {
          "enum": [
            "pending",
            "active",
            "suspended",
            "cancelled"
          ]
        },
        "expires_at": {
          "type": "integer",
          "minimum": 0
        },
        "topics": {
          "type": "array",
          "minItems": 1,
          "maxItems": 8,
          "uniqueItems": true,
          "items": {
            "enum": [
              "Patient",
              "Observation",
              "Condition",
              "AllergyIntolerance",
              "MedicationStatement",
              "Encounter",
              "Procedure",
              "Immunization"
            ]
          }
        }
      }
    },
    "challenge": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "operation",
        "subscription_id",
        "challenge",
        "callback",
        "expires_at"
      ],
      "properties": {
        "operation": {
          "const": "subscription_challenge"
        },
        "subscription_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        },
        "challenge": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_-]{43}$"
        },
        "callback": {
          "type": "string",
          "format": "uri",
          "maxLength": 2048
        },
        "expires_at": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "challengeAck": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status",
        "operation",
        "subscription_id",
        "challenge",
        "callback"
      ],
      "properties": {
        "status": {
          "const": "ok"
        },
        "operation": {
          "const": "subscription_challenge_ack"
        },
        "subscription_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        },
        "challenge": {
          "type": "string",
          "pattern": "^[A-Za-z0-9_-]{43}$"
        },
        "callback": {
          "type": "string",
          "format": "uri",
          "maxLength": 2048
        }
      }
    },
    "notification": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "operation",
        "subscription_id",
        "delivery_id"
      ],
      "properties": {
        "operation": {
          "const": "change_notification"
        },
        "subscription_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        },
        "delivery_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        }
      }
    },
    "notificationAck": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "status",
        "operation",
        "subscription_id",
        "delivery_id"
      ],
      "properties": {
        "status": {
          "const": "ok"
        },
        "operation": {
          "const": "notification_ack"
        },
        "subscription_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        },
        "delivery_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 200
        }
      }
    }
  },
  "oneOf": [
    {
      "$ref": "#/$defs/subscribe"
    },
    {
      "$ref": "#/$defs/control"
    },
    {
      "$ref": "#/$defs/view"
    },
    {
      "$ref": "#/$defs/challenge"
    },
    {
      "$ref": "#/$defs/challengeAck"
    },
    {
      "$ref": "#/$defs/notification"
    },
    {
      "$ref": "#/$defs/notificationAck"
    }
  ]
}
