forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators.go
103 lines (83 loc) · 2.66 KB
/
validators.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
95
96
97
98
99
100
101
102
103
package azurerm
import (
"fmt"
"regexp"
"time"
"github.com/Azure/go-autorest/autorest/date"
"github.com/hashicorp/terraform/helper/schema"
"github.com/satori/uuid"
)
func validateRFC3339Date(v interface{}, k string) (ws []string, errors []error) {
dateString := v.(string)
if _, err := date.ParseTime(time.RFC3339, dateString); err != nil {
errors = append(errors, fmt.Errorf("%q is an invalid RFC3339 date: %+v", k, err))
}
return
}
// validateIntInSlice returns a SchemaValidateFunc which tests if the provided value
// is of type int and matches the value of an element in the valid slice
func validateIntInSlice(valid []int) schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(int)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be int", k))
return
}
for _, str := range valid {
if v == str {
return
}
}
es = append(es, fmt.Errorf("expected %q to be one of %v, got %v", k, valid, v))
return
}
}
func validateUUID(v interface{}, k string) (ws []string, errors []error) {
if _, err := uuid.FromString(v.(string)); err != nil {
errors = append(errors, fmt.Errorf("%q is an invalid UUUID: %s", k, err))
}
return
}
func validateDBAccountName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
r, _ := regexp.Compile("^[a-z0-9\\-]+$")
if !r.MatchString(value) {
errors = append(errors, fmt.Errorf("Account Name can only contain lower-case characters, numbers and the `-` character."))
}
length := len(value)
if length > 50 || 3 > length {
errors = append(errors, fmt.Errorf("Account Name can only be between 3 and 50 seconds."))
}
return
}
func evaluateSchemaValidateFunc(i interface{}, k string, validateFunc schema.SchemaValidateFunc) (bool, error) {
_, es := validateFunc(i, k)
if len(es) > 0 {
return false, es[0]
}
return true, nil
}
func validateStringLength(maxLength int) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > maxLength {
errors = append(errors, fmt.Errorf(
"The %q can be no longer than %d chars", k, maxLength))
}
return
}
}
func validateIso8601Duration() schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(string)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be string", k))
return
}
matched, _ := regexp.MatchString(`^P([0-9]+Y)?([0-9]+M)?([0-9]+W)?([0-9]+D)?(T([0-9]+H)?([0-9]+M)?([0-9]+(\.?[0-9]+)?S)?)?$`, v)
if !matched {
es = append(es, fmt.Errorf("expected %s to be in ISO 8601 duration format, got %s", k, v))
}
return
}
}