{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://openappdefinition.org/schema/0.2/history.schema.json",
  "title": "OAD 0.2 history sidecar",
  "description": "A history sidecar (*.oad.history.json): the ordered entries that record how a definition changed. Each entry carries an RFC 6902 JSON Patch from the previous entry's document, so applying inverse patches undoes edits one at a time.",
  "type": "object",
  "required": ["oadHistory", "id", "entries"],
  "properties": {
    "$schema": {
      "description": "Optional URI of a JSON Schema for this document, for editors. Hosts ignore it.",
      "$ref": "common.schema.json#/$defs/uriReference"
    },
    "oadHistory": {
      "title": "version marker",
      "description": "Spec version the sidecar targets, MAJOR.MINOR. This schema validates version 0.2.",
      "$ref": "common.schema.json#/$defs/specVersion",
      "const": "0.2"
    },
    "id": {
      "description": "The id of the definition this history belongs to.",
      "$ref": "common.schema.json#/$defs/reverseDnsId"
    },
    "base": {
      "description": "Optional version and digest of the document the first patch applies to. Without base, the first patch applies to the empty object.",
      "$ref": "#/$defs/base"
    },
    "entries": {
      "description": "The ordered entries, oldest first.",
      "type": "array",
      "items": { "$ref": "#/$defs/entry" }
    }
  },
  "additionalProperties": false,
  "$defs": {
    "base": {
      "title": "history base",
      "description": "The document the first patch applies to.",
      "type": "object",
      "required": ["version", "digest"],
      "properties": {
        "version": {
          "description": "Version of the base document.",
          "$ref": "common.schema.json#/$defs/semver"
        },
        "digest": {
          "description": "Digest of the base document.",
          "$ref": "common.schema.json#/$defs/digest"
        }
      },
      "additionalProperties": false
    },
    "entry": {
      "title": "history entry",
      "description": "One recorded change to a definition.",
      "type": "object",
      "required": ["version", "at", "by", "intent", "patch", "digest"],
      "properties": {
        "version": {
          "description": "The definition version this entry produced.",
          "$ref": "common.schema.json#/$defs/semver"
        },
        "at": {
          "description": "When the change was made, as a UTC timestamp.",
          "$ref": "common.schema.json#/$defs/datetime"
        },
        "by": {
          "description": "Who made the change.",
          "$ref": "#/$defs/author"
        },
        "approvedBy": {
          "description": "Optional. The person who agreed to the change. A tool that applies a change which widens what the app may do should record it.",
          "$ref": "#/$defs/approver"
        },
        "intent": {
          "description": "The request that led to the change, in the requester's words.",
          "$ref": "common.schema.json#/$defs/text"
        },
        "patch": {
          "description": "RFC 6902 JSON Patch from the previous entry's document (or from base, or from the empty object, for the first entry).",
          "type": "array",
          "items": { "$ref": "#/$defs/patchOperation" }
        },
        "digest": {
          "description": "Digest of the document after the patch.",
          "$ref": "common.schema.json#/$defs/digest"
        }
      },
      "additionalProperties": false
    },
    "author": {
      "title": "change author",
      "description": "The principal that made a change.",
      "type": "object",
      "required": ["kind", "id"],
      "properties": {
        "kind": {
          "description": "person or agent.",
          "enum": ["person", "agent"]
        },
        "id": {
          "description": "Identifier of the person or agent.",
          "$ref": "common.schema.json#/$defs/text"
        }
      },
      "additionalProperties": false
    },
    "approver": {
      "title": "change approver",
      "description": "The person who agreed to a change.",
      "type": "object",
      "required": ["kind", "id"],
      "properties": {
        "kind": {
          "description": "person.",
          "const": "person"
        },
        "id": {
          "description": "Identifier of the person.",
          "$ref": "common.schema.json#/$defs/text"
        }
      },
      "additionalProperties": false
    },
    "jsonPointer": {
      "title": "JSON Pointer",
      "description": "An RFC 6901 JSON Pointer.",
      "type": "string",
      "pattern": "^(?:/(?:[^~/]|~[01])*)*$"
    },
    "patchOperation": {
      "title": "patch operation",
      "description": "One RFC 6902 operation. op selects the shape.",
      "type": "object",
      "oneOf": [
        { "$ref": "#/$defs/addOperation" },
        { "$ref": "#/$defs/removeOperation" },
        { "$ref": "#/$defs/replaceOperation" },
        { "$ref": "#/$defs/moveOperation" },
        { "$ref": "#/$defs/copyOperation" },
        { "$ref": "#/$defs/testOperation" }
      ]
    },
    "addOperation": {
      "title": "add operation",
      "description": "Add a value at a location.",
      "type": "object",
      "required": ["op", "path", "value"],
      "properties": {
        "op": {
          "description": "add.",
          "const": "add"
        },
        "path": {
          "description": "Target location.",
          "$ref": "#/$defs/jsonPointer"
        },
        "value": {
          "description": "The value to add."
        }
      },
      "additionalProperties": false
    },
    "removeOperation": {
      "title": "remove operation",
      "description": "Remove the value at a location.",
      "type": "object",
      "required": ["op", "path"],
      "properties": {
        "op": {
          "description": "remove.",
          "const": "remove"
        },
        "path": {
          "description": "Target location.",
          "$ref": "#/$defs/jsonPointer"
        }
      },
      "additionalProperties": false
    },
    "replaceOperation": {
      "title": "replace operation",
      "description": "Replace the value at a location.",
      "type": "object",
      "required": ["op", "path", "value"],
      "properties": {
        "op": {
          "description": "replace.",
          "const": "replace"
        },
        "path": {
          "description": "Target location.",
          "$ref": "#/$defs/jsonPointer"
        },
        "value": {
          "description": "The replacement value."
        }
      },
      "additionalProperties": false
    },
    "moveOperation": {
      "title": "move operation",
      "description": "Move a value from one location to another.",
      "type": "object",
      "required": ["op", "from", "path"],
      "properties": {
        "op": {
          "description": "move.",
          "const": "move"
        },
        "from": {
          "description": "Source location.",
          "$ref": "#/$defs/jsonPointer"
        },
        "path": {
          "description": "Target location.",
          "$ref": "#/$defs/jsonPointer"
        }
      },
      "additionalProperties": false
    },
    "copyOperation": {
      "title": "copy operation",
      "description": "Copy a value from one location to another.",
      "type": "object",
      "required": ["op", "from", "path"],
      "properties": {
        "op": {
          "description": "copy.",
          "const": "copy"
        },
        "from": {
          "description": "Source location.",
          "$ref": "#/$defs/jsonPointer"
        },
        "path": {
          "description": "Target location.",
          "$ref": "#/$defs/jsonPointer"
        }
      },
      "additionalProperties": false
    },
    "testOperation": {
      "title": "test operation",
      "description": "Test that the value at a location equals a value.",
      "type": "object",
      "required": ["op", "path", "value"],
      "properties": {
        "op": {
          "description": "test.",
          "const": "test"
        },
        "path": {
          "description": "Location to test.",
          "$ref": "#/$defs/jsonPointer"
        },
        "value": {
          "description": "The expected value."
        }
      },
      "additionalProperties": false
    }
  }
}
