{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:hayat:schema:action-event:vNEXT-D",
  "title": "HAYAT action-event \u2014 vNEXT-D draft",
  "type": "object",
  "properties": {
    "protocol_version": {
      "const": "vNEXT-D"
    },
    "project_id": {
      "type": "string",
      "format": "uuid"
    },
    "artifact_id": {
      "type": "string",
      "format": "uuid"
    },
    "record_type": {
      "const": "action_event"
    },
    "author_id": {
      "type": "string",
      "format": "uuid"
    },
    "created_at": {
      "type": "string",
      "format": "date-time",
      "pattern": "Z$"
    },
    "visibility": {
      "enum": [
        "local-example",
        "public",
        "shared",
        "private"
      ]
    },
    "source_refs": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "artifact_id": {
            "type": "string",
            "format": "uuid"
          },
          "sha256": {
            "type": "string",
            "pattern": "^[a-f0-9]{64}$"
          },
          "location": {
            "type": "string",
            "minLength": 1,
            "pattern": "^(?!/)(?!.*(?:\\.\\.|:)).+$"
          }
        },
        "required": [
          "artifact_id",
          "sha256"
        ],
        "additionalProperties": false
      },
      "minItems": 0,
      "uniqueItems": true
    },
    "content_sha256": {
      "type": "string",
      "pattern": "^[a-f0-9]{64}$"
    },
    "payload_location": {
      "type": "string",
      "minLength": 1
    },
    "provenance_limitation": {
      "type": "string"
    },
    "simulation": {
      "type": "boolean"
    },
    "event_id": {
      "type": "string",
      "format": "uuid"
    },
    "action_id": {
      "type": "string",
      "format": "uuid"
    },
    "previous_event_id": {
      "anyOf": [
        {
          "type": "string",
          "format": "uuid"
        },
        {
          "type": "null"
        }
      ]
    },
    "from_state": {
      "anyOf": [
        {
          "enum": [
            "draft",
            "approved",
            "executing",
            "uncertain"
          ]
        },
        {
          "type": "null"
        }
      ]
    },
    "to_state": {
      "enum": [
        "draft",
        "approved",
        "executing",
        "stored",
        "rejected",
        "uncertain",
        "cancelled",
        "revoked",
        "expired"
      ]
    },
    "actor_id": {
      "type": "string",
      "format": "uuid"
    },
    "occurred_at": {
      "type": "string",
      "format": "date-time",
      "pattern": "Z$"
    },
    "approval_ref": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "artifact_id": {
              "type": "string",
              "format": "uuid"
            },
            "sha256": {
              "type": "string",
              "pattern": "^[a-f0-9]{64}$"
            },
            "location": {
              "type": "string",
              "minLength": 1,
              "pattern": "^(?!/)(?!.*(?:\\.\\.|:)).+$"
            }
          },
          "required": [
            "artifact_id",
            "sha256"
          ],
          "additionalProperties": false
        },
        {
          "type": "null"
        }
      ]
    },
    "receipt_ref": {
      "anyOf": [
        {
          "type": "object",
          "properties": {
            "artifact_id": {
              "type": "string",
              "format": "uuid"
            },
            "sha256": {
              "type": "string",
              "pattern": "^[a-f0-9]{64}$"
            },
            "location": {
              "type": "string",
              "minLength": 1,
              "pattern": "^(?!/)(?!.*(?:\\.\\.|:)).+$"
            }
          },
          "required": [
            "artifact_id",
            "sha256"
          ],
          "additionalProperties": false
        },
        {
          "type": "null"
        }
      ]
    },
    "reason": {
      "type": "string",
      "minLength": 1
    }
  },
  "required": [
    "protocol_version",
    "project_id",
    "artifact_id",
    "record_type",
    "author_id",
    "created_at",
    "visibility",
    "source_refs",
    "content_sha256",
    "payload_location",
    "provenance_limitation",
    "simulation",
    "event_id",
    "action_id",
    "previous_event_id",
    "from_state",
    "to_state",
    "actor_id",
    "occurred_at",
    "approval_ref",
    "receipt_ref",
    "reason"
  ],
  "additionalProperties": false,
  "allOf": [
    {
      "if": {
        "properties": {
          "source_refs": {
            "maxItems": 0
          }
        }
      },
      "then": {
        "properties": {
          "provenance_limitation": {
            "type": "string",
            "minLength": 1
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "to_state": {
            "const": "draft"
          }
        }
      },
      "then": {
        "properties": {
          "from_state": {
            "type": "null"
          },
          "previous_event_id": {
            "type": "null"
          },
          "approval_ref": {
            "type": "null"
          },
          "receipt_ref": {
            "type": "null"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "to_state": {
            "const": "approved"
          }
        }
      },
      "then": {
        "properties": {
          "approval_ref": {
            "type": "object",
            "properties": {
              "artifact_id": {
                "type": "string",
                "format": "uuid"
              },
              "sha256": {
                "type": "string",
                "pattern": "^[a-f0-9]{64}$"
              },
              "location": {
                "type": "string",
                "minLength": 1,
                "pattern": "^(?!/)(?!.*(?:\\.\\.|:)).+$"
              }
            },
            "required": [
              "artifact_id",
              "sha256"
            ],
            "additionalProperties": false
          },
          "from_state": {
            "type": "string",
            "minLength": 1
          },
          "previous_event_id": {
            "type": "string",
            "format": "uuid"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "to_state": {
            "const": "executing"
          }
        }
      },
      "then": {
        "properties": {
          "approval_ref": {
            "type": "object",
            "properties": {
              "artifact_id": {
                "type": "string",
                "format": "uuid"
              },
              "sha256": {
                "type": "string",
                "pattern": "^[a-f0-9]{64}$"
              },
              "location": {
                "type": "string",
                "minLength": 1,
                "pattern": "^(?!/)(?!.*(?:\\.\\.|:)).+$"
              }
            },
            "required": [
              "artifact_id",
              "sha256"
            ],
            "additionalProperties": false
          },
          "from_state": {
            "type": "string",
            "minLength": 1
          },
          "previous_event_id": {
            "type": "string",
            "format": "uuid"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "to_state": {
            "const": "stored"
          }
        }
      },
      "then": {
        "properties": {
          "approval_ref": {
            "type": "object",
            "properties": {
              "artifact_id": {
                "type": "string",
                "format": "uuid"
              },
              "sha256": {
                "type": "string",
                "pattern": "^[a-f0-9]{64}$"
              },
              "location": {
                "type": "string",
                "minLength": 1,
                "pattern": "^(?!/)(?!.*(?:\\.\\.|:)).+$"
              }
            },
            "required": [
              "artifact_id",
              "sha256"
            ],
            "additionalProperties": false
          },
          "from_state": {
            "type": "string",
            "minLength": 1
          },
          "previous_event_id": {
            "type": "string",
            "format": "uuid"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "to_state": {
            "const": "rejected"
          }
        }
      },
      "then": {
        "properties": {
          "approval_ref": {
            "type": "object",
            "properties": {
              "artifact_id": {
                "type": "string",
                "format": "uuid"
              },
              "sha256": {
                "type": "string",
                "pattern": "^[a-f0-9]{64}$"
              },
              "location": {
                "type": "string",
                "minLength": 1,
                "pattern": "^(?!/)(?!.*(?:\\.\\.|:)).+$"
              }
            },
            "required": [
              "artifact_id",
              "sha256"
            ],
            "additionalProperties": false
          },
          "from_state": {
            "type": "string",
            "minLength": 1
          },
          "previous_event_id": {
            "type": "string",
            "format": "uuid"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "to_state": {
            "const": "uncertain"
          }
        }
      },
      "then": {
        "properties": {
          "approval_ref": {
            "type": "object",
            "properties": {
              "artifact_id": {
                "type": "string",
                "format": "uuid"
              },
              "sha256": {
                "type": "string",
                "pattern": "^[a-f0-9]{64}$"
              },
              "location": {
                "type": "string",
                "minLength": 1,
                "pattern": "^(?!/)(?!.*(?:\\.\\.|:)).+$"
              }
            },
            "required": [
              "artifact_id",
              "sha256"
            ],
            "additionalProperties": false
          },
          "from_state": {
            "type": "string",
            "minLength": 1
          },
          "previous_event_id": {
            "type": "string",
            "format": "uuid"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "to_state": {
            "const": "stored"
          }
        }
      },
      "then": {
        "properties": {
          "receipt_ref": {
            "type": "object",
            "properties": {
              "artifact_id": {
                "type": "string",
                "format": "uuid"
              },
              "sha256": {
                "type": "string",
                "pattern": "^[a-f0-9]{64}$"
              },
              "location": {
                "type": "string",
                "minLength": 1,
                "pattern": "^(?!/)(?!.*(?:\\.\\.|:)).+$"
              }
            },
            "required": [
              "artifact_id",
              "sha256"
            ],
            "additionalProperties": false
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "to_state": {
            "const": "rejected"
          }
        }
      },
      "then": {
        "properties": {
          "receipt_ref": {
            "type": "object",
            "properties": {
              "artifact_id": {
                "type": "string",
                "format": "uuid"
              },
              "sha256": {
                "type": "string",
                "pattern": "^[a-f0-9]{64}$"
              },
              "location": {
                "type": "string",
                "minLength": 1,
                "pattern": "^(?!/)(?!.*(?:\\.\\.|:)).+$"
              }
            },
            "required": [
              "artifact_id",
              "sha256"
            ],
            "additionalProperties": false
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "to_state": {
            "const": "approved"
          }
        }
      },
      "then": {
        "properties": {
          "receipt_ref": {
            "type": "null"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "to_state": {
            "const": "executing"
          }
        }
      },
      "then": {
        "properties": {
          "receipt_ref": {
            "type": "null"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "to_state": {
            "const": "cancelled"
          }
        }
      },
      "then": {
        "properties": {
          "receipt_ref": {
            "type": "null"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "to_state": {
            "const": "revoked"
          }
        }
      },
      "then": {
        "properties": {
          "receipt_ref": {
            "type": "null"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "to_state": {
            "const": "expired"
          }
        }
      },
      "then": {
        "properties": {
          "receipt_ref": {
            "type": "null"
          }
        }
      }
    }
  ]
}
