{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://documentationfirst.ai/contracts/xd-contract-1.0.schema.json",
  "title": "xd-contract 1.0 structural profile (no CEL execution)",
  "type": "object",
  "required": [
    "openapi",
    "info",
    "paths"
  ],
  "properties": {
    "openapi": {
      "type": "string",
      "pattern": "^3\\.1\\.\\d+$"
    },
    "info": {
      "type": "object",
      "properties": {
        "title": {
          "type": "string",
          "minLength": 1
        },
        "version": {
          "type": "string",
          "minLength": 1
        },
        "description": {
          "type": "string"
        },
        "x-reading-mode": {
          "const": "interpret + read"
        },
        "x-conventions": {
          "type": "object",
          "properties": {
            "name": {
              "const": "xd-contract"
            },
            "version": {
              "const": "1.0"
            },
            "url": {
              "type": "string"
            }
          },
          "required": [
            "name",
            "version"
          ],
          "additionalProperties": false
        },
        "x-params": {
          "type": "object",
          "additionalProperties": {
            "type": [
              "string",
              "number",
              "boolean"
            ]
          }
        }
      },
      "required": [
        "title",
        "version",
        "x-reading-mode",
        "x-conventions"
      ],
      "patternProperties": {
        "^x-(?!(?:reading-mode|conventions|params)$)": false
      }
    },
    "paths": {
      "type": "object",
      "patternProperties": {
        "^/": {
          "$ref": "#/$defs/path"
        }
      },
      "additionalProperties": false
    },
    "components": {
      "type": "object",
      "properties": {
        "schemas": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/schema"
          }
        },
        "parameters": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/parameter"
          }
        },
        "responses": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/response"
          }
        },
        "requestBodies": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/requestBody"
          }
        }
      }
    }
  },
  "$defs": {
    "path": {
      "type": "object",
      "properties": {
        "get": {
          "$ref": "#/$defs/operation"
        },
        "post": {
          "$ref": "#/$defs/operation"
        },
        "put": {
          "$ref": "#/$defs/operation"
        },
        "patch": {
          "$ref": "#/$defs/operation"
        },
        "delete": {
          "$ref": "#/$defs/operation"
        },
        "head": {
          "$ref": "#/$defs/operation"
        },
        "options": {
          "$ref": "#/$defs/operation"
        },
        "trace": {
          "$ref": "#/$defs/operation"
        },
        "summary": {
          "type": "string"
        },
        "description": {
          "type": "string"
        },
        "$ref": {
          "type": "string",
          "pattern": "^#/"
        },
        "servers": {
          "type": "array"
        },
        "parameters": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/parameter"
          }
        }
      },
      "additionalProperties": false
    },
    "operation": {
      "type": "object",
      "properties": {
        "operationId": {
          "type": "string",
          "minLength": 1
        },
        "summary": {
          "type": "string"
        },
        "description": {
          "type": "string"
        },
        "responses": {
          "type": "object",
          "minProperties": 1,
          "patternProperties": {
            "^(default|[1-5][0-9X]{2})$": {
              "$ref": "#/$defs/response"
            }
          },
          "additionalProperties": false
        },
        "x-preconditions": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "minItems": 1
        },
        "x-postconditions": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "minItems": 1
        },
        "x-on-error": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "code": {
                "type": "string",
                "minLength": 1
              },
              "recovery": {
                "type": "string",
                "minLength": 1
              }
            },
            "required": [
              "code",
              "recovery"
            ],
            "additionalProperties": false
          }
        },
        "x-state-machine": {
          "type": "object",
          "properties": {
            "initial": {
              "type": "string",
              "minLength": 1
            },
            "states": {
              "type": "array",
              "items": {
                "type": "string",
                "minLength": 1
              },
              "uniqueItems": true,
              "minItems": 1
            },
            "transitions": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "from": {
                    "type": "string",
                    "minLength": 1
                  },
                  "to": {
                    "type": "string",
                    "minLength": 1
                  },
                  "on": {
                    "enum": [
                      "trigger",
                      "success",
                      "error"
                    ]
                  },
                  "when": {
                    "type": "string",
                    "minLength": 1
                  }
                },
                "required": [
                  "from",
                  "to",
                  "on"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "initial",
            "states",
            "transitions"
          ],
          "additionalProperties": false
        },
        "x-rules": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "when": {
                "type": "string",
                "minLength": 1
              },
              "then": {
                "type": "string",
                "minLength": 1
              }
            },
            "required": [
              "when",
              "then"
            ],
            "additionalProperties": false
          }
        },
        "x-perf": {
          "type": "object",
          "properties": {
            "latencyMs": {
              "type": "number",
              "exclusiveMinimum": 0
            },
            "ratePerSecond": {
              "type": "number",
              "exclusiveMinimum": 0
            }
          },
          "required": [],
          "additionalProperties": false
        },
        "parameters": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/parameter"
          }
        },
        "requestBody": {
          "$ref": "#/$defs/requestBody"
        }
      },
      "required": [
        "operationId",
        "responses"
      ],
      "patternProperties": {
        "^x-(?!(?:preconditions|postconditions|on-error|state-machine|rules|perf)$)": false
      }
    },
    "schema": {
      "anyOf": [
        {
          "type": "boolean"
        },
        {
          "type": "object",
          "properties": {
            "type": {
              "oneOf": [
                {
                  "enum": [
                    "object",
                    "array",
                    "string",
                    "number",
                    "integer",
                    "boolean",
                    "null"
                  ]
                },
                {
                  "type": "array",
                  "items": {
                    "enum": [
                      "object",
                      "array",
                      "string",
                      "number",
                      "integer",
                      "boolean",
                      "null"
                    ]
                  },
                  "minItems": 1,
                  "uniqueItems": true
                }
              ]
            },
            "properties": {
              "type": "object",
              "additionalProperties": {
                "$ref": "#/$defs/schema"
              }
            },
            "items": {
              "$ref": "#/$defs/schema"
            },
            "required": {
              "type": "array",
              "items": {
                "type": "string",
                "minLength": 1
              },
              "uniqueItems": true
            },
            "allOf": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/schema"
              }
            },
            "anyOf": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/schema"
              }
            },
            "oneOf": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/schema"
              }
            },
            "x-invariants": {
              "type": "array",
              "items": {
                "type": "string",
                "minLength": 1
              },
              "minItems": 1
            },
            "x-source-field": {
              "type": "string",
              "minLength": 1
            },
            "x-rules": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "when": {
                    "type": "string",
                    "minLength": 1
                  },
                  "then": {
                    "type": "string",
                    "minLength": 1
                  }
                },
                "required": [
                  "when",
                  "then"
                ],
                "additionalProperties": false
              }
            },
            "prefixItems": {
              "type": "array",
              "items": {
                "$ref": "#/$defs/schema"
              }
            },
            "additionalProperties": {
              "anyOf": [
                {
                  "type": "boolean"
                },
                {
                  "$ref": "#/$defs/schema"
                }
              ]
            },
            "minimum": {
              "type": "number"
            },
            "maximum": {
              "type": "number"
            },
            "exclusiveMinimum": {
              "type": "number"
            },
            "exclusiveMaximum": {
              "type": "number"
            },
            "multipleOf": {
              "type": "number"
            },
            "minItems": {
              "type": "integer",
              "minimum": 0
            },
            "maxItems": {
              "type": "integer",
              "minimum": 0
            },
            "minLength": {
              "type": "integer",
              "minimum": 0
            },
            "maxLength": {
              "type": "integer",
              "minimum": 0
            },
            "minProperties": {
              "type": "integer",
              "minimum": 0
            },
            "maxProperties": {
              "type": "integer",
              "minimum": 0
            },
            "pattern": {
              "type": "string"
            },
            "uniqueItems": {
              "type": "boolean"
            }
          },
          "required": [],
          "patternProperties": {
            "^x-(?!(?:invariants|source-field|rules)$)": false
          }
        }
      ]
    },
    "reference": {
      "type": "object",
      "required": [
        "$ref"
      ],
      "properties": {
        "$ref": {
          "type": "string",
          "pattern": "^#/"
        }
      },
      "patternProperties": {
        "^x-": false
      }
    },
    "media": {
      "type": "object",
      "properties": {
        "schema": {
          "$ref": "#/$defs/schema"
        }
      }
    },
    "response": {
      "anyOf": [
        {
          "$ref": "#/$defs/reference"
        },
        {
          "type": "object",
          "required": [
            "description"
          ],
          "properties": {
            "description": {
              "type": "string"
            },
            "content": {
              "type": "object",
              "additionalProperties": {
                "$ref": "#/$defs/media"
              }
            }
          },
          "patternProperties": {
            "^x-": false
          }
        }
      ]
    },
    "parameter": {
      "anyOf": [
        {
          "$ref": "#/$defs/reference"
        },
        {
          "type": "object",
          "required": [
            "name",
            "in"
          ],
          "properties": {
            "name": {
              "type": "string",
              "minLength": 1
            },
            "in": {
              "enum": [
                "query",
                "header",
                "path",
                "cookie"
              ]
            },
            "required": {
              "type": "boolean"
            },
            "schema": {
              "$ref": "#/$defs/schema"
            },
            "content": {
              "type": "object",
              "additionalProperties": {
                "$ref": "#/$defs/media"
              }
            }
          },
          "patternProperties": {
            "^x-": false
          }
        }
      ]
    },
    "requestBody": {
      "anyOf": [
        {
          "$ref": "#/$defs/reference"
        },
        {
          "type": "object",
          "required": [
            "content"
          ],
          "properties": {
            "required": {
              "type": "boolean"
            },
            "content": {
              "type": "object",
              "additionalProperties": {
                "$ref": "#/$defs/media"
              }
            }
          },
          "patternProperties": {
            "^x-": false
          }
        }
      ]
    }
  },
  "patternProperties": {
    "^x-": false
  }
}
