{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://standards.rootblocks.com/schemas/buildpacks/buildpack-project.v1.schema.json",
  "title": "RootBlocks BuildPack Project v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schemaVersion",
    "packId",
    "version",
    "standard",
    "profile",
    "sourceMode",
    "visibility",
    "owner",
    "rootLayout",
    "packageIr",
    "buildPacks",
    "components",
    "registration",
    "packageArtifacts",
    "tests",
    "rbstdSync",
    "compatibility",
    "evidence",
    "build"
  ],
  "properties": {
    "schemaVersion": {
      "const": "rootblocks.buildpack-project/v1"
    },
    "packId": {
      "$ref": "#/$defs/id"
    },
    "version": {
      "$ref": "#/$defs/semver"
    },
    "standard": {
      "$ref": "#/$defs/standardRef"
    },
    "profile": {
      "$ref": "#/$defs/profileRef"
    },
    "sourceMode": {
      "enum": ["builderx-compiler", "agent-generated", "manual-governed", "imported"]
    },
    "visibility": {
      "enum": ["public", "organization-private", "project-local", "personal-private"]
    },
    "owner": {
      "$ref": "#/$defs/owner"
    },
    "rootLayout": {
      "$ref": "#/$defs/rootLayout"
    },
    "packageIr": {
      "$ref": "#/$defs/packageIr"
    },
    "buildPacks": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/buildPackRef"
      }
    },
    "components": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/component"
      }
    },
    "registration": {
      "$ref": "#/$defs/registration"
    },
    "packageArtifacts": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/packageArtifact"
      }
    },
    "tests": {
      "$ref": "#/$defs/tests"
    },
    "rbstdSync": {
      "$ref": "#/$defs/rbstdSync"
    },
    "compatibility": {
      "$ref": "#/$defs/compatibility"
    },
    "evidence": {
      "$ref": "#/$defs/evidencePolicy"
    },
    "build": {
      "$ref": "#/$defs/build"
    }
  },
  "allOf": [
    {
      "properties": {
        "rootLayout": {
          "properties": {
            "rootFolders": {
              "allOf": [
                {
                  "contains": {
                    "properties": {
                      "path": {
                        "const": "src"
                      }
                    },
                    "required": ["path"]
                  }
                },
                {
                  "contains": {
                    "properties": {
                      "path": {
                        "const": "tests"
                      }
                    },
                    "required": ["path"]
                  }
                },
                {
                  "contains": {
                    "properties": {
                      "path": {
                        "const": "builderx"
                      }
                    },
                    "required": ["path"]
                  }
                },
                {
                  "contains": {
                    "properties": {
                      "path": {
                        "const": "docs"
                      }
                    },
                    "required": ["path"]
                  }
                },
                {
                  "contains": {
                    "properties": {
                      "path": {
                        "const": "dist"
                      }
                    },
                    "required": ["path"]
                  }
                }
              ]
            },
            "sourceProjects": {
              "allOf": [
                {
                  "contains": {
                    "properties": {
                      "role": {
                        "const": "abstractions"
                      }
                    },
                    "required": ["role"]
                  }
                },
                {
                  "contains": {
                    "properties": {
                      "role": {
                        "const": "components"
                      }
                    },
                    "required": ["role"]
                  }
                },
                {
                  "contains": {
                    "properties": {
                      "role": {
                        "const": "registration"
                      }
                    },
                    "required": ["role"]
                  }
                },
                {
                  "contains": {
                    "properties": {
                      "role": {
                        "const": "workflows"
                      }
                    },
                    "required": ["role"]
                  }
                }
              ]
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "sourceMode": {
            "const": "builderx-compiler"
          }
        },
        "required": ["sourceMode"]
      },
      "then": {
        "properties": {
          "packageArtifacts": {
            "contains": {
              "properties": {
                "kind": {
                  "const": "builderx-buildpack"
                }
              },
              "required": ["kind"]
            }
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "sourceMode": {
            "const": "agent-generated"
          }
        },
        "required": ["sourceMode"]
      },
      "then": {
        "properties": {
          "packageArtifacts": {
            "contains": {
              "properties": {
                "kind": {
                  "enum": ["nuget", "npm", "pypi", "maven", "gradle", "cargo", "go-module", "composer", "gem", "docker-oci", "custom"]
                }
              },
              "required": ["kind"]
            }
          }
        }
      }
    },
    {
      "properties": {
        "tests": {
          "properties": {
            "suites": {
              "allOf": [
                {
                  "contains": {
                    "properties": {
                      "type": {
                        "const": "component"
                      },
                      "required": {
                        "const": true
                      }
                    },
                    "required": ["type", "required"]
                  }
                },
                {
                  "contains": {
                    "properties": {
                      "type": {
                        "const": "contract"
                      },
                      "required": {
                        "const": true
                      }
                    },
                    "required": ["type", "required"]
                  }
                },
                {
                  "contains": {
                    "properties": {
                      "type": {
                        "const": "workflow-regression"
                      },
                      "required": {
                        "const": true
                      }
                    },
                    "required": ["type", "required"]
                  }
                }
              ]
            }
          }
        }
      }
    }
  ],
  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[a-z0-9][a-z0-9.-]{2,127}$"
    },
    "name": {
      "type": "string",
      "pattern": "^[A-Za-z0-9][A-Za-z0-9_.-]{1,160}$"
    },
    "semver": {
      "type": "string",
      "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-[A-Za-z0-9.-]+)?(\\+[A-Za-z0-9.-]+)?$"
    },
    "hash": {
      "type": "string",
      "pattern": "^[a-f0-9]{64,128}$"
    },
    "path": {
      "type": "string",
      "pattern": "^[A-Za-z0-9_./@*-]+$"
    },
    "standardRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["bundleId", "version", "hash"],
      "properties": {
        "bundleId": {
          "$ref": "#/$defs/id"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "profileRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["profileId", "version", "path", "hash"],
      "properties": {
        "profileId": {
          "enum": ["component-pack", "custom"]
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "path": {
          "type": "string",
          "pattern": "^profiles/architecture/.+\\.json$"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "owner": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "id"],
      "properties": {
        "type": {
          "enum": ["rootblocks", "organization", "project", "personal"]
        },
        "id": {
          "$ref": "#/$defs/id"
        }
      }
    },
    "rootLayout": {
      "type": "object",
      "additionalProperties": false,
      "required": ["rootFolders", "sourceProjects"],
      "properties": {
        "rootFolders": {
          "type": "array",
          "minItems": 5,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["path", "purpose", "retention"],
            "properties": {
              "path": {
                "enum": ["src", "tests", "builderx", "docs", "dist"]
              },
              "purpose": {
                "enum": ["source", "tests", "builderx-metadata", "docs", "reproducible-output"]
              },
              "retention": {
                "enum": ["mandatory", "output-only"]
              }
            }
          }
        },
        "sourceProjects": {
          "type": "array",
          "minItems": 4,
          "items": {
            "$ref": "#/$defs/sourceProject"
          }
        }
      }
    },
    "sourceProject": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "role", "path", "dependencyPolicy"],
      "properties": {
        "name": {
          "$ref": "#/$defs/name"
        },
        "role": {
          "enum": ["abstractions", "components", "registration", "workflows"]
        },
        "path": {
          "type": "string",
          "pattern": "^src/.+"
        },
        "dependencyPolicy": {
          "enum": ["public-contracts-only", "implements-contracts", "registration-only", "workflow-wrappers"]
        }
      }
    },
    "packageIr": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "components", "buildPackRefs", "publicContracts", "requiredSecrets", "capabilities"],
      "properties": {
        "path": {
          "type": "string",
          "pattern": "^builderx/inventory/package-ir\\.(json|yaml|yml)$"
        },
        "components": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/componentIrRef"
          }
        },
        "buildPackRefs": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "publicContracts": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/publicContract"
          }
        },
        "requiredSecrets": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/secretRequirement"
          }
        },
        "capabilities": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        }
      }
    },
    "componentIrRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["componentId", "manifestPath", "sourceRoot", "testRoot", "buildPackId"],
      "properties": {
        "componentId": {
          "$ref": "#/$defs/id"
        },
        "manifestPath": {
          "type": "string",
          "pattern": "^(builderx|components)/.+\\.component\\.(json|yaml|yml)$"
        },
        "sourceRoot": {
          "type": "string",
          "pattern": "^src/.+"
        },
        "testRoot": {
          "type": "string",
          "pattern": "^tests/.+"
        },
        "buildPackId": {
          "$ref": "#/$defs/id"
        }
      }
    },
    "publicContract": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "kind", "path", "versioningPolicy"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "kind": {
          "enum": ["config-dto", "input-dto", "output-dto", "interface", "event", "schema"]
        },
        "path": {
          "type": "string",
          "pattern": "^src/.+"
        },
        "versioningPolicy": {
          "enum": ["semver-required", "breaking-change-blocked-without-major"]
        }
      }
    },
    "secretRequirement": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "provider", "scope", "required", "ref"],
      "properties": {
        "name": {
          "$ref": "#/$defs/id"
        },
        "provider": {
          "enum": ["azure-key-vault", "aws-secrets-manager", "gcp-secret-manager", "hashicorp-vault", "environment", "custom"]
        },
        "scope": {
          "enum": ["organization", "project", "environment", "personal"]
        },
        "required": {
          "type": "boolean"
        },
        "ref": {
          "type": "string",
          "pattern": "^secret://[A-Za-z0-9_./@-]+$"
        }
      }
    },
    "buildPackRef": {
      "type": "object",
      "additionalProperties": false,
      "required": ["buildPackId", "version", "path", "categoryPath", "componentIds", "exposedWorkflowIds"],
      "properties": {
        "buildPackId": {
          "$ref": "#/$defs/id"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "path": {
          "type": "string",
          "pattern": "^(builderx|components)/.+\\.buildpack\\.(yaml|yml|json)$"
        },
        "categoryPath": {
          "type": "array",
          "minItems": 2,
          "items": {
            "$ref": "#/$defs/id"
          }
        },
        "componentIds": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "exposedWorkflowIds": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        }
      }
    },
    "component": {
      "type": "object",
      "additionalProperties": false,
      "required": ["componentId", "version", "manifestPath", "buildPackId", "contractIds", "sourcePath", "testPath"],
      "properties": {
        "componentId": {
          "$ref": "#/$defs/id"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "manifestPath": {
          "type": "string",
          "pattern": "^(builderx|components)/.+\\.component\\.(json|yaml|yml)$"
        },
        "buildPackId": {
          "$ref": "#/$defs/id"
        },
        "contractIds": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "sourcePath": {
          "type": "string",
          "pattern": "^src/.+"
        },
        "testPath": {
          "type": "string",
          "pattern": "^tests/.+"
        }
      }
    },
    "registration": {
      "type": "object",
      "additionalProperties": false,
      "required": ["path", "dependencyInjection", "catalogMetadata", "factories", "capabilityDeclarations"],
      "properties": {
        "path": {
          "type": "string",
          "pattern": "^src/.+"
        },
        "dependencyInjection": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "catalogMetadata": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "factories": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        },
        "capabilityDeclarations": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/id"
          },
          "uniqueItems": true
        }
      }
    },
    "packageArtifact": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "kind", "language", "packageName", "version", "sourcePath", "artifactRef", "hash"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "kind": {
          "enum": ["builderx-buildpack", "nuget", "npm", "pypi", "maven", "gradle", "cargo", "go-module", "composer", "gem", "docker-oci", "custom"]
        },
        "language": {
          "enum": ["csharp", "typescript", "javascript", "python", "java", "kotlin", "go", "rust", "php", "ruby", "custom"]
        },
        "packageName": {
          "type": "string",
          "pattern": "^[a-zA-Z0-9@][a-zA-Z0-9_.:/@-]{1,160}$"
        },
        "version": {
          "$ref": "#/$defs/semver"
        },
        "sourcePath": {
          "type": "string",
          "pattern": "^(src|components|builderx|packages)/.+$"
        },
        "artifactRef": {
          "type": "string",
          "pattern": "^(dist|artifacts|packages|builderx)/.+$"
        },
        "hash": {
          "$ref": "#/$defs/hash"
        }
      }
    },
    "tests": {
      "type": "object",
      "additionalProperties": false,
      "required": ["suites"],
      "properties": {
        "suites": {
          "type": "array",
          "minItems": 3,
          "items": {
            "$ref": "#/$defs/testSuite"
          }
        }
      }
    },
    "testSuite": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "path", "required"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "type": {
          "enum": ["component", "contract", "workflow-smoke", "workflow-regression", "compatibility"]
        },
        "path": {
          "type": "string",
          "pattern": "^(tests|builderx/regression)/.+"
        },
        "required": {
          "type": "boolean"
        }
      }
    },
    "rbstdSync": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required", "tool", "targetScope", "importCommand", "syncCommand", "status", "evidenceRef"],
      "properties": {
        "required": {
          "const": true
        },
        "tool": {
          "const": "rbstd"
        },
        "targetScope": {
          "enum": ["global", "organization", "project", "personal"]
        },
        "importCommand": {
          "type": "string",
          "pattern": "^rbstd components import .+$"
        },
        "syncCommand": {
          "type": "string",
          "pattern": "^rbstd components sync .+$"
        },
        "status": {
          "enum": ["pending", "synced", "failed", "quarantined"]
        },
        "evidenceRef": {
          "$ref": "#/$defs/path"
        }
      }
    },
    "compatibility": {
      "type": "object",
      "additionalProperties": false,
      "required": ["minimumStandardVersion", "supportedBuilderXVersions", "supportedConsumers"],
      "properties": {
        "minimumStandardVersion": {
          "$ref": "#/$defs/semver"
        },
        "supportedBuilderXVersions": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/semver"
          },
          "uniqueItems": true
        },
        "supportedConsumers": {
          "type": "array",
          "minItems": 1,
          "items": {
            "enum": ["rootblocks-console", "builderx", "rbstd", "agent", "custom"]
          },
          "uniqueItems": true
        }
      }
    },
    "evidencePolicy": {
      "type": "object",
      "additionalProperties": false,
      "required": ["required", "publishEvidencePath", "evidenceRefs"],
      "properties": {
        "required": {
          "const": true
        },
        "publishEvidencePath": {
          "type": "string",
          "pattern": "^evidence/.+\\.(json|yaml|yml)$"
        },
        "evidenceRefs": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string",
            "pattern": "^evidence/.+\\.(json|yaml|yml)$"
          },
          "uniqueItems": true
        },
        "sourceAuditReportRef": {
          "type": "string",
          "pattern": "^evidence/.+\\.source-audit\\.(json|yaml|yml)$"
        }
      }
    },
    "build": {
      "type": "object",
      "additionalProperties": false,
      "required": ["commands", "outputs"],
      "properties": {
        "commands": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["id", "command", "workingDirectory"],
            "properties": {
              "id": {
                "$ref": "#/$defs/id"
              },
              "command": {
                "type": "string",
                "minLength": 1,
                "maxLength": 240
              },
              "workingDirectory": {
                "enum": [".", "src", "tests"]
              }
            }
          }
        },
        "outputs": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["id", "kind", "path", "hash", "rebuildableFromSource"],
            "properties": {
              "id": {
                "$ref": "#/$defs/id"
              },
              "kind": {
                "enum": ["buildpack", "native-package", "test-report", "coverage-report", "sbom"]
              },
              "path": {
                "type": "string",
                "pattern": "^dist/.+"
              },
              "hash": {
                "$ref": "#/$defs/hash"
              },
              "rebuildableFromSource": {
                "const": true
              }
            }
          }
        }
      }
    }
  }
}
