2021-09-27 00:38:15 +00:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/xeipuuv/gojsonschema"
|
2021-10-12 07:25:13 +00:00
|
|
|
|
|
|
|
"github.com/woodpecker-ci/woodpecker/shared/yml"
|
2021-09-27 00:38:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
//go:embed schema.json
|
|
|
|
var schemaDefinition []byte
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
func Lint(file string) ([]gojsonschema.ResultError, error) {
|
2021-09-27 00:38:15 +00:00
|
|
|
schemaLoader := gojsonschema.NewBytesLoader(schemaDefinition)
|
2021-12-01 13:22:06 +00:00
|
|
|
j, err := yml.LoadYmlFileAsJSON(file)
|
2021-09-27 00:38:15 +00:00
|
|
|
if err != nil {
|
2021-12-01 13:22:06 +00:00
|
|
|
return nil, fmt.Errorf("Failed to load yml file %w", err)
|
2021-09-27 00:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
documentLoader := gojsonschema.NewBytesLoader(j)
|
|
|
|
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
|
|
|
|
if err != nil {
|
2021-12-01 13:22:06 +00:00
|
|
|
return nil, fmt.Errorf("Validation failed %w", err)
|
2021-09-27 00:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !result.Valid() {
|
2021-12-01 13:22:06 +00:00
|
|
|
return result.Errors(), fmt.Errorf("Config not valid")
|
2021-09-27 00:38:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|