-
Notifications
You must be signed in to change notification settings - Fork 29
/
validators.go
93 lines (82 loc) · 2.43 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
package qingcloud
import (
"fmt"
"net"
"regexp"
"github.com/hashicorp/terraform/helper/schema"
)
var ColorRegex = regexp.MustCompile("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$")
var PortRegex = regexp.MustCompile("^0*(?:6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{1,3}|[0-9])$")
func validateNetworkCIDR(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if _, _, err := net.ParseCIDR(value); err != nil {
errors = append(errors, fmt.Errorf(
"%q must a valid CIDR, got error parsing: %s", value, err))
return
}
return
}
func withinArrayString(limits ...string) schema.SchemaValidateFunc {
var limitsMap = make(map[string]bool)
for _, v := range limits {
limitsMap[v] = true
}
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if limitsMap[value] {
return
}
errors = append(errors, fmt.Errorf("%q (%q) doesn't match", k, value))
return
}
}
func withinArrayInt(limits ...int) schema.SchemaValidateFunc {
var limitsMap = make(map[int]bool)
for _, v := range limits {
limitsMap[v] = true
}
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if limitsMap[value] {
return
}
errors = append(errors, fmt.Errorf("%q (%q) doesn't match", k, value))
return
}
}
func withinArrayIntRange(begin, end int) schema.SchemaValidateFunc {
return func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value >= begin && value <= end {
return
}
errors = append(errors, fmt.Errorf("%q (%q) should > %d && < %d ", k, value, begin, end))
return
}
}
func validateColorString(v interface{}, k string) (ws []string, errors []error) {
colorstring := v.(string)
if !ColorRegex.MatchString(colorstring) {
errors = append(errors, fmt.Errorf("%q (%q) doesn't match", k, colorstring))
return
}
return
}
func validatePortString(v interface{}, k string) (ws []string, errors []error) {
portstring := v.(string)
if !PortRegex.MatchString(portstring) {
errors = append(errors, fmt.Errorf("%q (%q) doesn't match", k, portstring))
return
}
return
}
func validateVolumeSize(v interface{}, k string) (ws []string, errors []error) {
size := v.(int)
if size < 10 || size > 5000 {
errors = append(errors, fmt.Errorf("%q (%q) should > 10 && < 5000 ", k, size))
}
if size%10 != 0 {
errors = append(errors, fmt.Errorf("%q (%q) should be multiples of 10", k, size))
}
return
}