{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://duckcode-ai.github.io/manifest-spec/v1/datalex-manifest.schema.json",
  "title": "DataLex Manifest v1",
  "description": "Compiled output of the DataLex compiler. Describes domains, entities, contracts, governance, and rules at conceptual / logical / physical layers. Producers (DataLex 1.8.x and later) emit JSON validating against this schema. Consumers (catalogs, AI agents, DQL compilers) read it to resolve datalex_contract references.",
  "type": "object",
  "required": ["manifestSpecVersion", "datalexVersion", "generatedAt", "project", "domains"],
  "additionalProperties": true,
  "properties": {
    "manifestSpecVersion": {
      "type": "string",
      "pattern": "^1\\.[0-9]+\\.[0-9]+$",
      "description": "Major version of the manifest spec this artifact validates against. v1.x at this time."
    },
    "datalexVersion": {
      "type": "string",
      "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$",
      "description": "Version of the DataLex compiler that produced this manifest."
    },
    "generatedAt": {
      "type": "string",
      "format": "date-time",
      "description": "RFC3339 timestamp the manifest was emitted."
    },
    "project": {
      "type": "object",
      "required": ["name"],
      "properties": {
        "name": {"type": "string", "minLength": 1},
        "description": {"type": "string"},
        "dialect": {"type": "string"},
        "owners": {"type": "array", "items": {"type": "string"}}
      }
    },
    "domains": {
      "type": "array",
      "items": {"$ref": "#/$defs/domain"}
    },
    "diagnostics": {
      "type": "array",
      "items": {"$ref": "#/$defs/diagnostic"},
      "description": "Non-fatal warnings the compiler emitted; consumers may surface or ignore."
    }
  },
  "$defs": {
    "domain": {
      "type": "object",
      "required": ["name", "entities"],
      "additionalProperties": true,
      "properties": {
        "name": {"type": "string", "minLength": 1},
        "description": {"type": "string"},
        "owners": {"type": "array", "items": {"type": "string"}},
        "entities": {
          "type": "array",
          "items": {"$ref": "#/$defs/entity"}
        },
        "glossary": {
          "type": "array",
          "items": {"$ref": "#/$defs/glossaryTerm"}
        }
      }
    },
    "entity": {
      "type": "object",
      "required": ["name"],
      "additionalProperties": true,
      "properties": {
        "name": {"type": "string"},
        "description": {"type": "string"},
        "tags": {"type": "array", "items": {"type": "string"}},
        "fields": {
          "type": "array",
          "items": {"$ref": "#/$defs/field"}
        },
        "contracts": {
          "type": "array",
          "items": {"$ref": "#/$defs/contract"}
        },
        "binding": {
          "type": "object",
          "description": "How this conceptual/logical entity binds to a physical artifact (typically a dbt model).",
          "properties": {
            "kind": {"type": "string", "enum": ["dbt_model", "dbt_source", "view", "table", "external"]},
            "ref": {"type": "string"}
          }
        }
      }
    },
    "field": {
      "type": "object",
      "required": ["name"],
      "additionalProperties": true,
      "properties": {
        "name": {"type": "string"},
        "type": {"type": "string"},
        "description": {"type": "string"},
        "primary_key": {"type": "boolean"},
        "nullable": {"type": "boolean"},
        "unique": {"type": "boolean"},
        "tags": {"type": "array", "items": {"type": "string"}},
        "classification": {"type": "string"}
      }
    },
    "contract": {
      "type": "object",
      "required": ["id", "name", "version"],
      "additionalProperties": true,
      "description": "A versioned, machine-checkable promise about an entity's behavior. Referenced by DQL blocks via the `datalex_contract` field. See docs/interop.md.",
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-zA-Z][a-zA-Z0-9_]*\\.[A-Z][A-Za-z0-9]*\\.[a-z][a-z0-9_]*$",
          "description": "Stable id of the form <domain>.<Entity>.<contract_name>."
        },
        "name": {"type": "string"},
        "description": {"type": "string"},
        "version": {"type": "integer", "minimum": 1},
        "signature": {
          "type": "object",
          "properties": {
            "inputs": {
              "type": "array",
              "items": {"$ref": "#/$defs/contractParam"}
            },
            "outputs": {
              "type": "array",
              "items": {"$ref": "#/$defs/contractParam"}
            }
          }
        },
        "owner": {"type": "string"},
        "tags": {"type": "array", "items": {"type": "string"}}
      }
    },
    "contractParam": {
      "type": "object",
      "required": ["name", "type"],
      "properties": {
        "name": {"type": "string"},
        "type": {"type": "string"},
        "description": {"type": "string"},
        "constraints": {"type": "array", "items": {"type": "string"}}
      }
    },
    "glossaryTerm": {
      "type": "object",
      "required": ["term", "definition"],
      "properties": {
        "term": {"type": "string"},
        "definition": {"type": "string"},
        "tags": {"type": "array", "items": {"type": "string"}},
        "related_fields": {"type": "array", "items": {"type": "string"}}
      }
    },
    "diagnostic": {
      "type": "object",
      "required": ["severity", "message"],
      "properties": {
        "severity": {"type": "string", "enum": ["info", "warning", "error"]},
        "message": {"type": "string"},
        "path": {"type": "string"},
        "code": {"type": "string"}
      }
    }
  }
}
