-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
75 lines (61 loc) · 1.85 KB
/
main.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
package main
import (
"fmt"
"io/fs"
"os"
"strings"
"github.com/xeipuuv/gojsonschema"
)
var validationErrors []string
func main() {
dirs, _ := os.ReadDir("./")
for _, dir := range dirs {
validate(dir)
}
outputResults()
}
func validate(dir fs.DirEntry) {
dirName := dir.Name()
if dir.IsDir() && stringContains(gatewayDirNames(), dirName) {
fmt.Println("Running schema validation against sample " + dirName + " identities")
schemaLoader := gojsonschema.NewReferenceLoader("file://" + dirName + "/schema.json")
files, _ := os.ReadDir(dirName + "/identities")
for _, file := range files {
fileName := file.Name()
if !file.IsDir() {
documentLoader := gojsonschema.NewReferenceLoader("file://" + dirName + "/identities/" + fileName)
result, err := gojsonschema.Validate(schemaLoader, documentLoader)
if err != nil {
panic(err.Error())
}
if !result.Valid() {
for _, desc := range result.Errors() {
e := "- " + dirName + "/" + fileName + ": " + desc.Description()
validationErrors = append(validationErrors, e)
}
}
}
}
}
}
func stringContains(slice []string, ele string) bool {
for _, sliceEle := range slice {
if sliceEle == ele {
return true
}
}
return false
}
func gatewayDirNames() []string {
return []string{"3scale", "turnpike"}
}
func outputResults() {
fmt.Println()
if len(validationErrors) > 0 {
fmt.Printf("Error(s) found in validation:\n")
fmt.Println(strings.Join(validationErrors, "\n"))
os.Exit(1)
} else {
fmt.Printf("No errors found.\n")
}
}