-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
62 lines (52 loc) · 1.39 KB
/
schema.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
package config
import (
"fmt"
"github.com/kube-cicd/pipelines-feedback-core/pkgs/apis/pipelinesfeedback.keskad.pl/v1alpha1"
"github.com/pkg/errors"
"strings"
)
type SchemaValidator struct {
schema map[string]Schema
}
func (sm *SchemaValidator) Add(schema Schema) {
if len(sm.schema) == 0 {
sm.schema = make(map[string]Schema)
}
sm.schema[schema.Name] = schema
}
func (sm *SchemaValidator) ValidateRequestedEntry(group string, key string) error {
if _, exists := sm.schema[group]; !exists {
return errors.New(fmt.Sprintf("component '%s' has no registered its schema", group))
}
grp := sm.schema[group]
for _, fieldName := range grp.AllowedFields {
if key == fieldName {
return nil
}
}
return errors.New(fmt.Sprintf("field '%s' is not a valid field for component '%s'", key, group))
}
func (sm *SchemaValidator) ValidateConfig(data v1alpha1.Data) error {
for fullKey, _ := range data {
group, key := parseEntryIntoGroupAndKey(fullKey)
if err := sm.ValidateRequestedEntry(group, key); err != nil {
return err
}
}
return nil
}
func parseEntryIntoGroupAndKey(entry string) (string, string) {
parts := strings.Split(entry, ".")
key := parts[len(parts)-1:][0]
// remove key from parts
parts = parts[:len(parts)-1]
group := strings.Join(parts, ".")
if group == "" {
group = "global"
}
return group, key
}
type Schema struct {
Name string
AllowedFields []string
}