mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-26 20:01:02 +00:00
31bad81979
this move shared/yml/* into an independent lib
36 lines
826 B
Go
36 lines
826 B
Go
package schema
|
|
|
|
import (
|
|
"bytes"
|
|
_ "embed"
|
|
"fmt"
|
|
"io"
|
|
|
|
"codeberg.org/6543/go-yaml2json"
|
|
"github.com/xeipuuv/gojsonschema"
|
|
)
|
|
|
|
//go:embed schema.json
|
|
var schemaDefinition []byte
|
|
|
|
// Lint lints an io.Reader against the Woodpecker schema.json
|
|
func Lint(r io.Reader) ([]gojsonschema.ResultError, error) {
|
|
schemaLoader := gojsonschema.NewBytesLoader(schemaDefinition)
|
|
buff := new(bytes.Buffer)
|
|
err := yaml2json.StreamConverter(r, buff)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Failed to load yml file %w", err)
|
|
}
|
|
|
|
documentLoader := gojsonschema.NewBytesLoader(buff.Bytes())
|
|
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Validation failed %w", err)
|
|
}
|
|
|
|
if !result.Valid() {
|
|
return result.Errors(), fmt.Errorf("Config not valid")
|
|
}
|
|
|
|
return nil, nil
|
|
}
|