{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "action": {
      "type": "string",
      "const": "patch"
    },
    "id": {
      "type": "string",
      "format": "uuid",
      "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-7[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$"
    },
    "base": {
      "type": "object",
      "properties": {
        "revision": {
          "type": "integer",
          "exclusiveMinimum": 0,
          "maximum": 9007199254740991
        },
        "revision_op": {
          "type": "string",
          "format": "uuid",
          "pattern": "^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-7[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$"
        }
      },
      "required": ["revision", "revision_op"],
      "additionalProperties": false
    },
    "fields": {
      "type": "object",
      "propertyNames": {
        "type": "string"
      },
      "additionalProperties": {
        "$ref": "#/$defs/__schema0"
      },
      "opaque": true
    },
    "part_ops": {
      "maxItems": 1000,
      "type": "array",
      "items": {
        "oneOf": [
          {
            "type": "object",
            "properties": {
              "op": {
                "type": "string",
                "const": "insert"
              },
              "parent": {
                "anyOf": [
                  {
                    "type": "string",
                    "const": "$root"
                  },
                  {
                    "type": "string",
                    "pattern": "^[A-Za-z0-9_.:-]{1,64}$"
                  }
                ]
              },
              "list": {
                "type": "string",
                "pattern": "^[a-z_][a-z_0-9]*(?:\\.[a-z_][a-z_0-9]*)*$"
              },
              "after": {
                "type": "string",
                "pattern": "^[A-Za-z0-9_.:-]{1,64}$"
              },
              "value": {
                "type": "object",
                "propertyNames": {
                  "type": "string"
                },
                "additionalProperties": {
                  "$ref": "#/$defs/__schema0"
                },
                "opaque": true
              }
            },
            "required": ["op", "parent", "list", "value"],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "op": {
                "type": "string",
                "const": "move"
              },
              "part": {
                "type": "string",
                "pattern": "^[A-Za-z0-9_.:-]{1,64}$"
              },
              "parent": {
                "anyOf": [
                  {
                    "type": "string",
                    "const": "$root"
                  },
                  {
                    "type": "string",
                    "pattern": "^[A-Za-z0-9_.:-]{1,64}$"
                  }
                ]
              },
              "list": {
                "type": "string",
                "pattern": "^[a-z_][a-z_0-9]*(?:\\.[a-z_][a-z_0-9]*)*$"
              },
              "after": {
                "type": "string",
                "pattern": "^[A-Za-z0-9_.:-]{1,64}$"
              }
            },
            "required": ["op", "part", "parent", "list"],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "op": {
                "type": "string",
                "const": "remove"
              },
              "part": {
                "type": "string",
                "pattern": "^[A-Za-z0-9_.:-]{1,64}$"
              }
            },
            "required": ["op", "part"],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "op": {
                "type": "string",
                "const": "patch"
              },
              "part": {
                "type": "string",
                "pattern": "^[A-Za-z0-9_.:-]{1,64}$"
              },
              "fields": {
                "type": "object",
                "propertyNames": {
                  "type": "string"
                },
                "additionalProperties": {
                  "$ref": "#/$defs/__schema0"
                },
                "opaque": true
              }
            },
            "required": ["op", "part", "fields"],
            "additionalProperties": false
          }
        ]
      },
      "atomic": true
    },
    "reset": {
      "minItems": 1,
      "maxItems": 1000,
      "type": "array",
      "items": {
        "anyOf": [
          {
            "type": "object",
            "properties": {
              "part": {
                "anyOf": [
                  {
                    "type": "string",
                    "const": "$root"
                  },
                  {
                    "type": "string",
                    "pattern": "^[A-Za-z0-9_.:-]{1,64}$"
                  }
                ]
              },
              "field": {
                "type": "string",
                "minLength": 1,
                "pattern": "^[^$]"
              },
              "path": {
                "minItems": 1,
                "maxItems": 2,
                "type": "array",
                "items": {
                  "type": "string",
                  "minLength": 1
                }
              }
            },
            "required": ["field"],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "part": {
                "anyOf": [
                  {
                    "type": "string",
                    "const": "$root"
                  },
                  {
                    "type": "string",
                    "pattern": "^[A-Za-z0-9_.:-]{1,64}$"
                  }
                ]
              },
              "list": {
                "type": "string",
                "minLength": 1
              }
            },
            "required": ["list"],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "part": {
                "anyOf": [
                  {
                    "type": "string",
                    "const": "$root"
                  },
                  {
                    "type": "string",
                    "pattern": "^[A-Za-z0-9_.:-]{1,64}$"
                  }
                ]
              }
            },
            "required": ["part"],
            "additionalProperties": false
          }
        ]
      },
      "description": "Return owner-edited addresses to the source before applying fields and part operations. Requires the current base. Omit part for a root field or list. Use only part for a whole part, or only part: $root for the whole record."
    },
    "state": {
      "type": "object",
      "propertyNames": {
        "type": "string"
      },
      "additionalProperties": {
        "$ref": "#/$defs/__schema0"
      },
      "opaque": true,
      "description": "Per-account state. A supplied map replaces caller-owned keys and preserves omitted server-owned keys. Omission leaves existing state untouched."
    },
    "state_patch": {
      "minItems": 1,
      "maxItems": 1000,
      "type": "array",
      "items": {
        "anyOf": [
          {
            "type": "object",
            "properties": {
              "op": {
                "type": "string",
                "const": "set"
              },
              "key": {
                "type": "string",
                "maxLength": 65536
              },
              "value": {
                "$ref": "#/$defs/__schema0"
              }
            },
            "required": ["op", "key", "value"],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "op": {
                "type": "string",
                "const": "remove"
              },
              "key": {
                "type": "string",
                "maxLength": 65536
              }
            },
            "required": ["op", "key"],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "op": {
                "type": "string",
                "const": "set_entry"
              },
              "key": {
                "type": "string",
                "const": "cooked"
              },
              "entry": {
                "type": "string",
                "pattern": "^[A-Za-z0-9_.:-]{1,64}$"
              },
              "value": {
                "type": "object",
                "properties": {
                  "at": {
                    "type": "string",
                    "format": "date-time",
                    "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$"
                  },
                  "scale": {
                    "type": "number",
                    "exclusiveMinimum": 0
                  },
                  "notes": {
                    "type": "string",
                    "maxLength": 5000
                  }
                },
                "required": ["at"],
                "additionalProperties": false
              }
            },
            "required": ["op", "key", "entry", "value"],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "op": {
                "type": "string",
                "const": "set_entry"
              },
              "key": {
                "type": "string",
                "const": "episodes"
              },
              "entry": {
                "type": "string",
                "maxLength": 256,
                "pattern": "^(?:s(?:0|[1-9]\\d*)e(?:0|[1-9]\\d*)|(?:openlibrary|wikidata|tvmaze|itunes|musicbrainz|rss|tmdb|spotify):[\\s\\S]+)$"
              },
              "value": {
                "type": "string",
                "format": "date-time",
                "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$"
              }
            },
            "required": ["op", "key", "entry", "value"],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "op": {
                "type": "string",
                "const": "remove_entry"
              },
              "key": {
                "type": "string",
                "const": "cooked"
              },
              "entry": {
                "type": "string",
                "pattern": "^[A-Za-z0-9_.:-]{1,64}$"
              }
            },
            "required": ["op", "key", "entry"],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "op": {
                "type": "string",
                "const": "remove_entry"
              },
              "key": {
                "type": "string",
                "const": "episodes"
              },
              "entry": {
                "type": "string",
                "maxLength": 256,
                "pattern": "^(?:s(?:0|[1-9]\\d*)e(?:0|[1-9]\\d*)|(?:openlibrary|wikidata|tvmaze|itunes|musicbrainz|rss|tmdb|spotify):[\\s\\S]+)$"
              }
            },
            "required": ["op", "key", "entry"],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "op": {
                "type": "string",
                "const": "add_member"
              },
              "key": {
                "type": "string",
                "const": "tags"
              },
              "member": {
                "type": "string",
                "maxLength": 60
              }
            },
            "required": ["op", "key", "member"],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "op": {
                "type": "string",
                "const": "add_member"
              },
              "key": {
                "type": "string",
                "const": "completed_stop_ids"
              },
              "member": {
                "type": "string",
                "pattern": "^[A-Za-z0-9_.:-]{1,64}$"
              }
            },
            "required": ["op", "key", "member"],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "op": {
                "type": "string",
                "const": "remove_member"
              },
              "key": {
                "type": "string",
                "const": "tags"
              },
              "member": {
                "type": "string",
                "maxLength": 60
              }
            },
            "required": ["op", "key", "member"],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "op": {
                "type": "string",
                "const": "remove_member"
              },
              "key": {
                "type": "string",
                "const": "completed_stop_ids"
              },
              "member": {
                "type": "string",
                "pattern": "^[A-Za-z0-9_.:-]{1,64}$"
              }
            },
            "required": ["op", "key", "member"],
            "additionalProperties": false
          },
          {
            "type": "object",
            "properties": {
              "op": {
                "type": "string",
                "const": "merge_experience"
              },
              "value": {
                "type": "object",
                "properties": {
                  "version": {
                    "type": "number",
                    "const": 1
                  },
                  "opened_at": {
                    "type": "string",
                    "format": "date-time",
                    "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$"
                  },
                  "visits": {
                    "type": "object",
                    "properties": {
                      "current": {
                        "type": "object",
                        "properties": {
                          "date": {
                            "type": "object",
                            "properties": {
                              "value": {
                                "anyOf": [
                                  {
                                    "type": "string",
                                    "format": "date",
                                    "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))$"
                                  },
                                  {
                                    "type": "null"
                                  }
                                ]
                              },
                              "updated_at": {
                                "type": "string",
                                "format": "date-time",
                                "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$"
                              }
                            },
                            "required": ["value", "updated_at"],
                            "additionalProperties": false
                          },
                          "rating": {
                            "type": "object",
                            "properties": {
                              "value": {
                                "anyOf": [
                                  {
                                    "type": "integer",
                                    "minimum": 0,
                                    "maximum": 8
                                  },
                                  {
                                    "type": "null"
                                  }
                                ]
                              },
                              "updated_at": {
                                "type": "string",
                                "format": "date-time",
                                "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$"
                              }
                            },
                            "required": ["value", "updated_at"],
                            "additionalProperties": false
                          },
                          "note": {
                            "type": "object",
                            "properties": {
                              "value": {
                                "type": "string",
                                "maxLength": 5000
                              },
                              "updated_at": {
                                "type": "string",
                                "format": "date-time",
                                "pattern": "^(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))T(?:(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d+)?(?:Z|([+-](?:[01]\\d|2[0-3]):[0-5]\\d)))$"
                              }
                            },
                            "required": ["value", "updated_at"],
                            "additionalProperties": false
                          }
                        },
                        "additionalProperties": false
                      }
                    },
                    "additionalProperties": false
                  }
                },
                "required": ["version"],
                "additionalProperties": false
              }
            },
            "required": ["op", "value"],
            "additionalProperties": false
          }
        ]
      },
      "description": "Addressed edits to caller-owned keys in current account state. Exclusive with state."
    },
    "conflict": {
      "type": "string",
      "const": "strict"
    }
  },
  "required": ["action", "id", "base"],
  "additionalProperties": false,
  "anyOf": [
    {
      "type": "object",
      "required": ["state"],
      "properties": {
        "state": {
          "type": "object"
        },
        "state_patch": false
      }
    },
    {
      "type": "object",
      "required": ["state_patch"],
      "properties": {
        "state_patch": {
          "type": "array",
          "items": {},
          "minItems": 1
        },
        "state": false
      }
    },
    {
      "type": "object",
      "required": ["fields"],
      "properties": {
        "fields": {
          "type": "object",
          "minProperties": 1
        },
        "state": false,
        "state_patch": false
      }
    },
    {
      "type": "object",
      "required": ["part_ops"],
      "properties": {
        "part_ops": {
          "type": "array",
          "items": {},
          "minItems": 1
        },
        "state": false,
        "state_patch": false
      }
    },
    {
      "type": "object",
      "required": ["reset"],
      "properties": {
        "reset": {
          "type": "array",
          "items": {},
          "minItems": 1
        },
        "state": false,
        "state_patch": false
      }
    }
  ],
  "$defs": {
    "__schema0": {
      "anyOf": [
        {
          "type": "string"
        },
        {
          "type": "number"
        },
        {
          "type": "boolean"
        },
        {
          "type": "null"
        },
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/__schema0"
          }
        },
        {
          "type": "object",
          "propertyNames": {
            "type": "string"
          },
          "additionalProperties": {
            "$ref": "#/$defs/__schema0"
          }
        }
      ]
    }
  },
  "$id": "https://docs.sendtopigeon.com/schemas/record-patch@1.schema.json"
}
