{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "imports-v2/attachments.json",
  "title": "Attachments",
  "description": "A file of a card, of a comment, of a document or of an attachment custom property. Give the bytes with `file_path` (under `files/`) or keep them in the cloud with `url`; a document accepts only `file_path`. `attached_to` names what the file belongs to: a card, a comment or a document. For a card, `property_ref` names a custom property of type `attachment`, and the file is then a value of that property on that card. To show a file in a text write `attachment://<external_id>` with the external_id of this record, percent-encoding spaces, quotes, parentheses, brackets, < and > in it: a card text (description, history, comments) may show files attached to the card, to its comments and to its attachment properties, and the `data` of a document shows files attached to that document. An unknown external_id is an error, and a linked file's `file_path` cannot contain \", <, > or &.",
  "type": "object",
  "properties": {
    "external_id": {
      "type": "string",
      "minLength": 1,
      "maxLength": 512,
      "description": "Your own identifier of the record, for example `k-mrr`, unique within the resource across all of its files and case sensitive. Other records refer to this one by it."
    },
    "attached_to": {
      "type": "object",
      "properties": {
        "resource": {
          "type": "string",
          "enum": [
            "cards",
            "comments",
            "documents"
          ]
        },
        "external_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 512
        }
      },
      "required": [
        "resource",
        "external_id"
      ],
      "additionalProperties": false,
      "x-refs": [
        "cards",
        "comments",
        "documents"
      ],
      "description": "What the file belongs to: an object with `resource` (`cards`, `comments` or `documents`) and the `external_id` of that record."
    },
    "property_ref": {
      "type": [
        "string",
        "null"
      ],
      "minLength": 1,
      "maxLength": 512,
      "x-ref": "custom_properties",
      "description": "For a file that is a value of a custom property of the card: the property, by the external_id of a `custom_properties` record of type `attachment`. Only when `attached_to` is a card. The file is then listed under that property, not among the attachments of the card."
    },
    "file_path": {
      "type": "string",
      "minLength": 1,
      "description": "Where the file is inside the package, relative to the `files/` directory, for example `report.txt`. No leading slash, \".\" or \"..\" segments, backslashes, NUL characters or links. It cannot start with emails/ or index/, cannot be exactly \"undefined\", and cannot contain % followed by two hexadecimal digits or a ~ segment: the target keeps or changes those names.",
      "x-required-when": "url is absent"
    },
    "url": {
      "type": "string",
      "maxLength": 4098,
      "pattern": "^https?://",
      "format": "uri",
      "description": "Web address of a file that stays in the cloud (http or https only), for example a shared-drive link. The file is not copied; users open it by this link. Use it instead of `file_path`.",
      "x-required-when": "file_path is absent"
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 512,
      "description": "The name users see for the file, for example `report.txt`."
    },
    "mime_type": {
      "type": [
        "string",
        "null"
      ],
      "maxLength": 128,
      "description": "MIME type of the file, for example `text/plain`."
    },
    "size": {
      "type": "integer",
      "minimum": 0,
      "maximum": 9007199254740991,
      "description": "Size of the file in bytes, for example `48213` for a 47 KB file. Required with `file_path`, where it is checked against the real file; optional with `url`.",
      "x-required-when": "file_path is given"
    },
    "sha256": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$",
      "description": "SHA-256 checksum of the file as 64 lowercase hex characters. It is checked against the real file when given; not allowed with `url`."
    },
    "card_cover": {
      "type": "boolean",
      "description": "Whether the file is the cover picture of the card. At most one cover per card.",
      "default": false
    },
    "created_at": {
      "type": "string",
      "format": "date-time",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(Z|[+-]\\d{2}:\\d{2})$",
      "description": "When the file was added. For a file of a comment it defaults to the time of the comment.",
      "x-default-text": "created_at of the comment for a file attached to a comment, source.exported_at of the manifest otherwise"
    },
    "author_ref": {
      "type": [
        "string",
        "null"
      ],
      "minLength": 1,
      "maxLength": 512,
      "x-ref": "users",
      "description": "The user who created the record, by the external_id of a `users` record, for example `u-ava`. Leave it out or use null when the author is unknown or has left."
    }
  },
  "required": [
    "external_id",
    "attached_to",
    "name"
  ],
  "additionalProperties": false,
  "allOf": [
    {
      "if": {
        "required": [
          "file_path"
        ]
      },
      "then": {
        "required": [
          "size"
        ],
        "not": {
          "required": [
            "url"
          ]
        }
      },
      "else": {
        "required": [
          "url"
        ],
        "not": {
          "required": [
            "sha256"
          ]
        }
      }
    },
    {
      "if": {
        "properties": {
          "attached_to": {
            "properties": {
              "resource": {
                "enum": [
                  "documents"
                ]
              }
            },
            "required": [
              "resource"
            ]
          }
        },
        "required": [
          "attached_to"
        ]
      },
      "then": {
        "required": [
          "file_path"
        ]
      }
    },
    {
      "if": {
        "properties": {
          "attached_to": {
            "properties": {
              "resource": {
                "enum": [
                  "cards"
                ]
              }
            },
            "required": [
              "resource"
            ]
          }
        },
        "required": [
          "attached_to"
        ]
      },
      "else": {
        "not": {
          "anyOf": [
            {
              "required": [
                "card_cover"
              ]
            }
          ]
        }
      }
    },
    {
      "if": {
        "properties": {
          "attached_to": {
            "properties": {
              "resource": {
                "enum": [
                  "cards"
                ]
              }
            },
            "required": [
              "resource"
            ]
          }
        },
        "required": [
          "attached_to"
        ]
      },
      "else": {
        "not": {
          "anyOf": [
            {
              "required": [
                "property_ref"
              ]
            }
          ]
        }
      }
    },
    {
      "if": {
        "required": [
          "property_ref"
        ],
        "properties": {
          "property_ref": {
            "type": "string"
          }
        }
      },
      "then": {
        "not": {
          "anyOf": [
            {
              "required": [
                "card_cover"
              ]
            }
          ]
        }
      }
    }
  ]
}
