2021-09-27 00:38:15 +00:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
2022-04-19 08:40:48 +00:00
|
|
|
"io"
|
2021-09-27 00:38:15 +00:00
|
|
|
|
|
|
|
"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
|
|
|
|
|
2022-04-19 08:40:48 +00:00
|
|
|
// Lint lints an io.Reader against the Woodpecker schema.json
|
|
|
|
func Lint(r io.Reader) ([]gojsonschema.ResultError, error) {
|
2021-09-27 00:38:15 +00:00
|
|
|
schemaLoader := gojsonschema.NewBytesLoader(schemaDefinition)
|
2022-04-19 08:40:48 +00:00
|
|
|
j, err := yml.LoadYmlReaderAsJSON(r)
|
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
|
|
|
|
}
|