{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "PKI MM Extension schema",
  "description": "Schema for extensions to the PKI Maturity Model (extensions/catalog/<id>/<id>-extension.yaml). See https://pkic.org/wg/pkimm/model/extensions/ for the framework documentation.",
  "version": "1.0.0",
  "type": "object",
  "required": ["schemaVersion", "extension", "relevance", "overlays"],
  "properties": {
    "schemaVersion": {
      "description": "Version of the extension schema this file conforms to. Used by tools to load the matching schema for validation.",
      "type": "string",
      "const": "1.0.0"
    },
    "extension": {
      "type": "object",
      "required": ["id", "name", "version", "description", "compatibility", "documentation"],
      "properties": {
        "id": { "type": "string" },
        "name": { "type": "string" },
        "version": {
          "type": "string",
          "pattern": "^\\d+\\.\\d+\\.\\d+$"
        },
        "description": { "type": "string" },
        "documentation": {
          "type": "string",
          "format": "uri"
        },
        "compatibility": {
          "description": "List of PKI MM model versions this extension is compatible with.",
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string",
            "pattern": "^\\d+\\.\\d+\\.\\d+$"
          }
        },
        "floorScore": {
          "description": "When true, the extension reports a floor score (minimum extension category level) alongside the aggregate.",
          "type": "boolean"
        }
      }
    },
    "relevance": {
      "type": "object",
      "properties": {
        "modules": {
          "type": "array",
          "maxItems": 4,
          "allOf": [
            { "contains": { "properties": { "id": { "const": "G" } } }, "minContains": 0, "maxContains": 1 },
            { "contains": { "properties": { "id": { "const": "M" } } }, "minContains": 0, "maxContains": 1 },
            { "contains": { "properties": { "id": { "const": "O" } } }, "minContains": 0, "maxContains": 1 },
            { "contains": { "properties": { "id": { "const": "R" } } }, "minContains": 0, "maxContains": 1 }
          ],
          "items": {
            "type": "object",
            "required": ["id", "categories"],
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^[GMOR]$"
              },
              "categories": {
                "type": "array",
                "uniqueItems": true,
                "items": {
                  "type": "object",
                  "required": ["id", "weight", "guidance", "assessment", "references", "levels"],
                  "properties": {
                    "id": {
                      "type": "string",
                      "pattern": "^[a-z][a-z0-9-]*$"
                    },
                    "weight": { "type": "integer", "minimum": 0 },
                    "guidance": { "type": "string" },
                    "assessment": { "type": "string" },
                    "references": {
                      "type": "array",
                      "items": { "type": "string", "pattern": "^[a-z][a-z0-9-]*$" },
                      "description": "Catalog IDs resolving against the extension's local references block or the global pkimm-references.yaml catalog."
                    },
                    "levels": {
                      "type": "array",
                      "minItems": 5,
                      "maxItems": 5,
                      "items": {
                        "type": "object",
                        "required": ["number", "name", "description"],
                        "properties": {
                          "number": {
                            "type": "integer",
                            "minimum": 1,
                            "maximum": 5
                          },
                          "name": { "type": "string" },
                          "description": { "type": "string" }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "overlays": {
      "type": "object",
      "properties": {
        "modules": {
          "type": "array",
          "maxItems": 4,
          "allOf": [
            { "contains": { "properties": { "id": { "const": "G" } } }, "minContains": 0, "maxContains": 1 },
            { "contains": { "properties": { "id": { "const": "M" } } }, "minContains": 0, "maxContains": 1 },
            { "contains": { "properties": { "id": { "const": "O" } } }, "minContains": 0, "maxContains": 1 },
            { "contains": { "properties": { "id": { "const": "R" } } }, "minContains": 0, "maxContains": 1 }
          ],
          "items": {
            "type": "object",
            "required": ["id", "categories"],
            "properties": {
              "id": {
                "type": "string",
                "pattern": "^[GMOR]$"
              },
              "categories": {
                "type": "array",
                "uniqueItems": true,
                "items": {
                  "type": "object",
                  "required": ["id"],
                  "properties": {
                    "id": {
                      "type": "string",
                      "pattern": "^[a-z][a-z0-9-]*$"
                    },
                    "type": {
                      "type": "string",
                      "enum": ["multiplier", "addition", "override"]
                    },
                    "multiplier": { "type": "number", "minimum": 0 },
                    "addition": { "type": "number" },
                    "override": { "type": "number", "minimum": 0 },
                    "rationale": { "type": "string" },
                    "requirements": {
                      "type": "array",
                      "uniqueItems": true,
                      "items": {
                        "type": "object",
                        "required": ["id", "type", "rationale"],
                        "properties": {
                          "id": {
                            "type": "string",
                            "pattern": "^[a-z][a-z0-9-]*$"
                          },
                          "type": {
                            "type": "string",
                            "enum": ["multiplier", "addition", "override"]
                          },
                          "multiplier": { "type": "number", "minimum": 0 },
                          "addition": { "type": "number" },
                          "override": { "type": "number", "minimum": 0 },
                          "rationale": { "type": "string" }
                        },
                        "oneOf": [
                          {
                            "properties": { "type": { "const": "multiplier" } },
                            "required": ["multiplier"],
                            "allOf": [
                              { "not": { "required": ["addition"] } },
                              { "not": { "required": ["override"] } }
                            ]
                          },
                          {
                            "properties": { "type": { "const": "addition" } },
                            "required": ["addition"],
                            "allOf": [
                              { "not": { "required": ["multiplier"] } },
                              { "not": { "required": ["override"] } }
                            ]
                          },
                          {
                            "properties": { "type": { "const": "override" } },
                            "required": ["override"],
                            "allOf": [
                              { "not": { "required": ["multiplier"] } },
                              { "not": { "required": ["addition"] } }
                            ]
                          }
                        ]
                      }
                    }
                  },
                  "oneOf": [
                    {
                      "properties": { "type": { "const": "multiplier" } },
                      "required": ["type", "multiplier"],
                      "allOf": [
                        { "not": { "required": ["addition"] } },
                        { "not": { "required": ["override"] } }
                      ]
                    },
                    {
                      "properties": { "type": { "const": "addition" } },
                      "required": ["type", "addition"],
                      "allOf": [
                        { "not": { "required": ["multiplier"] } },
                        { "not": { "required": ["override"] } }
                      ]
                    },
                    {
                      "properties": { "type": { "const": "override" } },
                      "required": ["type", "override"],
                      "allOf": [
                        { "not": { "required": ["multiplier"] } },
                        { "not": { "required": ["addition"] } }
                      ]
                    },
                    {
                      "allOf": [
                        { "not": { "required": ["type"] } },
                        { "not": { "required": ["multiplier"] } },
                        { "not": { "required": ["addition"] } },
                        { "not": { "required": ["override"] } }
                      ]
                    }
                  ]
                }
              }
            }
          }
        }
      }
    },
    "references": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["id", "title"],
        "properties": {
          "id": { "type": "string", "pattern": "^[a-z][a-z0-9-]*$" },
          "title": { "type": "string" },
          "authority": { "type": "string" },
          "regions": {
            "type": "array",
            "items": {
              "type": "string",
              "pattern": "^[A-Z][A-Z0-9-]*$",
              "description": "ISO 3166-1 alpha-2 country code (e.g., US, FR, JP) or supra-national token (e.g., GLOBAL, EU, EEA)."
            }
          },
          "url": { "type": "string", "format": "uri" },
          "deprecated": { "type": "boolean" },
          "supersededBy": { "type": "string", "pattern": "^[a-z][a-z0-9-]*$" }
        }
      }
    }
  }
}
