-
Notifications
You must be signed in to change notification settings - Fork 3
/
validate.go
79 lines (70 loc) · 2.23 KB
/
validate.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
package file
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"github.com/kong/go-database-reconciler/pkg/utils"
"github.com/xeipuuv/gojsonschema"
"sigs.k8s.io/yaml"
)
type ValidationError struct {
Object string `json:"object"`
Err error `json:"error"`
}
func (e *ValidationError) Error() string {
return fmt.Sprintf("validation error: object=%s, err=%v", e.Object, e.Err)
}
func validate(content []byte) error {
var c map[string]interface{}
err := yaml.Unmarshal(content, &c)
if err != nil {
return fmt.Errorf("unmarshaling file content: %w", err)
}
c = ensureJSON(c)
schemaLoader := gojsonschema.NewStringLoader(kongJSONSchema)
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() {
jsonString, err := json.Marshal(desc.Value())
if err != nil {
return err
}
errs.Errors = append(errs.Errors, &ValidationError{Object: string(jsonString), Err: errors.New(desc.String())})
}
return errs
}
func validateWorkspaces(workspaces []string) error {
utils.RemoveDuplicates(&workspaces)
if len(workspaces) > 1 {
return fmt.Errorf("it seems like you are trying to sync multiple workspaces "+
"at the same time (%v).\ndecK doesn't support syncing multiple workspaces at the same time, "+
"please sync one workspace at a time", workspaces)
}
return nil
}
func validateRuntimeGroups(names []string) error {
utils.RemoveDuplicates(&names)
if len(names) > 1 {
return fmt.Errorf("it seems like you are trying to sync multiple Konnect Runtime Groups "+
"at the same time (%v).\ndecK doesn't support syncing multiple Runtime Groups at the same time, "+
"please sync one Runtime Group at a time", names)
}
return nil
}
func validateEmptyContent(content Content) error {
// if content is empty, return an error
if reflect.DeepEqual(content, Content{}) {
return fmt.Errorf("it seems like you are trying to sync an empty configuration file " +
"or directory. That would lead to the removal of the default workspace. Please make " +
"sure that the file or directory you are trying to sync references a workspace")
}
return nil
}