{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "kind",
    "name",
    "version",
    "purpose",
    "types",
    "functions",
    "rules"
  ],
  "properties": {
    "$schema": {
      "type": "string"
    },
    "kind": {
      "const": "service"
    },
    "name": {
      "type": "string",
      "pattern": "^[A-Za-z][A-Za-z0-9_-]*$"
    },
    "version": {
      "type": "string"
    },
    "purpose": {
      "type": "string",
      "minLength": 1
    },
    "types": {
      "type": "string"
    },
    "declarationSchema": {
      "type": "string"
    },
    "lookupParameters": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "required"],
      "properties": {
        "type": { "$ref": "#/$defs/Type" },
        "required": { "type": "boolean" }
      }
    },
    "functions": {
      "type": "object",
      "additionalProperties": {
        "$ref": "#/$defs/Function"
      }
    },
    "rules": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "references": {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  },
  "$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"
        }
      }
    }
  }
}
