2021-09-27 00:38:15 +00:00
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
2022-07-17 15:23:31 +00:00
|
|
|
"bytes"
|
2021-09-27 00:38:15 +00:00
|
|
|
_ "embed"
|
|
|
|
"fmt"
|
2022-04-19 08:40:48 +00:00
|
|
|
"io"
|
2021-09-27 00:38:15 +00:00
|
|
|
|
2022-07-17 15:23:31 +00:00
|
|
|
"codeberg.org/6543/go-yaml2json"
|
2021-09-27 00:38:15 +00:00
|
|
|
"github.com/xeipuuv/gojsonschema"
|
|
|
|
)
|
|
|
|
|
|
|
|
//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-07-17 15:23:31 +00:00
|
|
|
buff := new(bytes.Buffer)
|
|
|
|
err := yaml2json.StreamConverter(r, buff)
|
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
|
|
|
}
|
|
|
|
|
2022-07-17 15:23:31 +00:00
|
|
|
documentLoader := gojsonschema.NewBytesLoader(buff.Bytes())
|
2021-09-27 00:38:15 +00:00
|
|
|
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
|
|
|
|
}
|