From 35071f03281a61a8d7c9d0ee0a20ab4684c48337 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 14 Jul 2014 18:22:05 -0700 Subject: [PATCH] helper/config: can validate array configurations --- helper/config/validator.go | 8 ++++++-- helper/config/validator_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/helper/config/validator.go b/helper/config/validator.go index 1d8616c79..2b973237c 100644 --- a/helper/config/validator.go +++ b/helper/config/validator.go @@ -84,6 +84,8 @@ func (v *Validator) Validate( } type validatorKey interface { + // Validate validates the given configuration and returns viewed keys, + // warnings, and errors. Validate(map[string]string) ([]string, []string, []error) } @@ -179,8 +181,10 @@ func (v *nestedValidatorKey) Validate( } for k, _ := range m { - if !strings.HasPrefix(k, prefix) { - continue + if k != prefix[:len(prefix)-1] { + if !strings.HasPrefix(k, prefix) { + continue + } } used = append(used, k) diff --git a/helper/config/validator_test.go b/helper/config/validator_test.go index 3886c6b91..d09fd2044 100644 --- a/helper/config/validator_test.go +++ b/helper/config/validator_test.go @@ -43,6 +43,31 @@ func TestValidator(t *testing.T) { 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) { v := &Validator{ Required: []string{