/
validations.go
51 lines (45 loc) · 1.45 KB
/
validations.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
package util
import (
"fmt"
"regexp"
)
// The following code came from "k8s.io/apimachinery/pkg/util/validation/validation.go"
// If be required the usage of more funcs from this then please replace it for the import
// ---------------------------------------
const (
qnameCharFmt string = "[a-z0-9]([-a-z0-9]*[a-z0-9])?"
// The value is 56 because it will be contact with "-system" = 63
qualifiedNameMaxLength int = 56
)
var qualifiedNameRegexp = regexp.MustCompile("^" + qnameCharFmt + "$")
//IsValidName used to check the name of the project
func IsValidName(value string) []string {
var errs []string
if len(value) > qualifiedNameMaxLength {
errs = append(errs, MaxLenError(qualifiedNameMaxLength))
}
if !qualifiedNameRegexp.MatchString(value) {
errs = append(errs, RegexError("invalid value for project name", qnameCharFmt))
}
return errs
}
// RegexError returns a string explanation of a regex validation failure.
func RegexError(msg string, fmt string, examples ...string) string {
if len(examples) == 0 {
return msg + " (regex used for validation is '" + fmt + "')"
}
msg += " (e.g. "
for i := range examples {
if i > 0 {
msg += " or "
}
msg += "'" + examples[i] + "', "
}
msg += "regex used for validation is '" + fmt + "')"
return msg
}
// MaxLenError returns a string explanation of a "string too long" validation
// failure.
func MaxLenError(length int) string {
return fmt.Sprintf("must be no more than %d characters", length)
}