Skip to content

✅ Validate

Validates whether composer.json is valid, equivalent to running composer validate. This is a pure validation method: it only returns error; when issues are found, it returns them directly as an error, and returns nil when validation passes.

📋 Signature

go
func (c *Composer) Validate() error

📥 Parameters

ParameterTypeDescription
NoneThis method takes no parameters

📤 Return value

Return valueTypeDescription
Sole return valueerrorReturns an error when validation fails (including Composer's error message); nil indicates composer.json is valid

Difference from ValidateStructured / ValidateStrict

This method's signature is error (not (string, error)), meaning it is a pure validation entry point: when any issue is found, it returns an error directly and aborts, without producing result text that can be parsed programmatically. If you need the full validation output text for logging or for display in CI, use ValidateStructured or ValidateStrict instead; this method is better suited for the simple guard scenario of "pass and continue, fail and error out".

📝 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)
	}

	if err := comp.Validate(); err != nil {
		log.Fatalf("composer.json validation failed: %v", err)
	}
	fmt.Println("composer.json validation passed")
}

🚀 Advanced

  • 📋 Need stricter validation (treating warnings as errors): use ValidateComposerJson(true, false) (composer validate --strict).
  • 🔗 Need to validate together with dependencies: use ValidateComposerJson(false, true) (composer validate --with-dependencies).
  • 📦 Need structured results for programmatic processing: use ValidateStructured.
  • 🧪 Calling this in a CI/CD pipeline or pre-commit hook can catch a malformed composer.json early, before dependencies are installed.

Released under the MIT License