-
Notifications
You must be signed in to change notification settings - Fork 7
/
validation.go
90 lines (77 loc) · 1.76 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
package main
import (
"errors"
"regexp"
"strings"
"github.com/gin-gonic/gin"
)
const (
updateLen = 10
)
func validateUpdate(plainUpdate string) error {
splitUpdate := strings.Split(plainUpdate, delimiter)
if len(splitUpdate) < updateLen {
return errors.New("Malformed update")
}
splitUpdate = splitUpdate[:updateLen]
for _, item := range splitUpdate {
if !validateString(item) {
return errors.New("String validation failed for " + item)
}
}
if splitUpdate[0] != "team" || !validateTeamByID(splitUpdate[1]) {
return errors.New("Invalid team specified")
}
if splitUpdate[2] != "image" || !validateImage(splitUpdate[3]) {
return errors.New("Invalid image specified")
}
return nil
}
func validateReq(c *gin.Context) (string, string, error) {
teamID := c.Param("id")
if !validateTeam(teamID) {
err := errors.New("Invalid team id: " + teamID)
return "", "", err
}
imageName := c.Param("image")
if !validateImage(imageName) {
err := errors.New("Invalid image name: " + imageName)
return "", "", err
}
return teamID, imageName, nil
}
func validateString(input string) bool {
if input == "" {
return false
}
validationString := `^[a-zA-Z0-9-_ ]+$`
inputValidation := regexp.MustCompile(validationString)
return inputValidation.MatchString(input)
}
func validateTeam(teamName string) bool {
for _, team := range sarpConfig.Team {
if team.ID == teamName {
return true
}
if team.Alias == teamName {
return true
}
}
return false
}
func validateTeamByID(teamName string) bool {
for _, team := range sarpConfig.Team {
if team.ID == teamName {
return true
}
}
return false
}
func validateImage(imageName string) bool {
for _, image := range sarpConfig.Image {
if image.Name == imageName {
return true
}
}
return false
}