Skip to content

🧪 ValidateComposerJson

Validates composer.json, with support for enabling strict mode and validating dependencies together. Equivalent to running composer validate [--strict] [--with-dependencies].

📋 Signature

go
func (c *Composer) ValidateComposerJson(strict bool, withDependencies bool) error

📥 Parameters

ParameterTypeDescription
strictboolWhen true, appends --strict, treating warnings as errors for stricter validation
withDependenciesboolWhen true, appends --with-dependencies, validating installed dependencies as well

📤 Return value

Return valueTypeDescription
Sole return valueerrorReturns an error when validation fails (including Composer's error message); nil indicates validation passed

📝 Example

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("init Composer failed: %v", err)
	}

	// Strict mode + validate together with dependencies
	if err := comp.ValidateComposerJson(true, true); err != nil {
		log.Fatalf("validation failed: %v", err)
	}
	fmt.Println("composer.json and dependencies validation passed")
}

🚀 Advanced

  • ✅ For the most basic quick validation: use Validate (composer validate, no extra arguments).
  • 📦 When you need structured validation results for programmatic parsing: use ValidateStructured.
  • 🔗 Use alongside CheckPlatformReqs: validate the config file first, then check whether the runtime meets platform requirements.
  • 🧪 Calling with strict=true, withDependencies=true before release or deployment surfaces configuration and dependency-chain issues at the earliest stage.
  • ⚠️ When withDependencies=true, Composer checks whether dependency version constraints match what is actually installed, which is relatively time-consuming. It is better suited for CI than for frequent local invocations.

Released under the MIT License