-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.go
148 lines (115 loc) · 2.98 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
145
146
147
148
package sdp
import (
"errors"
"fmt"
)
// Validate Ensures that en item is valid (e.g. contains the required fields)
func (i *Item) Validate() error {
if i == nil {
return errors.New("Item is nil")
}
if i.GetType() == "" {
return fmt.Errorf("item has empty Type: %v", i.GloballyUniqueName())
}
if i.GetUniqueAttribute() == "" {
return fmt.Errorf("item has empty UniqueAttribute: %v", i.GloballyUniqueName())
}
if i.GetAttributes() == nil {
return fmt.Errorf("item has nil Attributes: %v", i.GloballyUniqueName())
}
if i.GetScope() == "" {
return fmt.Errorf("item has empty Scope: %v", i.GloballyUniqueName())
}
if i.UniqueAttributeValue() == "" {
return fmt.Errorf("item has empty UniqueAttributeValue: %v", i.GloballyUniqueName())
}
return nil
}
// Validate Ensures a reference is valid
func (r *Reference) Validate() error {
if r == nil {
return errors.New("reference is nil")
}
if r.GetType() == "" {
return fmt.Errorf("reference has empty Type: %v", r)
}
if r.GetUniqueAttributeValue() == "" {
return fmt.Errorf("reference has empty UniqueAttributeValue: %v", r)
}
if r.GetScope() == "" {
return fmt.Errorf("reference has empty Scope: %v", r)
}
return nil
}
// Validate Ensures an edge is valid
func (e *Edge) Validate() error {
if e == nil {
return errors.New("edge is nil")
}
var err error
err = e.GetFrom().Validate()
if err != nil {
return err
}
err = e.GetTo().Validate()
return err
}
// Validate Ensures a Response is valid
func (r *Response) Validate() error {
if r == nil {
return errors.New("response is nil")
}
if r.GetResponder() == "" {
return fmt.Errorf("response has empty Responder: %v", r)
}
if len(r.GetUUID()) == 0 {
return fmt.Errorf("response has empty UUID: %v", r)
}
return nil
}
// Validate Ensures a QueryError is valid
func (e *QueryError) Validate() error {
if e == nil {
return errors.New("queryError is nil")
}
if len(e.GetUUID()) == 0 {
return fmt.Errorf("queryError has empty UUID: %w", e)
}
if e.GetErrorString() == "" {
return fmt.Errorf("queryError has empty ErrorString: %w", e)
}
if e.GetScope() == "" {
return fmt.Errorf("queryError has empty Scope: %w", e)
}
if e.GetSourceName() == "" {
return fmt.Errorf("queryError has empty SourceName: %w", e)
}
if e.GetItemType() == "" {
return fmt.Errorf("queryError has empty ItemType: %w", e)
}
if e.GetResponderName() == "" {
return fmt.Errorf("queryError has empty ResponderName: %w", e)
}
return nil
}
// Validate Ensures a Query is valid
func (q *Query) Validate() error {
if q == nil {
return errors.New("query is nil")
}
if q.GetType() == "" {
return fmt.Errorf("query has empty Type: %v", q)
}
if q.GetScope() == "" {
return fmt.Errorf("query has empty Scope: %v", q)
}
if len(q.GetUUID()) != 16 {
return fmt.Errorf("query has invalid UUID: %v", q)
}
if q.GetMethod() == QueryMethod_GET {
if q.GetQuery() == "" {
return fmt.Errorf("query cannot have empty Query when method is Get: %v", q)
}
}
return nil
}