{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://markdown-contract-docs.pages.dev/schema/mcv2.json",
  "title": "markdown-contract declarative document (mcVersion 2)",
  "description": "The mcVersion 2 authoring vocabulary (D-0020): a kind: contract or kind: config YAML document. Schema nodes are a documented subset of JSON Schema 2020-12 compiled to Zod.",
  "type": "object",
  "required": ["mcVersion", "kind"],
  "properties": {
    "mcVersion": { "const": 2 },
    "kind": { "enum": ["contract", "config"] }
  },
  "allOf": [
    {
      "if": { "properties": { "kind": { "const": "contract" } } },
      "then": { "$ref": "#/$defs/contract" }
    },
    {
      "if": { "properties": { "kind": { "const": "config" } } },
      "then": { "$ref": "#/$defs/config" }
    }
  ],
  "$defs": {
    "contract": {
      "type": "object",
      "properties": {
        "mcVersion": true,
        "kind": true,
        "description": { "type": "string" },
        "frontmatter": { "$ref": "#/$defs/frontmatterNode" },
        "body": { "$ref": "#/$defs/bodyLevel" }
      },
      "additionalProperties": false
    },
    "frontmatterNode": {
      "description": "The frontmatter root: an object schema node with an explicit type: object (no null union at the root).",
      "allOf": [
        { "$ref": "#/$defs/objectSchemaNode" },
        { "type": "object", "properties": { "type": { "const": "object" } } }
      ]
    },
    "config": {
      "type": "object",
      "required": ["rules"],
      "properties": {
        "mcVersion": true,
        "kind": true,
        "description": { "type": "string" },
        "contracts": {
          "type": "object",
          "additionalProperties": { "type": "string" }
        },
        "rules": {
          "type": "array",
          "items": { "$ref": "#/$defs/configRule" }
        }
      },
      "additionalProperties": false
    },
    "configRule": {
      "type": "object",
      "required": ["include", "contract"],
      "properties": {
        "include": { "type": "array", "minItems": 1, "items": { "type": "string" } },
        "exclude": { "type": "array", "items": { "type": "string" } },
        "contract": {
          "anyOf": [
            {
              "type": "string",
              "description": "a contract name (from `contracts:`) or a .yaml/.yml path"
            },
            { "$ref": "#/$defs/inlineContract" }
          ]
        }
      },
      "additionalProperties": false
    },
    "inlineContract": {
      "type": "object",
      "properties": {
        "description": { "type": "string" },
        "frontmatter": { "$ref": "#/$defs/frontmatterNode" },
        "body": { "$ref": "#/$defs/bodyLevel" }
      },
      "additionalProperties": false
    },

    "schemaNode": {
      "description": "One schema node — the JSON Schema 2020-12 subset (exactly one base: type / enum / const).",
      "anyOf": [
        { "$ref": "#/$defs/stringSchemaNode" },
        { "$ref": "#/$defs/numberSchemaNode" },
        { "$ref": "#/$defs/booleanSchemaNode" },
        { "$ref": "#/$defs/arraySchemaNode" },
        { "$ref": "#/$defs/objectSchemaNode" },
        { "$ref": "#/$defs/enumSchemaNode" },
        { "$ref": "#/$defs/constSchemaNode" }
      ]
    },
    "stringType": {
      "description": "type: string, or its null-pairing union (either order — the only supported union form).",
      "enum": ["string", ["string", "null"], ["null", "string"]]
    },
    "numericType": {
      "enum": [
        "number",
        "integer",
        ["number", "null"],
        ["null", "number"],
        ["integer", "null"],
        ["null", "integer"]
      ]
    },
    "booleanType": {
      "enum": ["boolean", ["boolean", "null"], ["null", "boolean"]]
    },
    "arrayType": {
      "enum": ["array", ["array", "null"], ["null", "array"]]
    },
    "objectType": {
      "enum": ["object", ["object", "null"], ["null", "object"]]
    },
    "stringSchemaNode": {
      "type": "object",
      "required": ["type"],
      "properties": {
        "type": { "$ref": "#/$defs/stringType" },
        "minLength": { "type": "integer", "minimum": 0 },
        "maxLength": { "type": "integer", "minimum": 0 },
        "pattern": { "type": "string", "format": "regex" },
        "format": {
          "enum": [
            "email",
            "url",
            "uuid",
            "hostname",
            "datetime",
            "date",
            "time",
            "duration",
            "ipv4",
            "ipv6",
            "cidrv4",
            "cidrv6",
            "nanoid",
            "cuid",
            "cuid2",
            "ulid",
            "base64",
            "emoji",
            "e164"
          ]
        },
        "default": true,
        "description": { "type": "string" }
      },
      "additionalProperties": false
    },
    "numberSchemaNode": {
      "type": "object",
      "required": ["type"],
      "properties": {
        "type": { "$ref": "#/$defs/numericType" },
        "minimum": { "type": "number" },
        "maximum": { "type": "number" },
        "default": true,
        "description": { "type": "string" }
      },
      "additionalProperties": false
    },
    "booleanSchemaNode": {
      "type": "object",
      "required": ["type"],
      "properties": {
        "type": { "$ref": "#/$defs/booleanType" },
        "default": true,
        "description": { "type": "string" }
      },
      "additionalProperties": false
    },
    "arraySchemaNode": {
      "type": "object",
      "required": ["type", "items"],
      "properties": {
        "type": { "$ref": "#/$defs/arrayType" },
        "items": { "$ref": "#/$defs/schemaNode" },
        "minItems": { "type": "integer", "minimum": 0 },
        "maxItems": { "type": "integer", "minimum": 0 },
        "default": true,
        "description": { "type": "string" }
      },
      "additionalProperties": false
    },
    "objectSchemaNode": {
      "type": "object",
      "required": ["type", "properties"],
      "properties": {
        "type": { "$ref": "#/$defs/objectType" },
        "properties": {
          "type": "object",
          "additionalProperties": { "$ref": "#/$defs/schemaNode" }
        },
        "required": { "type": "array", "items": { "type": "string" } },
        "additionalProperties": { "type": "boolean" },
        "default": true,
        "description": { "type": "string" }
      },
      "additionalProperties": false
    },
    "enumSchemaNode": {
      "type": "object",
      "required": ["enum"],
      "properties": {
        "enum": { "type": "array", "minItems": 1, "items": { "type": "string" } },
        "default": true,
        "description": { "type": "string" }
      },
      "additionalProperties": false
    },
    "constSchemaNode": {
      "type": "object",
      "required": ["const"],
      "properties": {
        "const": { "type": ["string", "number", "boolean"] },
        "default": true,
        "description": { "type": "string" }
      },
      "additionalProperties": false
    },

    "bodyLevel": {
      "type": "object",
      "required": ["sections"],
      "properties": {
        "order": { "enum": ["none", "recognized-relative", "strict"] },
        "additionalSections": { "type": "boolean" },
        "sections": { "type": "array", "items": { "$ref": "#/$defs/bodyNode" } },
        "description": { "type": "string" },
        "requires": { "type": "array", "items": { "$ref": "#/$defs/matchSpec" } },
        "forbids": { "type": "array", "items": { "$ref": "#/$defs/matchSpec" } }
      },
      "additionalProperties": false
    },
    "bodyNode": {
      "anyOf": [
        { "$ref": "#/$defs/sectionNode" },
        { "$ref": "#/$defs/oneOfNode" },
        { "$ref": "#/$defs/gapNode" }
      ]
    },
    "sectionNode": {
      "type": "object",
      "required": ["section"],
      "properties": {
        "section": { "type": "string" },
        "aliases": { "type": "array", "items": { "type": "string" } },
        "anchor": { "type": "string" },
        "minContains": { "type": "integer", "minimum": 0 },
        "maxContains": { "type": "integer", "minimum": 1 },
        "content": { "$ref": "#/$defs/content" },
        "sections": { "type": "array", "items": { "$ref": "#/$defs/bodyNode" } },
        "order": { "enum": ["none", "recognized-relative", "strict"] },
        "additionalSections": { "type": "boolean" },
        "requires": { "type": "array", "items": { "$ref": "#/$defs/matchSpec" } },
        "forbids": { "type": "array", "items": { "$ref": "#/$defs/matchSpec" } },
        "description": { "type": "string" }
      },
      "dependentRequired": {
        "order": ["sections"],
        "additionalSections": ["sections"]
      },
      "additionalProperties": false
    },
    "oneOfNode": {
      "type": "object",
      "required": ["oneOf"],
      "properties": {
        "oneOf": { "type": "array", "minItems": 1, "items": { "type": "string" } },
        "anchor": { "type": "string" },
        "minContains": { "type": "integer", "minimum": 0 },
        "maxContains": { "type": "integer", "minimum": 1 },
        "content": { "$ref": "#/$defs/content" },
        "sections": { "type": "array", "items": { "$ref": "#/$defs/bodyNode" } },
        "order": { "enum": ["none", "recognized-relative", "strict"] },
        "additionalSections": { "type": "boolean" },
        "requires": { "type": "array", "items": { "$ref": "#/$defs/matchSpec" } },
        "forbids": { "type": "array", "items": { "$ref": "#/$defs/matchSpec" } },
        "description": { "type": "string" }
      },
      "dependentRequired": {
        "order": ["sections"],
        "additionalSections": ["sections"]
      },
      "additionalProperties": false
    },
    "gapNode": {
      "type": "object",
      "required": ["gap"],
      "properties": {
        "gap": {
          "anyOf": [
            { "type": "null" },
            {
              "type": "object",
              "properties": {
                "min": { "type": "integer", "minimum": 0 },
                "max": { "type": "integer", "minimum": 0 }
              },
              "additionalProperties": false
            }
          ]
        }
      },
      "additionalProperties": false
    },

    "content": {
      "description": "A single content leaf, or a map of ^anchor-named leaves.",
      "anyOf": [
        { "$ref": "#/$defs/leaf" },
        {
          "type": "object",
          "additionalProperties": { "$ref": "#/$defs/leaf" }
        }
      ]
    },
    "leaf": {
      "anyOf": [
        {
          "type": "object",
          "required": ["maxWords"],
          "properties": { "maxWords": { "type": "number" } },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["code"],
          "properties": {
            "code": {
              "anyOf": [
                { "type": "null" },
                {
                  "type": "object",
                  "properties": {
                    "lang": { "type": "string" },
                    "description": { "type": "string" }
                  },
                  "additionalProperties": false
                }
              ]
            }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["table"],
          "properties": {
            "table": {
              "type": "object",
              "required": ["columns"],
              "properties": {
                "columns": { "type": "array", "items": { "type": "string" } },
                "cells": {
                  "type": "object",
                  "additionalProperties": { "$ref": "#/$defs/schemaNode" }
                },
                "minRows": { "type": "integer", "minimum": 0 },
                "anchor": { "type": "string" },
                "extraColumns": { "enum": ["ignore", "error"] },
                "description": { "type": "string" }
              },
              "additionalProperties": false
            }
          },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["list"],
          "properties": {
            "list": {
              "type": "object",
              "properties": {
                "items": {
                  "anyOf": [{ "const": "checkbox" }, { "$ref": "#/$defs/schemaNode" }]
                },
                "minItems": { "type": "integer", "minimum": 0 },
                "ordered": { "type": "boolean" },
                "description": { "type": "string" }
              },
              "additionalProperties": false
            }
          },
          "additionalProperties": false
        }
      ]
    },

    "matchSpec": {
      "type": "object",
      "properties": {
        "pattern": { "type": "string" },
        "regex": { "type": "string", "format": "regex" },
        "normalize": { "type": "boolean" },
        "ignoreCase": { "type": "boolean" },
        "min": { "type": "integer", "minimum": 0 },
        "max": { "type": "integer", "minimum": 0 },
        "id": { "type": "string" },
        "note": { "type": "string" },
        "level": { "enum": ["error", "warn"] }
      },
      "oneOf": [{ "required": ["pattern"] }, { "required": ["regex"] }],
      "additionalProperties": false
    }
  }
}
