{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "namespace",
    "version",
    "types"
  ],
  "properties": {
    "$schema": {
      "type": "string"
    },
    "namespace": {
      "type": "string"
    },
    "version": {
      "type": "string"
    },
    "types": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "name",
          "kind",
          "purpose"
        ],
        "properties": {
          "name": {
            "type": "string",
            "pattern": "^[A-Za-z][A-Za-z0-9_-]*\\.[A-Za-z][A-Za-z0-9_-]*$"
          },
          "kind": {
            "enum": [
              "primitive",
              "object",
              "array",
              "union",
              "function",
              "handle"
            ]
          },
          "purpose": {
            "type": "string",
            "minLength": 1
          },
          "schema": {
            "type": "object"
          },
          "methods": {
            "type": "object",
            "additionalProperties": {
              "$ref": "#/$defs/Function"
            }
          },
          "fields": {
            "type": "object",
            "additionalProperties": {
              "$ref": "#/$defs/Type"
            }
          },
          "parameters": {
            "type": "array",
            "items": {
              "$ref": "#/$defs/Parameter"
            }
          },
          "returns": {
            "$ref": "#/$defs/Type"
          },
          "items": {
            "$ref": "#/$defs/Type"
          },
          "iterableOf": {
            "$ref": "#/$defs/Type"
          },
          "alternatives": {
            "type": "array",
            "items": {
              "$ref": "#/$defs/Type"
            }
          },
          "minimum": {
            "type": "string"
          },
          "maximum": {
            "type": "string"
          },
          "representation": {
            "type": "string"
          },
          "minimumItems": {
            "type": "integer",
            "minimum": 0
          }
        },
        "allOf": [
          {
            "if": {"properties": {"kind": {"const": "function"}}},
            "then": {"required": ["parameters", "returns"]}
          },
          {
            "if": {
              "properties": {
                "kind": {
                  "const": "object"
                }
              }
            },
            "then": {
              "required": [
                "schema"
              ]
            }
          },
          {
            "if": {
              "properties": {
                "kind": {
                  "const": "array"
                }
              }
            },
            "then": {
              "required": [
                "items"
              ]
            }
          },
          {
            "if": {
              "properties": {
                "kind": {
                  "const": "union"
                }
              }
            },
            "then": {
              "required": [
                "alternatives"
              ]
            }
          }
        ],
        "additionalProperties": false
      }
    }
  },
  "$defs": {
    "Type": {
      "anyOf": [
        {
          "type": "string",
          "pattern": "^[A-Za-z][A-Za-z0-9_-]*\\.[A-Za-z][A-Za-z0-9_-]*$"
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "arrayOf"
          ],
          "properties": {
            "arrayOf": {
              "$ref": "#/$defs/Type"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "nullable"
          ],
          "properties": {
            "nullable": {
              "$ref": "#/$defs/Type"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "oneOf"
          ],
          "properties": {
            "oneOf": {
              "type": "array",
              "minItems": 2,
              "items": {
                "$ref": "#/$defs/Type"
              }
            }
          }
        }
      ]
    },
    "Parameter": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "name",
        "type",
        "required"
      ],
      "properties": {
        "name": {
          "type": "string",
          "pattern": "^[A-Za-z][A-Za-z0-9_]*$"
        },
        "type": {
          "$ref": "#/$defs/Type"
        },
        "required": {
          "type": "boolean"
        },
        "default": {},
        "variadic": {
          "type": "boolean"
        }
      }
    },
    "Function": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "parameters",
        "returns",
        "purpose"
      ],
      "properties": {
        "parameters": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/Parameter"
          }
        },
        "returns": {
          "$ref": "#/$defs/Type"
        },
        "purpose": {
          "type": "string",
          "minLength": 1
        },
        "statementOnly": {
          "type": "boolean"
        }
      }
    }
  }
}
