mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
6b5ca49578
With the SDK moving out into its own repository, a lot of the packages under "helper/" are no longer needed. We still need to keep around just enough legacy SDK to support the "test" provider and some little bits and bobs in the backends and provisioners, but a lot of this is now just dead code. One of the test provider tests was depending on some validation functions only because the schema in there was originally copied from a "real" provider. The validation rules are not actually important for the test, so I removed them. Otherwise, this removes only dead code.
50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package validation
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
)
|
|
|
|
// IntBetween returns a SchemaValidateFunc which tests if the provided value
|
|
// is of type int and is between min and max (inclusive)
|
|
func IntBetween(min, max int) schema.SchemaValidateFunc {
|
|
return func(i interface{}, k string) (s []string, es []error) {
|
|
v, ok := i.(int)
|
|
if !ok {
|
|
es = append(es, fmt.Errorf("expected type of %s to be int", k))
|
|
return
|
|
}
|
|
|
|
if v < min || v > max {
|
|
es = append(es, fmt.Errorf("expected %s to be in the range (%d - %d), got %d", k, min, max, v))
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
}
|
|
|
|
// StringInSlice returns a SchemaValidateFunc which tests if the provided value
|
|
// is of type string and matches the value of an element in the valid slice
|
|
// will test with in lower case if ignoreCase is true
|
|
func StringInSlice(valid []string, ignoreCase bool) schema.SchemaValidateFunc {
|
|
return func(i interface{}, k string) (s []string, es []error) {
|
|
v, ok := i.(string)
|
|
if !ok {
|
|
es = append(es, fmt.Errorf("expected type of %s to be string", k))
|
|
return
|
|
}
|
|
|
|
for _, str := range valid {
|
|
if v == str || (ignoreCase && strings.ToLower(v) == strings.ToLower(str)) {
|
|
return
|
|
}
|
|
}
|
|
|
|
es = append(es, fmt.Errorf("expected %s to be one of %v, got %s", k, valid, v))
|
|
return
|
|
}
|
|
}
|