/
validators.go
62 lines (54 loc) · 1.65 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
package imaging
import (
"strconv"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func stringAsIntBetween(lowerLimitRaw, upperLimitRaw interface{}) schema.SchemaValidateDiagFunc {
return func(i interface{}, path cty.Path) diag.Diagnostics {
value, err := strconv.Atoi(i.(string))
if err != nil {
return diag.Errorf("incorrect attribute value type: expect int")
}
if lowerLimitRaw != nil {
lowerLimit := lowerLimitRaw.(int)
if value < lowerLimit {
return diag.Errorf("provided value %d must be at least %d", value, lowerLimit)
}
}
if upperLimitRaw != nil {
upperLimit := upperLimitRaw.(int)
if value > upperLimit {
return diag.Errorf("provided value %d must be at most %d", value, upperLimit)
}
}
return nil
}
}
func validateIsTypeFloat() schema.SchemaValidateDiagFunc {
return func(i interface{}, path cty.Path) diag.Diagnostics {
if _, err := strconv.ParseFloat(i.(string), 64); err != nil {
return diag.Errorf("incorrect attribute value type: expect float64")
}
return nil
}
}
func validateIsTypeInt() schema.SchemaValidateDiagFunc {
return func(i interface{}, path cty.Path) diag.Diagnostics {
_, err := strconv.Atoi(i.(string))
if err != nil {
return diag.Errorf("incorrect attribute value type: expect int")
}
return nil
}
}
func validateIsTypeBool() schema.SchemaValidateDiagFunc {
return func(i interface{}, path cty.Path) diag.Diagnostics {
value := i.(string)
if value != "false" && value != "true" {
return diag.Errorf("incorrect attribute value type: expect boolean")
}
return nil
}
}