45 lines
998 B
Go
45 lines
998 B
Go
package schema
|
|
|
|
import (
|
|
_ "embed"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/xeipuuv/gojsonschema"
|
|
)
|
|
|
|
var (
|
|
//go:embed schema.json
|
|
schema []byte
|
|
schemaLoader = gojsonschema.NewBytesLoader(schema)
|
|
)
|
|
|
|
// Lint is for linting a style against the schema
|
|
func Lint(r io.Reader) (bool, error) {
|
|
sourceLoader, t := gojsonschema.NewReaderLoader(r)
|
|
if _, err := io.Copy(io.Discard, t); err != nil {
|
|
return false, err
|
|
}
|
|
result, err := gojsonschema.Validate(schemaLoader, sourceLoader)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if len(result.Errors()) > 0 {
|
|
return false, ResultErrors(result.Errors())
|
|
}
|
|
return result.Valid(), nil
|
|
}
|
|
|
|
// ResultErrors is a slice of gojsonschema.ResultError that implements error
|
|
type ResultErrors []gojsonschema.ResultError
|
|
|
|
// Error implements error
|
|
func (r ResultErrors) Error() string {
|
|
errs := make([]string, 0, len(r))
|
|
for _, re := range r {
|
|
errs = append(errs, fmt.Sprintf("%s: %s", re.Field(), re.Description()))
|
|
}
|
|
return strings.Join(errs, " | ")
|
|
}
|