]>
Commit | Line | Data |
---|---|---|
bae9f6d2 JC |
1 | package validation |
2 | ||
3 | import ( | |
4 | "fmt" | |
5 | "net" | |
6 | "strings" | |
7 | ||
8 | "github.com/hashicorp/terraform/helper/schema" | |
9 | "github.com/hashicorp/terraform/helper/structure" | |
10 | ) | |
11 | ||
12 | // IntBetween returns a SchemaValidateFunc which tests if the provided value | |
13 | // is of type int and is between min and max (inclusive) | |
14 | func IntBetween(min, max int) schema.SchemaValidateFunc { | |
15 | return func(i interface{}, k string) (s []string, es []error) { | |
16 | v, ok := i.(int) | |
17 | if !ok { | |
18 | es = append(es, fmt.Errorf("expected type of %s to be int", k)) | |
19 | return | |
20 | } | |
21 | ||
22 | if v < min || v > max { | |
23 | es = append(es, fmt.Errorf("expected %s to be in the range (%d - %d), got %d", k, min, max, v)) | |
24 | return | |
25 | } | |
26 | ||
27 | return | |
28 | } | |
29 | } | |
30 | ||
31 | // StringInSlice returns a SchemaValidateFunc which tests if the provided value | |
32 | // is of type string and matches the value of an element in the valid slice | |
33 | // will test with in lower case if ignoreCase is true | |
34 | func StringInSlice(valid []string, ignoreCase bool) schema.SchemaValidateFunc { | |
35 | return func(i interface{}, k string) (s []string, es []error) { | |
36 | v, ok := i.(string) | |
37 | if !ok { | |
38 | es = append(es, fmt.Errorf("expected type of %s to be string", k)) | |
39 | return | |
40 | } | |
41 | ||
42 | for _, str := range valid { | |
43 | if v == str || (ignoreCase && strings.ToLower(v) == strings.ToLower(str)) { | |
44 | return | |
45 | } | |
46 | } | |
47 | ||
48 | es = append(es, fmt.Errorf("expected %s to be one of %v, got %s", k, valid, v)) | |
49 | return | |
50 | } | |
51 | } | |
52 | ||
53 | // StringLenBetween returns a SchemaValidateFunc which tests if the provided value | |
54 | // is of type string and has length between min and max (inclusive) | |
55 | func StringLenBetween(min, max int) schema.SchemaValidateFunc { | |
56 | return func(i interface{}, k string) (s []string, es []error) { | |
57 | v, ok := i.(string) | |
58 | if !ok { | |
59 | es = append(es, fmt.Errorf("expected type of %s to be string", k)) | |
60 | return | |
61 | } | |
62 | if len(v) < min || len(v) > max { | |
63 | es = append(es, fmt.Errorf("expected length of %s to be in the range (%d - %d), got %s", k, min, max, v)) | |
64 | } | |
65 | return | |
66 | } | |
67 | } | |
68 | ||
69 | // CIDRNetwork returns a SchemaValidateFunc which tests if the provided value | |
70 | // is of type string, is in valid CIDR network notation, and has significant bits between min and max (inclusive) | |
71 | func CIDRNetwork(min, max int) schema.SchemaValidateFunc { | |
72 | return func(i interface{}, k string) (s []string, es []error) { | |
73 | v, ok := i.(string) | |
74 | if !ok { | |
75 | es = append(es, fmt.Errorf("expected type of %s to be string", k)) | |
76 | return | |
77 | } | |
78 | ||
79 | _, ipnet, err := net.ParseCIDR(v) | |
80 | if err != nil { | |
81 | es = append(es, fmt.Errorf( | |
82 | "expected %s to contain a valid CIDR, got: %s with err: %s", k, v, err)) | |
83 | return | |
84 | } | |
85 | ||
86 | if ipnet == nil || v != ipnet.String() { | |
87 | es = append(es, fmt.Errorf( | |
88 | "expected %s to contain a valid network CIDR, expected %s, got %s", | |
89 | k, ipnet, v)) | |
90 | } | |
91 | ||
92 | sigbits, _ := ipnet.Mask.Size() | |
93 | if sigbits < min || sigbits > max { | |
94 | es = append(es, fmt.Errorf( | |
95 | "expected %q to contain a network CIDR with between %d and %d significant bits, got: %d", | |
96 | k, min, max, sigbits)) | |
97 | } | |
98 | ||
99 | return | |
100 | } | |
101 | } | |
102 | ||
103 | func ValidateJsonString(v interface{}, k string) (ws []string, errors []error) { | |
104 | if _, err := structure.NormalizeJsonString(v); err != nil { | |
105 | errors = append(errors, fmt.Errorf("%q contains an invalid JSON: %s", k, err)) | |
106 | } | |
107 | return | |
108 | } |