{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:standards:schema:standards-manifest:v3",
  "title": "Corporate standards manifest",
  "description": "Structural contract for .standards/standards.yaml.",
  "$comment": "Machine-readable schema required by STD-MAN-001. Its constraints derive from STD-MAN-001 through STD-MAN-005 and DEV-PROF-001 through DEV-PROF-003.",
  "type": "object",
  "required": [
    "schema_version",
    "standards",
    "project",
    "owner",
    "components",
    "capabilities",
    "targets"
  ],
  "properties": {
    "schema_version": {
      "const": 3
    },
    "standards": {
      "type": "object",
      "required": [
        "tag",
        "commit_sha"
      ],
      "properties": {
        "tag": {
          "type": "string",
          "pattern": "^\\S+$"
        },
        "commit_sha": {
          "type": "string",
          "pattern": "^[0-9a-f]{40}$"
        }
      },
      "additionalProperties": false
    },
    "components": {
      "type": "array",
      "minItems": 1,
      "items": {
        "type": "object",
        "required": [
          "id",
          "kind"
        ],
        "properties": {
          "id": {
            "$ref": "#/$defs/localId"
          },
          "kind": {
            "enum": [
              "backend",
              "frontend",
              "worker",
              "mobile-ios",
              "mobile-android",
              "data-pipeline",
              "public-cli",
              "public-sdk"
            ]
          }
        },
        "additionalProperties": false
      }
    },
    "capabilities": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "id",
          "type"
        ],
        "properties": {
          "id": {
            "$ref": "#/$defs/localId"
          },
          "type": {
            "enum": [
              "ai-assisted-development",
              "http-api",
              "graphql-api",
              "grpc-api",
              "websocket",
              "webhook-producer",
              "webhook-consumer",
              "event-producer",
              "event-consumer",
              "nats",
              "background-processing",
              "stateful-storage",
              "data-migrations",
              "audit-events",
              "user-authentication",
              "authorization",
              "local-offline-storage",
              "push-notifications",
              "deep-links",
              "analytics",
              "container-image"
            ]
          },
          "providers": {
            "type": "array",
            "minItems": 1,
            "uniqueItems": true,
            "items": {
              "$ref": "#/$defs/localId"
            }
          },
          "consumers": {
            "type": "array",
            "minItems": 1,
            "uniqueItems": true,
            "items": {
              "$ref": "#/$defs/localId"
            }
          }
        },
        "anyOf": [
          {
            "required": [
              "providers"
            ]
          },
          {
            "required": [
              "consumers"
            ]
          }
        ],
        "additionalProperties": false
      }
    },
    "exceptions": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "requirement_id",
          "adr"
        ],
        "properties": {
          "requirement_id": {
            "type": "string",
            "pattern": "^[A-Z]+(?:-[A-Z]+)*-[0-9]{3}$"
          },
          "adr": {
            "type": "string",
            "pattern": "^(?!/)(?!.*(?:^|/)[.][.](?:/|$)).+[.]md$"
          }
        },
        "additionalProperties": false
      }
    },
    "project": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9._/-]*$"
    },
    "owner": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "name",
        "contact"
      ],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "contact": {
          "type": "string",
          "minLength": 1
        },
        "escalation": {
          "type": "string",
          "minLength": 1
        }
      }
    },
    "targets": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/target"
      }
    }
  },
  "$defs": {
    "localId": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$"
    },
    "target": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "name",
        "environment",
        "platform",
        "runtime",
        "availability",
        "delivery",
        "configuration",
        "capabilities"
      ],
      "properties": {
        "name": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9-]*$"
        },
        "environment": {
          "enum": [
            "local",
            "non-production",
            "production"
          ]
        },
        "platform": {
          "type": "string",
          "pattern": "^[a-z0-9][a-z0-9._/-]*$"
        },
        "runtime": {
          "enum": [
            "kubernetes",
            "compose"
          ]
        },
        "availability": {
          "enum": [
            "single-instance",
            "high-availability"
          ]
        },
        "failure_tolerance": {
          "type": "array",
          "minItems": 1,
          "uniqueItems": true,
          "items": {
            "enum": [
              "worker-node",
              "control-plane-node",
              "storage-node",
              "availability-zone"
            ]
          }
        },
        "delivery": {
          "enum": [
            "local",
            "gitlab-ci-agent",
            "gitlab-ci-runner",
            "gitops"
          ]
        },
        "configuration": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "project",
            "path"
          ],
          "properties": {
            "project": {
              "type": "string",
              "minLength": 1
            },
            "path": {
              "type": "string",
              "pattern": "^(?:\\.|[A-Za-z0-9._-]+(?:/[A-Za-z0-9._-]+)*)$",
              "not": {
                "pattern": "(^|/)\\.\\.($|/)"
              }
            }
          }
        },
        "namespace": {
          "type": "string",
          "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
        },
        "capabilities": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "enum": [
              "postgresql",
              "valkey",
              "nats-jetstream",
              "object-storage",
              "persistent-data",
              "longhorn",
              "public-ingress",
              "infrastructure-as-code"
            ]
          }
        },
        "risk_acceptance": {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "owner",
            "reason",
            "review_date"
          ],
          "properties": {
            "owner": {
              "type": "string",
              "minLength": 1
            },
            "reason": {
              "type": "string",
              "minLength": 1
            },
            "review_date": {
              "type": "string",
              "format": "date"
            }
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "availability": {
                "const": "high-availability"
              }
            },
            "required": [
              "availability"
            ]
          },
          "then": {
            "required": [
              "failure_tolerance"
            ],
            "properties": {
              "failure_tolerance": {
                "contains": {
                  "const": "worker-node"
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "runtime": {
                "const": "kubernetes"
              }
            },
            "required": [
              "runtime"
            ]
          },
          "then": {
            "required": [
              "namespace"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "environment": {
                "const": "local"
              }
            },
            "required": [
              "environment"
            ]
          },
          "then": {
            "properties": {
              "delivery": {
                "const": "local"
              }
            }
          },
          "else": {
            "properties": {
              "delivery": {
                "enum": [
                  "gitlab-ci-agent",
                  "gitlab-ci-runner",
                  "gitops"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "runtime": {
                "const": "compose"
              },
              "environment": {
                "enum": [
                  "non-production",
                  "production"
                ]
              }
            },
            "required": [
              "runtime",
              "environment"
            ]
          },
          "then": {
            "properties": {
              "availability": {
                "const": "single-instance"
              },
              "delivery": {
                "const": "gitlab-ci-runner"
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "runtime": {
                "const": "kubernetes"
              },
              "environment": {
                "enum": [
                  "non-production",
                  "production"
                ]
              }
            },
            "required": [
              "runtime",
              "environment"
            ]
          },
          "then": {
            "properties": {
              "delivery": {
                "enum": [
                  "gitlab-ci-agent",
                  "gitops"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "runtime": {
                "const": "compose"
              },
              "environment": {
                "const": "production"
              }
            },
            "required": [
              "runtime",
              "environment"
            ]
          },
          "then": {
            "required": [
              "risk_acceptance"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "capabilities": {
                "contains": {
                  "const": "longhorn"
                }
              }
            },
            "required": [
              "capabilities"
            ]
          },
          "then": {
            "properties": {
              "runtime": {
                "const": "kubernetes"
              },
              "capabilities": {
                "contains": {
                  "const": "persistent-data"
                }
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "runtime": {
                "const": "compose"
              }
            },
            "required": [
              "runtime"
            ]
          },
          "then": {
            "properties": {
              "availability": {
                "const": "single-instance"
              }
            }
          }
        }
      ]
    }
  },
  "additionalProperties": false,
  "allOf": [
    {
      "if": {
        "properties": {
          "targets": {
            "contains": {
              "type": "object",
              "properties": {
                "environment": {
                  "const": "production"
                }
              },
              "required": [
                "environment"
              ]
            }
          }
        },
        "required": [
          "targets"
        ]
      },
      "then": {
        "properties": {
          "owner": {
            "required": [
              "escalation"
            ]
          }
        }
      }
    }
  ]
}
