This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
94 lines (74 loc) · 2.47 KB
/
validate.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
package model
import (
"regexp"
"strings"
"github.com/nais/teams-backend/pkg/graph/apierror"
)
var (
// Slightly modified from database schema because Golang doesn't like Perl-flavored regexes.
teamSlugRegex = regexp.MustCompile("^[a-z](-?[a-z0-9]+)+$")
// Rules can be found here: https://api.slack.com/methods/conversations.create#naming
slackChannelNameRegex = regexp.MustCompile("^#[a-z0-9æøå_-]{2,80}$")
// Slugs that are reserved
reservedSlugs = []string{
"nais-system",
"kube-system",
"kube-node-lease",
"kube-public",
"kyverno",
"cnrm-system",
"configconnector-operator-system",
}
)
func ptr[T any](value T) *T {
return &value
}
func (input CreateTeamInput) Validate() error {
if input.Slug == nil || !teamSlugRegex.MatchString(string(*input.Slug)) || len(*input.Slug) < 3 || len(*input.Slug) > 30 {
return apierror.ErrTeamSlug
}
if input.Purpose == "" {
return apierror.ErrTeamPurpose
}
slug := input.Slug.String()
if strings.HasPrefix(slug, "team") {
return apierror.ErrTeamPrefixRedundant
}
for _, reserved := range reservedSlugs {
if slug == reserved {
return apierror.ErrTeamSlugReserved
}
}
if !slackChannelNameRegex.MatchString(input.SlackChannel) {
return slackChannelError(input.SlackChannel)
}
return nil
}
func (input UpdateTeamInput) Validate(validEnvironments []string) error {
if input.Purpose != nil && *input.Purpose == "" {
return apierror.ErrTeamPurpose
}
if input.SlackChannel != nil && !slackChannelNameRegex.MatchString(*input.SlackChannel) {
return slackChannelError(*input.SlackChannel)
}
validEnvironment := func(env string) bool {
for _, environment := range validEnvironments {
if env == environment {
return true
}
}
return false
}
for _, entry := range input.SlackAlertsChannels {
if !validEnvironment(entry.Environment) {
return apierror.Errorf("The specified environment is not valid: %q. Valid environments are: %s.", entry.Environment, strings.Join(validEnvironments, ", "))
}
if entry.ChannelName != nil && !slackChannelNameRegex.MatchString(*entry.ChannelName) {
return slackChannelError(*entry.ChannelName)
}
}
return nil
}
func slackChannelError(channel string) apierror.Error {
return apierror.Errorf("The Slack channel does not fit the requirements: %q. The name must contain at least 2 characters and at most 80 characters. The name must consist of lowercase letters, numbers, hyphens and underscores, and it must be prefixed with a hash symbol.", channel)
}