|
| 1 | +package validation |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | +) |
| 9 | + |
| 10 | +// MapKeyLenBetween returns a SchemaValidateFunc which tests if the provided value |
| 11 | +// is of type map and the length of all keys are between min and max (inclusive) |
| 12 | +func MapKeyLenBetween(min, max int) schema.SchemaValidateFunc { |
| 13 | + return func(i interface{}, k string) (warnings []string, errors []error) { |
| 14 | + v, ok := i.(map[string]interface{}) |
| 15 | + if !ok { |
| 16 | + errors = append(errors, fmt.Errorf("expected type of %[1]q to be Map, got %[1]T", k)) |
| 17 | + return warnings, errors |
| 18 | + } |
| 19 | + |
| 20 | + for key := range v { |
| 21 | + len := len(key) |
| 22 | + if len < min || len > max { |
| 23 | + errors = append(errors, fmt.Errorf("expected the length of all keys of %q to be in the range (%d - %d), got %q (length = %d)", k, min, max, key, len)) |
| 24 | + return warnings, errors |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + return warnings, errors |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// MapValueLenBetween returns a SchemaValidateFunc which tests if the provided value |
| 33 | +// is of type map and the length of all values are between min and max (inclusive) |
| 34 | +func MapValueLenBetween(min, max int) schema.SchemaValidateFunc { |
| 35 | + return func(i interface{}, k string) (warnings []string, errors []error) { |
| 36 | + v, ok := i.(map[string]interface{}) |
| 37 | + if !ok { |
| 38 | + errors = append(errors, fmt.Errorf("expected type of %[1]q to be Map, got %[1]T", k)) |
| 39 | + return warnings, errors |
| 40 | + } |
| 41 | + |
| 42 | + for _, val := range v { |
| 43 | + if _, ok := val.(string); !ok { |
| 44 | + errors = append(errors, fmt.Errorf("expected all values of %[1]q to be strings, found %[2]v (type = %[2]T)", k, val)) |
| 45 | + return warnings, errors |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + for _, val := range v { |
| 50 | + len := len(val.(string)) |
| 51 | + if len < min || len > max { |
| 52 | + errors = append(errors, fmt.Errorf("expected the length of all values of %q to be in the range (%d - %d), got %q (length = %d)", k, min, max, val, len)) |
| 53 | + return warnings, errors |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return warnings, errors |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// MapKeyMatch returns a SchemaValidateFunc which tests if the provided value |
| 62 | +// is of type map and all keys match a given regexp. Optionally an error message |
| 63 | +// can be provided to return something friendlier than "expected to match some globby regexp". |
| 64 | +func MapKeyMatch(r *regexp.Regexp, message string) schema.SchemaValidateFunc { |
| 65 | + return func(i interface{}, k string) (warnings []string, errors []error) { |
| 66 | + v, ok := i.(map[string]interface{}) |
| 67 | + if !ok { |
| 68 | + errors = append(errors, fmt.Errorf("expected type of %[1]q to be Map, got %[1]T", k)) |
| 69 | + return warnings, errors |
| 70 | + } |
| 71 | + |
| 72 | + for key := range v { |
| 73 | + if ok := r.MatchString(key); !ok { |
| 74 | + if message != "" { |
| 75 | + errors = append(errors, fmt.Errorf("invalid key %q for %q (%s)", key, k, message)) |
| 76 | + return warnings, errors |
| 77 | + } |
| 78 | + |
| 79 | + errors = append(errors, fmt.Errorf("invalid key %q for %q (expected to match regular expression %q)", key, k, r)) |
| 80 | + return warnings, errors |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return warnings, errors |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// MapValueMatch returns a SchemaValidateFunc which tests if the provided value |
| 89 | +// is of type map and all values match a given regexp. Optionally an error message |
| 90 | +// can be provided to return something friendlier than "expected to match some globby regexp". |
| 91 | +func MapValueMatch(r *regexp.Regexp, message string) schema.SchemaValidateFunc { |
| 92 | + return func(i interface{}, k string) (warnings []string, errors []error) { |
| 93 | + v, ok := i.(map[string]interface{}) |
| 94 | + if !ok { |
| 95 | + errors = append(errors, fmt.Errorf("expected type of %[1]q to be Map, got %[1]T", k)) |
| 96 | + return warnings, errors |
| 97 | + } |
| 98 | + |
| 99 | + for _, val := range v { |
| 100 | + if _, ok := val.(string); !ok { |
| 101 | + errors = append(errors, fmt.Errorf("expected all values of %[1]q to be strings, found %[2]v (type = %[2]T)", k, val)) |
| 102 | + return warnings, errors |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + for _, val := range v { |
| 107 | + if ok := r.MatchString(val.(string)); !ok { |
| 108 | + if message != "" { |
| 109 | + errors = append(errors, fmt.Errorf("invalid value %q for %q (%s)", val, k, message)) |
| 110 | + return warnings, errors |
| 111 | + } |
| 112 | + |
| 113 | + errors = append(errors, fmt.Errorf("invalid value %q for %q (expected to match regular expression %q)", val, k, r)) |
| 114 | + return warnings, errors |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return warnings, errors |
| 119 | + } |
| 120 | +} |
0 commit comments