Merge pull request #19102 from hashicorp/v-helper-validation-All-IntInSlice
helper/validation: Add All() and IntInSlice() SchemaValidateFunc
This commit is contained in:
commit
02e135bac8
|
@ -13,6 +13,21 @@ import (
|
||||||
"github.com/hashicorp/terraform/helper/structure"
|
"github.com/hashicorp/terraform/helper/structure"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// All returns a SchemaValidateFunc which tests if the provided value
|
||||||
|
// passes all provided SchemaValidateFunc
|
||||||
|
func All(validators ...schema.SchemaValidateFunc) schema.SchemaValidateFunc {
|
||||||
|
return func(i interface{}, k string) ([]string, []error) {
|
||||||
|
var allErrors []error
|
||||||
|
var allWarnings []string
|
||||||
|
for _, validator := range validators {
|
||||||
|
validatorWarnings, validatorErrors := validator(i, k)
|
||||||
|
allWarnings = append(allWarnings, validatorWarnings...)
|
||||||
|
allErrors = append(allErrors, validatorErrors...)
|
||||||
|
}
|
||||||
|
return allWarnings, allErrors
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// IntBetween returns a SchemaValidateFunc which tests if the provided value
|
// IntBetween returns a SchemaValidateFunc which tests if the provided value
|
||||||
// is of type int and is between min and max (inclusive)
|
// is of type int and is between min and max (inclusive)
|
||||||
func IntBetween(min, max int) schema.SchemaValidateFunc {
|
func IntBetween(min, max int) schema.SchemaValidateFunc {
|
||||||
|
@ -70,6 +85,27 @@ func IntAtMost(max int) schema.SchemaValidateFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IntInSlice returns a SchemaValidateFunc which tests if the provided value
|
||||||
|
// is of type int and matches the value of an element in the valid slice
|
||||||
|
func IntInSlice(valid []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 an integer", k))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, validInt := range valid {
|
||||||
|
if v == validInt {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
es = append(es, fmt.Errorf("expected %s to be one of %v, got %d", k, valid, v))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// StringInSlice returns a SchemaValidateFunc which tests if the provided value
|
// 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
|
// 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
|
// will test with in lower case if ignoreCase is true
|
||||||
|
|
|
@ -13,6 +13,34 @@ type testCase struct {
|
||||||
expectedErr *regexp.Regexp
|
expectedErr *regexp.Regexp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidationAll(t *testing.T) {
|
||||||
|
runTestCases(t, []testCase{
|
||||||
|
{
|
||||||
|
val: "valid",
|
||||||
|
f: All(
|
||||||
|
StringLenBetween(5, 42),
|
||||||
|
StringMatch(regexp.MustCompile(`[a-zA-Z0-9]+`), "value must be alphanumeric"),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
val: "foo",
|
||||||
|
f: All(
|
||||||
|
StringLenBetween(5, 42),
|
||||||
|
StringMatch(regexp.MustCompile(`[a-zA-Z0-9]+`), "value must be alphanumeric"),
|
||||||
|
),
|
||||||
|
expectedErr: regexp.MustCompile("expected length of [\\w]+ to be in the range \\(5 - 42\\), got foo"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
val: "!!!!!",
|
||||||
|
f: All(
|
||||||
|
StringLenBetween(5, 42),
|
||||||
|
StringMatch(regexp.MustCompile(`[a-zA-Z0-9]+`), "value must be alphanumeric"),
|
||||||
|
),
|
||||||
|
expectedErr: regexp.MustCompile("value must be alphanumeric"),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestValidationIntBetween(t *testing.T) {
|
func TestValidationIntBetween(t *testing.T) {
|
||||||
runTestCases(t, []testCase{
|
runTestCases(t, []testCase{
|
||||||
{
|
{
|
||||||
|
@ -82,6 +110,25 @@ func TestValidationIntAtMost(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidationIntInSlice(t *testing.T) {
|
||||||
|
runTestCases(t, []testCase{
|
||||||
|
{
|
||||||
|
val: 42,
|
||||||
|
f: IntInSlice([]int{1, 42}),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
val: 42,
|
||||||
|
f: IntInSlice([]int{10, 20}),
|
||||||
|
expectedErr: regexp.MustCompile("expected [\\w]+ to be one of \\[10 20\\], got 42"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
val: "InvalidValue",
|
||||||
|
f: IntInSlice([]int{10, 20}),
|
||||||
|
expectedErr: regexp.MustCompile("expected type of [\\w]+ to be an integer"),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestValidationStringInSlice(t *testing.T) {
|
func TestValidationStringInSlice(t *testing.T) {
|
||||||
runTestCases(t, []testCase{
|
runTestCases(t, []testCase{
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue