Skip to content

🧪 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 valueTypeDescription
First return value*ValidateResultParsed validation result (Valid is set to false on execution error)
Second return valueerrorAlways nil — validation failure information is encapsulated in the result

ValidateResult fields:

FieldTypeDescription
ValidboolWhether validation passed
Errors[]stringList of error messages
Warnings[]stringList 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) *ValidateResult can independently parse any composer validate text.
  • For strict-mode output, run ValidateStrict first.
  • To validate the schema only, use ValidateSchema.

Released under the MIT License