🧪 ValidateComposerJson
校验 composer.json,支持启用严格模式与连同依赖一起校验,相当于执行 composer validate [--strict] [--with-dependencies]。
📋 签名
go
func (c *Composer) ValidateComposerJson(strict bool, withDependencies bool) error📥 参数
| 参数 | 类型 | 说明 |
|---|---|---|
strict | bool | 为 true 时追加 --strict,将警告也视为错误,校验更严格 |
withDependencies | bool | 为 true 时追加 --with-dependencies,连同已安装的依赖一并进行校验 |
📤 返回值
| 返回值 | 类型 | 说明 |
|---|---|---|
| 唯一返回值 | error | 校验失败时返回错误(包含 Composer 的报错信息),nil 表示校验通过 |
📝 示例
go
package main
import (
"fmt"
"log"
"github.com/scagogogo/composer-skills/pkg/composer"
)
func main() {
comp, err := composer.New(composer.DefaultOptions())
if err != nil {
log.Fatalf("初始化 Composer 失败: %v", err)
}
// 严格模式 + 连同依赖一起校验
if err := comp.ValidateComposerJson(true, true); err != nil {
log.Fatalf("校验失败: %v", err)
}
fmt.Println("composer.json 及依赖校验通过")
}🚀 进阶
- ✅ 只做最基础的快速校验:使用 Validate(
composer validate,无额外参数)。 - 📦 需要以结构化形式获取校验结果以便程序解析:使用 ValidateStructured。
- 🔗 与 CheckPlatformReqs 配合使用:先校验配置文件,再校验运行环境是否满足平台要求。
- 🧪 在发布或部署前以
strict=true, withDependencies=true调用,可在最早阶段发现配置与依赖链问题。 - ⚠️ 当
withDependencies=true时,Composer 会检查依赖项的版本约束与实际安装情况,耗时相对较长,适合 CI 而非高频本地调用。