forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 1
/
validators.go
92 lines (82 loc) · 2.29 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
package arukas
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"strings"
)
func validateMaxLength(minLength, maxLength int) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) < minLength {
errors = append(errors, fmt.Errorf(
"%q cannot be shorter than %d characters: %q", k, minLength, value))
}
if len(value) > maxLength {
errors = append(errors, fmt.Errorf(
"%q cannot be longer than %d characters: %q", k, maxLength, value))
}
return
}
}
func validateIntegerInRange(min, max int) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value < min {
errors = append(errors, fmt.Errorf(
"%q cannot be lower than %d: %d", k, min, value))
}
if value > max {
errors = append(errors, fmt.Errorf(
"%q cannot be higher than %d: %d", k, max, value))
}
return
}
}
func validateStringInWord(allowWords []string) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
var found bool
for _, t := range allowWords {
if v.(string) == t {
found = true
}
}
if !found {
errors = append(errors, fmt.Errorf("%q must be one of [%s]", k, strings.Join(allowWords, "/")))
}
return
}
}
func validateIntInWord(allowWords []string) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
var found bool
for _, t := range allowWords {
if fmt.Sprintf("%d", v.(int)) == t {
found = true
}
}
if !found {
errors = append(errors, fmt.Errorf("%q must be one of [%s]", k, strings.Join(allowWords, "/")))
}
return
}
}
func validateDNSRecordValue() schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
var rtype, value string
values := v.(map[string]interface{})
rtype = values["type"].(string)
value = values["value"].(string)
switch rtype {
case "MX", "NS", "CNAME":
if rtype == "MX" {
if values["priority"] == nil {
errors = append(errors, fmt.Errorf("%q required when TYPE was MX", k))
}
}
if !strings.HasSuffix(value, ".") {
errors = append(errors, fmt.Errorf("%q must be period at the end [%s]", k, value))
}
}
return
}
}