{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "version": {
      "$ref": "#/$defs/XtpVersion"
    }
  },
  "required": [
    "version"
  ],
  "allOf": [
    {
      "if": {
        "properties": {
          "version": {
            "const": "v0"
          }
        }
      },
      "then": {
        "properties": {
          "exports": {
            "type": "array",
            "items": {
              "type": "string",
              "pattern": "^[a-zA-Z_$][a-zA-Z0-9_$]*$"
            }
          },
          "version": {
            "const": "v0"
          }
        },
        "required": [
          "exports"
        ],
        "additionalProperties": false
      }
    },
    {
      "if": {
        "properties": {
          "version": {
            "const": "v1-draft"
          }
        }
      },
      "then": {
        "properties": {
          "version": {
            "$ref": "#/$defs/XtpVersion"
          },
          "exports": {
            "type": "object",
            "patternProperties": {
              "^[a-zA-Z_$][a-zA-Z0-9_$]*$": {
                "$ref": "#/$defs/Export"
              }
            },
            "additionalProperties": false
          },
          "imports": {
            "type": "object",
            "patternProperties": {
              "^[a-zA-Z_$][a-zA-Z0-9_$]*$": {
                "$ref": "#/$defs/Import"
              }
            },
            "additionalProperties": false
          },
          "components": {
            "type": "object",
            "properties": {
              "schemas": {
                "type": "object",
                "patternProperties": {
                  "^[a-zA-Z_$][a-zA-Z0-9_$]*$": {
                    "$ref": "#/$defs/Schema"
                  }
                },
                "additionalProperties": false
              }
            },
            "required": [
              "schemas"
            ],
            "additionalProperties": false
          }
        },
        "required": [
          "exports"
        ],
        "additionalProperties": false
      }
    }
  ],
  "$defs": {
    "XtpVersion": {
      "type": "string",
      "enum": [
        "v0",
        "v1-draft"
      ]
    },
    "Export": {
      "type": "object",
      "properties": {
        "description": {
          "type": "string"
        },
        "codeSamples": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/CodeSample"
          }
        },
        "input": {
          "$ref": "#/$defs/Parameter"
        },
        "output": {
          "$ref": "#/$defs/Parameter"
        }
      },
      "additionalProperties": false
    },
    "CodeSample": {
      "type": "object",
      "properties": {
        "lang": {
          "anyOf": [
            {
              "type": "string",
              "enum": [
                "typescript",
                "csharp",
                "zig",
                "rust",
                "go",
                "python",
                "c++"
              ]
            },
            {
              "type": "string"
            }
          ]
        },
        "source": {
          "type": "string"
        },
        "label": {
          "type": "string"
        }
      },
      "required": [
        "lang",
        "source"
      ],
      "additionalProperties": false
    },
    "Import": {
      "type": "object",
      "properties": {
        "description": {
          "type": "string"
        },
        "input": {
          "$ref": "#/$defs/Parameter"
        },
        "output": {
          "$ref": "#/$defs/Parameter"
        }
      },
      "additionalProperties": false
    },
    "Schema": {
      "oneOf": [
        {
          "$ref": "#/$defs/ObjectSchema"
        },
        {
          "$ref": "#/$defs/EnumSchema"
        }
      ]
    },
    "ObjectSchema": {
      "type": "object",
      "properties": {
        "description": {
          "type": "string"
        },
        "properties": {
          "type": "object",
          "patternProperties": {
            "^[a-zA-Z_$][a-zA-Z0-9_$]*$": {
              "$ref": "#/$defs/Property"
            }
          },
          "additionalProperties": false
        },
        "required": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      },
      "required": [
        "properties"
      ],
      "additionalProperties": false
    },
    "EnumSchema": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "string"
          ]
        },
        "description": {
          "type": "string"
        },
        "enum": {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^[a-zA-Z_$][a-zA-Z0-9_$]*$"
          }
        }
      },
      "required": [
        "enum"
      ],
      "additionalProperties": false
    },
    "Parameter": {
      "oneOf": [
        {
          "$ref": "#/$defs/ValueParameter"
        },
        {
          "$ref": "#/$defs/RefParameter"
        },
        {
          "$ref": "#/$defs/MapParameter"
        }
      ]
    },
    "RefParameter": {
      "type": "object",
      "properties": {
        "$ref": {
          "$ref": "#/$defs/SchemaReference"
        },
        "description": {
          "type": "string"
        },
        "nullable": {
          "type": "boolean",
          "default": false
        },
        "contentType": {
          "$ref": "#/$defs/ContentType"
        }
      },
      "required": [
        "$ref",
        "contentType"
      ],
      "additionalProperties": false
    },
    "ValueParameter": {
      "type": "object",
      "properties": {
        "contentType": {
          "$ref": "#/$defs/ContentType"
        },
        "type": {
          "$ref": "#/$defs/XtpType"
        },
        "format": {
          "$ref": "#/$defs/XtpFormat"
        },
        "nullable": {
          "type": "boolean",
          "default": false
        },
        "description": {
          "type": "string"
        },
        "items": {
          "type": "object",
          "$ref": "#/$defs/ArrayItem"
        }
      },
      "required": [
        "type",
        "contentType"
      ],
      "additionalProperties": false
    },
    "MapParameter": {
      "type": "object",
      "properties": {
        "type": {
          "const": "object"
        },
        "description": {
          "type": "string"
        },
        "additionalProperties": {
          "allOf": [
            {
              "$ref": "#/$defs/NonMapProperty"
            },
            {
              "type": "object",
              "properties": {
                "description": false
              },
              "additionalProperties": false
            }
          ]
        },
        "nullable": {
          "type": "boolean",
          "default": false
        },
        "contentType": {
          "$ref": "#/$defs/ContentType"
        }
      },
      "required": [
        "additionalProperties",
        "contentType"
      ]
    },
    "NonMapProperty": {
      "oneOf": [
        {
          "$ref": "#/$defs/ValueProperty"
        },
        {
          "$ref": "#/$defs/RefProperty"
        }
      ]
    },
    "Property": {
      "oneOf": [
        {
          "$ref": "#/$defs/ValueProperty"
        },
        {
          "$ref": "#/$defs/RefProperty"
        },
        {
          "$ref": "#/$defs/MapProperty"
        }
      ]
    },
    "ValueProperty": {
      "type": "object",
      "properties": {
        "type": {
          "$ref": "#/$defs/XtpType"
        },
        "format": {
          "$ref": "#/$defs/XtpFormat"
        },
        "nullable": {
          "type": "boolean",
          "default": false
        },
        "description": {
          "type": "string"
        },
        "items": {
          "type": "object",
          "$ref": "#/$defs/ArrayItem"
        }
      },
      "required": [
        "type"
      ],
      "additionalProperties": false
    },
    "MapProperty": {
      "type": "object",
      "properties": {
        "type": {
          "const": "object"
        },
        "description": {
          "type": "string"
        },
        "additionalProperties": {
          "allOf": [
            {
              "$ref": "#/$defs/NonMapProperty"
            },
            {
              "not": {
                "type": "object",
                "required": ["description"]
              }
            }
          ]
        },
        "nullable": {
          "type": "boolean",
          "default": false
        }
      },
      "required": [
        "additionalProperties"
      ],
      "additionalProperties": false
    },
    "RefProperty": {
      "type": "object",
      "properties": {
        "$ref": {
          "$ref": "#/$defs/SchemaReference"
        },
        "description": {
          "type": "string"
        },
        "nullable": {
          "type": "boolean",
          "default": false
        }
      },
      "required": [
        "$ref"
      ],
      "additionalProperties": false
    },
    "ContentType": {
      "type": "string",
      "enum": [
        "application/json",
        "application/x-binary",
        "text/plain; charset=utf-8"
      ]
    },
    "SchemaReference": {
      "type": "string",
      "pattern": "^#/components/schemas/[^/]+$"
    },
    "XtpType": {
      "type": "string",
      "enum": [
        "integer",
        "string",
        "number",
        "boolean",
        "object",
        "array",
        "buffer"
      ]
    },
    "XtpFormat": {
      "type": "string",
      "enum": [
        "int32",
        "int64",
        "float",
        "double",
        "date-time",
        "byte"
      ]
    },
    "ArrayItem": {
      "type": "object",
      "oneOf": [
        {
          "$ref": "#/$defs/ValueArrayItem"
        },
        {
          "$ref": "#/$defs/RefArrayItem"
        },
        {
          "$ref": "#/$defs/MapArrayItem"
        }
      ]
    },
    "ValueArrayItem": {
      "type": "object",
      "properties": {
        "type": {
          "$ref": "#/$defs/XtpType"
        },
        "format": {
          "$ref": "#/$defs/XtpFormat"
        },
        "nullable": {
          "type": "boolean"
        }
      },
      "required": [
        "type"
      ],
      "additionalProperties": false
    },
    "RefArrayItem": {
      "type": "object",
      "properties": {
        "$ref": {
          "$ref": "#/$defs/SchemaReference"
        },
        "nullable": {
          "type": "boolean",
          "default": false
        }
      },
      "required": [
        "$ref"
      ],
      "additionalProperties": false
    },
    "MapArrayItem": {
      "type": "object",
      "properties": {
        "type": {
          "const": "object"
        },
        "additionalProperties": {
          "allOf": [
            {
              "$ref": "#/$defs/NonMapProperty"
            },
            {
              "not": {
                "type": "object",
                "required": ["description"]
              }
            }
          ]
        }
      },
      "required": [
        "additionalProperties"
      ],
      "additionalProperties": false
    }
  }
}
