mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-26 20:01:02 +00:00
0fa271f465
- 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>
32 lines
723 B
Go
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
|
|
}
|