mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-11 14:09:21 +00:00
60 lines
1.5 KiB
Go
60 lines
1.5 KiB
Go
package validator
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/xeipuuv/gojsonschema"
|
|
"sigs.k8s.io/yaml"
|
|
)
|
|
|
|
type InvalidResourceError struct{ err string }
|
|
|
|
func (r InvalidResourceError) Error() string {
|
|
return r.err
|
|
}
|
|
|
|
// ValidFormat is a type for quickly forcing
|
|
// new formats on the gojsonschema loader
|
|
type ValidFormat struct{}
|
|
|
|
func (f ValidFormat) IsFormat(input interface{}) bool {
|
|
return true
|
|
}
|
|
|
|
// From kubeval - let's see if absolutely necessary
|
|
// func init () {
|
|
// gojsonschema.FormatCheckers.Add("int64", ValidFormat{})
|
|
// gojsonschema.FormatCheckers.Add("byte", ValidFormat{})
|
|
// gojsonschema.FormatCheckers.Add("int32", ValidFormat{})
|
|
// gojsonschema.FormatCheckers.Add("int-or-string", ValidFormat{})
|
|
// }
|
|
|
|
func Validate(rawResource []byte, schema *gojsonschema.Schema) error {
|
|
if schema == nil {
|
|
return nil
|
|
}
|
|
|
|
var resource map[string]interface{}
|
|
if err := yaml.Unmarshal(rawResource, &resource); err != nil {
|
|
return fmt.Errorf("error unmarshalling resource: %s", err)
|
|
}
|
|
resourceLoader := gojsonschema.NewGoLoader(resource)
|
|
|
|
results, err := schema.Validate(resourceLoader)
|
|
if err != nil {
|
|
// This error can only happen if the Object to validate is poorly formed. There's no hope of saving this one
|
|
return fmt.Errorf("problem validating schema. Check JSON formatting: %s", err)
|
|
}
|
|
|
|
if !results.Valid() {
|
|
msg := ""
|
|
for _, errMsg := range results.Errors() {
|
|
if msg != "" {
|
|
msg += " - "
|
|
}
|
|
msg += errMsg.Description()
|
|
}
|
|
return InvalidResourceError{err: msg}
|
|
}
|
|
|
|
return nil
|
|
}
|