1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
package file
import (
yaml "github.com/ghodss/yaml"
"github.com/kong/deck/utils"
"github.com/pkg/errors"
"github.com/xeipuuv/gojsonschema"
)
func validate(content []byte) error {
var c map[string]interface{}
err := yaml.Unmarshal(content, &c)
if err != nil {
return errors.Wrap(err, "unmarshaling file content")
}
c = ensureJSON(c)
schemaLoader := gojsonschema.NewStringLoader(contentSchema)
documentLoader := gojsonschema.NewGoLoader(c)
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
return err
}
if result.Valid() {
return nil
}
var errs utils.ErrArray
for _, desc := range result.Errors() {
err := errors.New(desc.String())
errs.Errors = append(errs.Errors, err)
}
return errs
}
|