helper/config: can validate array configurations

This commit is contained in:
Mitchell Hashimoto 2014-07-14 18:22:05 -07:00
parent 72b7e2c11a
commit 35071f0328
2 changed files with 31 additions and 2 deletions

View File

@ -84,6 +84,8 @@ func (v *Validator) Validate(
} }
type validatorKey interface { type validatorKey interface {
// Validate validates the given configuration and returns viewed keys,
// warnings, and errors.
Validate(map[string]string) ([]string, []string, []error) Validate(map[string]string) ([]string, []string, []error)
} }
@ -179,9 +181,11 @@ func (v *nestedValidatorKey) Validate(
} }
for k, _ := range m { for k, _ := range m {
if k != prefix[:len(prefix)-1] {
if !strings.HasPrefix(k, prefix) { if !strings.HasPrefix(k, prefix) {
continue continue
} }
}
used = append(used, k) used = append(used, k)
} }

View File

@ -43,6 +43,31 @@ func TestValidator(t *testing.T) {
testInvalid(v, c) testInvalid(v, c)
} }
func TestValidator_array(t *testing.T) {
v := &Validator{
Required: []string{
"foo",
"nested.*",
},
}
var c *terraform.ResourceConfig
// Valid
c = testConfig(t, map[string]interface{}{
"foo": "bar",
"nested": []string{"foo", "bar"},
})
testValid(v, c)
// Not a nested structure
c = testConfig(t, map[string]interface{}{
"foo": "bar",
"nested": "baa",
})
testInvalid(v, c)
}
func TestValidator_complex(t *testing.T) { func TestValidator_complex(t *testing.T) {
v := &Validator{ v := &Validator{
Required: []string{ Required: []string{