-
Notifications
You must be signed in to change notification settings - Fork 72
/
validation.go
93 lines (71 loc) · 2.54 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
package validation
import (
"regexp"
"github.com/redhat-developer/app-services-cli/pkg/core/errors"
"github.com/redhat-developer/app-services-cli/pkg/core/localize"
)
const (
// name validation rules
legalShortDescriptionChars = "^[a-z]([-a-z0-9]*[a-z0-9])?$"
maxNameLength = 50
minNameLength = 1
legalUUID = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
)
// Validator is a type for validating service account configuration values
type Validator struct {
Localizer localize.Localizer
}
// ValidateShortDescription validates the short description value
func (v *Validator) ValidateShortDescription(val interface{}) error {
name, ok := val.(string)
if !ok {
return errors.NewCastError(val, "string")
}
if len(name) < minNameLength {
return v.Localizer.MustLocalizeError("serviceAccount.common.validation.shortDescription.error.required")
} else if len(name) > maxNameLength {
return v.Localizer.MustLocalizeError("serviceAccount.common.validation.shortDescription.error.lengthError", localize.NewEntry("MaxNameLen", maxNameLength))
}
matched, _ := regexp.Match(legalShortDescriptionChars, []byte(name))
if matched {
return nil
}
return v.Localizer.MustLocalizeError("serviceAccount.common.validation.shortDescription.error.invalidChars", localize.NewEntry("Name", name))
}
// ValidateUUID validates if ID is a valid UUID
func (v *Validator) ValidateUUID(val interface{}) error {
id, ok := val.(string)
if !ok {
return errors.NewCastError(val, "string")
}
matched, _ := regexp.Match(legalUUID, []byte(id))
if matched {
return nil
}
return v.Localizer.MustLocalizeError("serviceAccount.common.validation.id.error.invalidID", localize.NewEntry("ID", id))
}
// ValidatePage validates if page number is valid
func (v *Validator) ValidatePage(val interface{}) error {
page, ok := val.(int32)
if !ok {
return errors.NewCastError(val, "int32")
}
if page < 1 {
return v.Localizer.MustLocalizeError("serviceAccount.list.validation.page.error", localize.NewEntry("Page", page))
}
return nil
}
// ValidateSize validates if a valid size is provided
func (v *Validator) ValidateSize(val interface{}) error {
size, ok := val.(int32)
if !ok {
return errors.NewCastError(val, "int32")
}
if size > 100 {
return v.Localizer.MustLocalizeError("serviceAccount.list.validation.size.error.max", localize.NewEntry("Size", size))
}
if size < 1 {
return v.Localizer.MustLocalizeError("serviceAccount.list.validation.size.error.min", localize.NewEntry("Size", size))
}
return nil
}