woodpecker/pipeline/schema/schema.go
Anbraten 0fa271f465
Add json schema and cli lint command (#342)
- Add json schema file
- Add tests to validate sample pipeline files
- Add new command `lint` to cli to test a directory or single file to use correct schema

Example:  `woodpecker-cli lint ./pipeline/schema/.woodpecker/`

---
close #275 
preparation for #276 


Co-authored-by: 6543 <6543@obermui.de>
2021-09-27 02:38:15 +02:00

32 lines
723 B
Go

package schema
import (
_ "embed"
"fmt"
"github.com/woodpecker-ci/woodpecker/shared/yml"
"github.com/xeipuuv/gojsonschema"
)
//go:embed schema.json
var schemaDefinition []byte
func Lint(file string) (error, []gojsonschema.ResultError) {
schemaLoader := gojsonschema.NewBytesLoader(schemaDefinition)
j, err := yml.LoadYmlFileAsJson(file)
if err != nil {
return fmt.Errorf("Failed to load yml file %w", err), nil
}
documentLoader := gojsonschema.NewBytesLoader(j)
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
return fmt.Errorf("Validation failed %w", err), nil
}
if !result.Valid() {
return fmt.Errorf("Config not valid"), result.Errors()
}
return nil, nil
}