-
Notifications
You must be signed in to change notification settings - Fork 53
/
validation.go
144 lines (132 loc) · 3.46 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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package core
import (
"fmt"
"github.com/rancher/opni/pkg/validation"
)
func (c *Cluster) Validate() error {
if c.Id == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "id")
}
if err := validation.ValidateID(c.Id); err != nil {
return err
}
if err := validation.ValidateLabels(c.GetMetadata().GetLabels()); err != nil {
return err
}
return nil
}
func (ls *LabelSelector) Validate() error {
if ls.MatchLabels != nil {
if err := validation.ValidateLabels(ls.MatchLabels); err != nil {
return err
}
}
for _, l := range ls.MatchExpressions {
if err := l.Validate(); err != nil {
return err
}
}
return nil
}
func (r *LabelSelectorRequirement) Validate() error {
if r.Key == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "key")
}
if r.Operator == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "operator")
}
if err := validation.ValidateLabelName(r.Key); err != nil {
return err
}
switch LabelSelectorOperator(r.Operator) {
case LabelSelectorOpIn, LabelSelectorOpNotIn, LabelSelectorOpExists, LabelSelectorOpDoesNotExist:
default:
return fmt.Errorf("%w: unknown operator %q (values are case-sensitive)", validation.ErrInvalidValue, r.Operator)
}
for _, value := range r.Values {
if err := validation.ValidateLabelValue(value); err != nil {
return err
}
}
return nil
}
func (r *Role) Validate() error {
if r.Id == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "id")
}
if err := validation.ValidateID(r.Id); err != nil {
return fmt.Errorf("%w: %q", err, r.Id)
}
for _, clusterID := range r.ClusterIDs {
if err := validation.ValidateID(clusterID); err != nil {
return fmt.Errorf("%w: %q", err, clusterID)
}
}
if r.MatchLabels != nil {
if err := r.MatchLabels.Validate(); err != nil {
return err
}
}
return nil
}
func (rb *RoleBinding) Validate() error {
if rb.Id == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "id")
}
if rb.RoleId == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "roleId")
}
if err := validation.ValidateID(rb.Id); err != nil {
return fmt.Errorf("%w: %q", err, rb.Id)
}
if err := validation.ValidateID(rb.RoleId); err != nil {
return fmt.Errorf("%w: %q", err, rb.RoleId)
}
for _, subject := range rb.Subjects {
if err := validation.ValidateSubject(subject); err != nil {
return err
}
}
return nil
}
func (ref *Reference) Validate() error {
if ref.Id == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "id")
}
if err := validation.ValidateID(ref.Id); err != nil {
return err
}
return nil
}
func (sar *SubjectAccessRequest) Validate() error {
if sar.Subject == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "subject")
}
if err := validation.ValidateSubject(sar.Subject); err != nil {
return err
}
return nil
}
func (o MatchOptions) Validate() error {
if _, ok := MatchOptions_name[int32(o)]; !ok {
return fmt.Errorf("%w: MatchOptions(%d)", validation.ErrInvalidValue, o)
}
return nil
}
func (tc *TokenCapability) Validate() error {
if tc.Type == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "type")
}
if ref := tc.GetReference(); ref != nil {
if err := ref.Validate(); err != nil {
return err
}
}
return nil
}
func (cc *ClusterCapability) Validate() error {
if cc.Name == "" {
return fmt.Errorf("%w: %s", validation.ErrMissingRequiredField, "name")
}
return nil
}