{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "imports-v2/custom_directories.json",
  "title": "Custom directories",
  "description": "A directory, shown as \"Directories\" in the app, is a small table of reference data that cards can pick from, such as \"Data providers\" with a name, a contact email and a country. One record defines the table: its name and its `fields`, the columns. The rows of the table are `custom_directory_records`, a separate entity because a directory can have many of them.\n\nHow the pieces fit:\n1. this record, the directory with its `fields`;\n2. `custom_directory_records`, one per row, each naming the directory in `directory_ref` and giving the cell of every field in `values`, keyed by the `external_id` of the field;\n3. a custom property of type `directory` (`custom_properties`), which lets a card pick a row.\n\nA directory with two fields:\n\n```json\n{\n  \"external_id\": \"dir-providers\",\n  \"name\": \"Data providers\",\n  \"fields\": [\n    { \"external_id\": \"fld-name\", \"name\": \"Name\", \"type\": \"string\", \"is_display\": true },\n    { \"external_id\": \"fld-seats\", \"name\": \"Licensed seats\", \"type\": \"number\" }\n  ]\n}\n```\n\nOne of its rows:\n\n```json\n{\n  \"external_id\": \"rec-nimbus\",\n  \"directory_ref\": \"dir-providers\",\n  \"display_value\": \"Nimbus Analytics\",\n  \"values\": { \"fld-name\": \"Nimbus Analytics\", \"fld-seats\": 40 }\n}\n```\n\nA card picks the row through a property of type `directory`, by the `external_id` of the row in its `properties` (only that field of the card is shown):\n\n```json\n{\n  \"external_id\": \"k-mrr\",\n  \"properties\": { \"cp-provider\": \"rec-nimbus\" }\n}\n```",
  "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."
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 255,
      "description": "Name of the directory, for example `Data providers`. Up to 255 characters."
    },
    "description": {
      "type": [
        "string",
        "null"
      ],
      "description": "A short note about what the directory holds."
    },
    "fields": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "external_id": {
            "type": "string",
            "minLength": 1,
            "maxLength": 512,
            "description": "Your own identifier of the field, for example `fld-name`, unique among the fields of all directories of the package and case sensitive. The `values` of a directory record name the field by it."
          },
          "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 255,
            "description": "Name of the field, for example `Country`. Up to 255 characters."
          },
          "type": {
            "type": "string",
            "enum": [
              "checkbox",
              "date",
              "directory_link",
              "email",
              "file",
              "number",
              "phone",
              "select",
              "string",
              "url",
              "user"
            ],
            "description": "What the field holds: `checkbox`, `date`, `directory_link` (a row of another directory), `email`, `file`, `number`, `phone`, `select`, `string` (text), `url` or `user`."
          },
          "custom_property_ref": {
            "type": "string",
            "minLength": 1,
            "maxLength": 512,
            "x-ref": "custom_properties",
            "x-required-when": "type is select or user",
            "description": "For a `select` or `user` field: the custom property whose options the field offers, for example `cp-priority`. Give the external_id of a `custom_properties` record; required for these two types and not allowed for others."
          },
          "linked_directory_ref": {
            "type": "string",
            "minLength": 1,
            "maxLength": 512,
            "x-ref": "custom_directories",
            "x-required-when": "type is directory_link",
            "description": "For a `directory_link` field: the directory whose records the field links to, for example `Countries`. Give the external_id of a `custom_directories` record; required for this type and not allowed for others."
          },
          "required": {
            "type": "boolean",
            "default": false,
            "description": "Whether every record of the directory must have this field filled in. The default is false."
          },
          "is_display": {
            "type": "boolean",
            "default": false,
            "description": "Whether this field gives each record its title: the text shown for the record in lists and when a card picks it, for example the `Name` field. At most one field per directory."
          },
          "condition": {
            "type": "string",
            "enum": [
              "active",
              "inactive"
            ],
            "default": "active",
            "description": "Whether the field is in use: `active` (the default) or `inactive`. An inactive field is left out of the directory's fields."
          }
        },
        "required": [
          "external_id",
          "name",
          "type"
        ],
        "additionalProperties": false
      },
      "x-identity": "custom_directories.fields",
      "description": "The fields (columns) of the directory, in the order they are shown: the first item is the first column. Each item has its own `external_id`, which the `values` of the directory's records use as a key. Left out, the directory has no fields. At most one field is the display field: its value is the text shown for a record in lists."
    },
    "condition": {
      "type": "string",
      "enum": [
        "active",
        "inactive"
      ],
      "description": "Whether the record is `active` (the default) or `inactive`.",
      "default": "active"
    },
    "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."
    },
    "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 record was created, with a time zone, for example `2026-01-12T09:30:00Z`.",
      "x-default-text": "source.exported_at of the manifest"
    }
  },
  "required": [
    "external_id",
    "name"
  ],
  "additionalProperties": false,
  "allOf": [
    {
      "properties": {
        "fields": {
          "items": {
            "if": {
              "properties": {
                "type": {
                  "enum": [
                    "select",
                    "user"
                  ]
                }
              },
              "required": [
                "type"
              ]
            },
            "then": {
              "required": [
                "custom_property_ref"
              ]
            },
            "else": {
              "not": {
                "required": [
                  "custom_property_ref"
                ]
              }
            }
          }
        }
      }
    },
    {
      "properties": {
        "fields": {
          "items": {
            "if": {
              "properties": {
                "type": {
                  "enum": [
                    "directory_link"
                  ]
                }
              },
              "required": [
                "type"
              ]
            },
            "then": {
              "required": [
                "linked_directory_ref"
              ]
            },
            "else": {
              "not": {
                "required": [
                  "linked_directory_ref"
                ]
              }
            }
          }
        }
      }
    }
  ]
}
