🧪 ValidateStructured
Runs composer validate and parses the output into a structured *ValidateResult, separating error and warning lists.
When to use
Use this when you need to handle errors and warnings separately in a program (e.g. reporting to a monitoring system, filtering by severity). Compared with the raw string returned by ValidateStrict, the structured result is easier to act on automatically.
Signature
go
func (c *Composer) ValidateStructured() (*ValidateResult, error)Parameters
This method takes no parameters.
Return value
| Return value | Type | Description |
|---|---|---|
| First return value | *ValidateResult | Parsed validation result (Valid is set to false on execution error) |
| Second return value | error | Always nil — validation failure information is encapsulated in the result |
ValidateResult fields:
| Field | Type | Description |
|---|---|---|
Valid | bool | Whether validation passed |
Errors | []string | List of error messages |
Warnings | []string | List of warning messages |
Note
This method does not return an error on validation failure. You must check result.Valid to determine whether it 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 failed: %v", err)
}
result, _ := comp.ValidateStructured()
if !result.Valid {
for _, e := range result.Errors {
fmt.Println("error:", e)
}
for _, w := range result.Warnings {
fmt.Println("warning:", w)
}
return
}
fmt.Println("composer.json validation passed")
}Advanced
- The underlying parser function
ParseValidateOutput(output string) *ValidateResultcan independently parse anycomposer validatetext. - For strict-mode output, run ValidateStrict first.
- To validate the schema only, use ValidateSchema.