-
Notifications
You must be signed in to change notification settings - Fork 787
/
validation.go
46 lines (40 loc) · 1.18 KB
/
validation.go
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package util
import (
schemagen "github.com/alecthomas/jsonschema"
"github.com/xeipuuv/gojsonschema"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/yaml"
)
// GenerateSchema generates a JSON schema for the given struct type and returns it.
func GenerateSchema(target interface{}) *schemagen.Schema {
reflector := schemagen.Reflector{
IgnoredTypes: []interface{}{
corev1.Container{},
},
RequiredFromJSONSchemaTags: true,
}
return reflector.Reflect(target)
}
// ValidateYaml generates a JSON schema for the given struct type, and then validates the given YAML against that
// schema, ignoring Containers and missing fields.
func ValidateYaml(target interface{}, data []byte) ([]string, error) {
schema := GenerateSchema(target)
dataAsJSON, err := yaml.YAMLToJSON(data)
if err != nil {
return nil, err
}
schemaLoader := gojsonschema.NewGoLoader(schema)
documentLoader := gojsonschema.NewBytesLoader(dataAsJSON)
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
return nil, err
}
if !result.Valid() {
errMsgs := []string{}
for _, e := range result.Errors() {
errMsgs = append(errMsgs, e.String())
}
return errMsgs, nil
}
return nil, nil
}