{
  "type": "object",
  "additionalProperties": false,
  "required": [
    "name",
    "description",
    "fields"
  ],
  "properties": {
    "name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160,
      "description": "Resource name, unique among Data-Collection resources in this System. Used exactly in filenames and references.",
      "pattern": "^[A-Za-z0-9_-]+$"
    },
    "description": {
      "type": "string",
      "minLength": 20,
      "maxLength": 4000,
      "description": "Business-facing explanation of what this resource represents, why it exists and how it is used."
    },
    "tags": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "type": "string",
        "pattern": "\\S"
      },
      "description": "Optional labels chosen by the author for grouping, filtering and discovery. Tags do not change storage, grant access or establish certification."
    },
    "fields": {
      "type": "array",
      "minItems": 1,
      "allOf": [
        {
          "contains": {
            "type": "object",
            "required": [
              "name",
              "type"
            ],
            "properties": {
              "name": {
                "const": "id"
              },
              "type": {
                "enum": [
                  "string",
                  "String",
                  "STRING",
                  "integer",
                  "Integer",
                  "INTEGER"
                ]
              }
            }
          }
        },
        {
          "contains": {
            "type": "object",
            "required": [
              "name",
              "type",
              "default"
            ],
            "properties": {
              "name": {
                "const": "timestamp"
              },
              "type": {
                "enum": [
                  "datetime",
                  "Datetime",
                  "DATETIME"
                ]
              },
              "default": {
                "const": "now"
              }
            }
          }
        },
        {
          "contains": {
            "type": "object",
            "required": [
              "name",
              "type",
              "default"
            ],
            "properties": {
              "name": {
                "const": "deleted"
              },
              "type": {
                "enum": [
                  "boolean",
                  "Boolean",
                  "BOOLEAN"
                ]
              },
              "default": {
                "const": false
              }
            }
          }
        }
      ],
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "name",
          "type",
          "description",
          "label"
        ],
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 160
          },
          "type": {
            "type": "string",
            "enum": [
              "string",
              "String",
              "STRING",
              "text",
              "Text",
              "TEXT",
              "integer",
              "Integer",
              "INTEGER",
              "long",
              "Long",
              "LONG",
              "decimal",
              "Decimal",
              "DECIMAL",
              "boolean",
              "Boolean",
              "BOOLEAN",
              "date",
              "Date",
              "DATE",
              "datetime",
              "Datetime",
              "DATETIME",
              "object",
              "Object",
              "OBJECT",
              "array",
              "Array",
              "ARRAY",
              "file",
              "File",
              "FILE",
              "reference",
              "Reference",
              "REFERENCE"
            ],
            "description": "Case-insensitive. Use integer or long for whole persisted numbers, decimal for fractional values, and reference for a link to another Data Collection. Every reference requires collection."
          },
          "description": {
            "type": "string",
            "minLength": 1,
            "maxLength": 2000
          },
          "collection": {
            "type": "string",
            "pattern": "^[A-Za-z0-9_-]+$",
            "minLength": 1,
            "maxLength": 160,
            "description": "For reference fields, the singular Data Collection name. Name the field after the related object, never with an Id suffix."
          },
          "sType": {
            "type": "string",
            "pattern": "^[A-Za-z0-9_-]+$",
            "description": "Array element type, defaulting to string. A Collection name denotes stored IDs with that Collection id type; it does not convert or hydrate record objects."
          },
          "schema": {
            "type": "object",
            "additionalProperties": true,
            "description": "Nested structure for object fields or structured array elements. Not required for primitive arrays."
          },
          "length": {
            "type": "integer",
            "minimum": 1
          },
          "required": {
            "type": "boolean"
          },
          "access": {
            "type": "string",
            "enum": [
              "r",
              "R",
              "w",
              "W",
              "rw",
              "Rw",
              "RW"
            ]
          },
          "default": {},
          "values": {
            "type": "array",
            "minItems": 1,
            "uniqueItems": true,
            "items": {},
            "description": "Allowed field values, chosen by the System author. Each value follows the field type. Omit when there is no fixed list."
          },
          "auto": {
            "type": "boolean",
            "description": "For id, true requests automatic generation: 24-character string identities or an integer sequence. A supplied identity is supported when needed."
          },
          "unique": {
            "type": "boolean"
          },
          "key": {
            "type": "boolean",
            "description": "id is the collection key and represents the identity of one stored record."
          },
          "volatile": {
            "type": "boolean"
          },
          "virtual": {
            "type": "boolean"
          },
          "traceable": {
            "type": "boolean"
          },
          "column": {
            "type": "string",
            "minLength": 1,
            "maxLength": 160,
            "description": "During a controlled change to a published System, set this to the existing physical column only when logically renaming a field without moving or losing its stored values. Do not use it for ordinary new fields."
          },
          "service": {
            "type": "string",
            "minLength": 1,
            "maxLength": 160
          },
          "classification": {
            "type": "string",
            "enum": [
              "public",
              "internal",
              "confidential",
              "restricted"
            ]
          },
          "categories": {
            "type": "array",
            "uniqueItems": true,
            "items": {
              "type": "string",
              "enum": [
                "personal-data",
                "cardholder-data",
                "sensitive-authentication-data",
                "financial-data",
                "other-regulated-data"
              ]
            }
          },
          "handling": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "storage": {
                "type": "string",
                "enum": [
                  "none",
                  "transient",
                  "persistent"
                ]
              },
              "retention": {
                "type": "string",
                "maxLength": 500
              },
              "disposal": {
                "type": "string",
                "maxLength": 1000
              },
              "protections": {
                "type": "array",
                "uniqueItems": true,
                "items": {
                  "type": "string",
                  "enum": [
                    "encryption",
                    "tokenization",
                    "masking",
                    "redaction",
                    "access-control",
                    "audit-logging"
                  ]
                }
              }
            }
          },
          "label": {
            "type": "string",
            "minLength": 1,
            "description": "Human-readable display label. The name remains the code and mapping key."
          }
        },
        "allOf": [
          {
            "if": {
              "properties": {
                "type": {
                  "const": "reference"
                }
              },
              "required": [
                "type"
              ]
            },
            "then": {
              "required": [
                "collection"
              ]
            }
          }
        ]
      }
    },
    "indexes": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "type",
          "fields"
        ],
        "properties": {
          "type": {
            "type": "string",
            "enum": [
              "plain",
              "Plain",
              "PLAIN",
              "unique",
              "Unique",
              "UNIQUE"
            ]
          },
          "fields": {
            "type": "array",
            "minItems": 1,
            "uniqueItems": true,
            "items": {
              "type": "string",
              "minLength": 1,
              "maxLength": 160
            }
          },
          "where": {
            "type": "object",
            "additionalProperties": true,
            "description": "Optional partial-index predicate."
          }
        }
      },
      "description": "Plain, unique, composite and partial indexes. Business uniqueness belongs here; every record already has its id."
    },
    "states": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 160
          },
          "description": {
            "type": "string",
            "maxLength": 2000
          },
          "initial": {
            "type": "boolean"
          },
          "terminal": {
            "type": "boolean"
          }
        }
      }
    },
    "relationships": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": [
          "name",
          "collection",
          "cardinality"
        ],
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 160
          },
          "collection": {
            "type": "string",
            "pattern": "^[A-Za-z0-9_-]+$",
            "description": "Exact name of the referenced resource, unique within its resource type in this System.",
            "minLength": 1
          },
          "cardinality": {
            "type": "string",
            "enum": [
              "one",
              "One",
              "ONE",
              "optional",
              "Optional",
              "OPTIONAL",
              "many",
              "Many",
              "MANY"
            ]
          },
          "description": {
            "type": "string",
            "maxLength": 2000
          }
        }
      }
    }
  },
  "description": "One Data Collection: stored records, their fields, relationships and indexes.",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Data-Collection.schema.json",
  "$id": "Data-Collection.schema.json"
}
