This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
validation.go
181 lines (167 loc) · 5.72 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package validation
import (
"strconv"
"strings"
"github.com/lyft/flyteadmin/pkg/common"
"github.com/lyft/flyteadmin/pkg/errors"
"github.com/lyft/flyteadmin/pkg/manager/impl/shared"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/lyft/flyteidl/gen/pb-go/flyteidl/core"
"github.com/lyft/flytepropeller/pkg/compiler/validators"
"google.golang.org/grpc/codes"
)
var entityToResourceType = map[common.Entity]core.ResourceType{
common.Task: core.ResourceType_TASK,
common.Workflow: core.ResourceType_WORKFLOW,
common.LaunchPlan: core.ResourceType_LAUNCH_PLAN,
}
func ValidateEmptyStringField(field, fieldName string) error {
if field == "" {
return shared.GetMissingArgumentError(fieldName)
}
return nil
}
// Validates that all required fields for an identifier are present.
func ValidateIdentifier(id *core.Identifier, expectedType common.Entity) error {
if id == nil {
return shared.GetMissingArgumentError(shared.ID)
}
if entityToResourceType[expectedType] != id.ResourceType {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"unexpected resource type %s for identifier [%+v], expected %s instead",
strings.ToLower(id.ResourceType.String()), id, strings.ToLower(entityToResourceType[expectedType].String()))
}
if err := ValidateEmptyStringField(id.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(id.Domain, shared.Domain); err != nil {
return err
}
if err := ValidateEmptyStringField(id.Name, shared.Name); err != nil {
return err
}
if err := ValidateEmptyStringField(id.Version, shared.Version); err != nil {
return err
}
return nil
}
func ValidateVersion(version string) error {
if err := ValidateEmptyStringField(version, shared.Version); err != nil {
return err
}
return nil
}
func ValidateResourceListRequest(request admin.ResourceListRequest) error {
if request.Id == nil {
return shared.GetMissingArgumentError(shared.ID)
}
if err := ValidateEmptyStringField(request.Id.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Id.Domain, shared.Domain); err != nil {
return err
}
if request.Limit <= 0 {
return shared.GetInvalidArgumentError(shared.Limit)
}
return nil
}
func ValidateActiveLaunchPlanRequest(request admin.ActiveLaunchPlanRequest) error {
if err := ValidateEmptyStringField(request.Id.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Id.Domain, shared.Domain); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Id.Name, shared.Name); err != nil {
return err
}
return nil
}
func ValidateActiveLaunchPlanListRequest(request admin.ActiveLaunchPlanListRequest) error {
if err := ValidateEmptyStringField(request.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Domain, shared.Domain); err != nil {
return err
}
if request.Limit <= 0 {
return shared.GetInvalidArgumentError(shared.Limit)
}
return nil
}
func ValidateNamedEntityIdentifierListRequest(request admin.NamedEntityIdentifierListRequest) error {
if err := ValidateEmptyStringField(request.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Domain, shared.Domain); err != nil {
return err
}
if request.Limit <= 0 {
return shared.GetInvalidArgumentError(shared.Limit)
}
return nil
}
func validateLiteralMap(inputMap *core.LiteralMap, fieldName string) error {
if inputMap != nil && len(inputMap.Literals) > 0 {
for name, fixedInput := range inputMap.Literals {
if name == "" {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, "missing key in %s", fieldName)
}
if fixedInput == nil || fixedInput.GetValue() == nil {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, "missing valid literal in %s %s", fieldName, name)
}
}
}
return nil
}
func validateParameterMap(inputMap *core.ParameterMap, fieldName string) error {
if inputMap != nil && len(inputMap.Parameters) > 0 {
for name, defaultInput := range inputMap.Parameters {
if name == "" {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, "missing key in %s", fieldName)
}
if defaultInput.GetVar() == nil || defaultInput.GetVar().GetType() == nil {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"The Variable component of the Parameter %s in %s either is missing, or has a missing Type",
name, fieldName)
}
if defaultInput.GetDefault() == nil && !defaultInput.GetRequired() {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"Invalid variable %s in %s - variable has neither default, nor is required. "+
"One must be specified", name, fieldName)
}
defaultValue := defaultInput.GetDefault()
if defaultValue != nil {
inputType := validators.LiteralTypeForLiteral(defaultValue)
if !validators.AreTypesCastable(inputType, defaultInput.GetVar().GetType()) {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"Type mismatch for Parameter %s in %s has type %s, expected %s", name, fieldName,
defaultInput.GetVar().GetType().String(), inputType.String())
}
}
}
}
return nil
}
// Offsets are encoded as string tokens to enable future api pagination changes. In addition to validating that an
// offset is a valid integer, we assert that it is non-negative.
func ValidateToken(token string) (int, error) {
if token == "" {
return 0, nil
}
offset, err := strconv.Atoi(token)
if err != nil {
return 0, err
}
if offset < 0 {
return 0, errors.NewFlyteAdminErrorf(codes.InvalidArgument, "Invalid token value: %s", token)
}
return offset, nil
}
func ValidateLimit(limit uint32) error {
if limit <= 0 {
return shared.GetMissingArgumentError(shared.Limit)
}
return nil
}