forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule_name.go
36 lines (30 loc) · 972 Bytes
/
rule_name.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
package validate
import (
"fmt"
"regexp"
)
func RuleName(v interface{}, k string) (warnings []string, errors []error) {
value := v.(string)
if !regexp.MustCompile(`^[a-zA-Z_0-9.-]+$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"only word characters, numbers, underscores, periods, and hyphens allowed in %q: %q",
k, value))
}
if len(value) > 80 {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than 80 characters: %q", k, value))
}
if len(value) == 0 {
errors = append(errors, fmt.Errorf(
"%q cannot be an empty string: %q", k, value))
}
if !regexp.MustCompile(`[a-zA-Z0-9_]$`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must end with a word character, number, or underscore: %q", k, value))
}
if !regexp.MustCompile(`^[a-zA-Z0-9]`).MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must start with a word character or number: %q", k, value))
}
return warnings, errors
}