forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.go
91 lines (77 loc) · 2.13 KB
/
validator.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package validation
import (
"fmt"
"io/ioutil"
"os"
"strings"
"path/filepath"
"github.com/xeipuuv/gojsonschema"
"sigs.k8s.io/yaml"
)
// https://stackoverflow.com/questions/10485743/contains-method-for-a-slice
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
func ValidateArgoYamlRecursively(fromPath string, skipFileNames []string) (map[string][]string, error) {
schemaBytes, err := ioutil.ReadFile("../api/jsonschema/schema.json")
if err != nil {
return nil, err
}
schemaLoader := gojsonschema.NewStringLoader(string(schemaBytes))
failed := map[string][]string{}
err = filepath.Walk(fromPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if contains(skipFileNames, info.Name()) {
// fmt.Printf("skipping %+v \n", info.Name())
return filepath.SkipDir
}
if info.IsDir() {
return nil
}
if filepath.Ext(path) != ".yaml" {
return nil
}
yamlBytes, err := ioutil.ReadFile(filepath.Clean(path))
if err != nil {
return err
}
jsonDoc, err := yaml.YAMLToJSON(yamlBytes)
if err != nil {
return err
}
documentLoader := gojsonschema.NewStringLoader(string(jsonDoc))
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
return err
}
incorrectError := false
if !result.Valid() {
errorDescriptions := []string{}
for _, err := range result.Errors() {
// port should be port number or port reference string, using string port number will cause issue
// due swagger 2.0 limitation, we can only specify one data type (we use string, same as k8s api swagger)
if strings.HasSuffix(err.Field(), "httpGet.port") && err.Description() == "Invalid type. Expected: string, given: integer" {
incorrectError = true
continue
} else {
errorDescriptions = append(errorDescriptions, fmt.Sprintf("%s in %s", err.Description(), err.Context().String()))
}
}
if !(incorrectError && len(errorDescriptions) == 1) {
failed[path] = errorDescriptions
}
}
return nil
})
if err != nil {
return nil, err
}
return failed, nil
}