helper/config: more correct logic with regards to nested things

This commit is contained in:
Mitchell Hashimoto 2014-07-10 17:05:40 -07:00
parent dbe5a1254a
commit 0b2860fafc
2 changed files with 39 additions and 2 deletions

View File

@ -145,7 +145,7 @@ func (v *nestedValidatorKey) Validate(
m map[string]string) ([]string, []string, []error) {
countStr, ok := m[v.Prefix+".#"]
if !ok {
if !v.Required {
if !v.Required || v.Key != "" {
// Not present, that is okay
return nil, nil, nil
} else {

View File

@ -70,6 +70,39 @@ func TestValidator_complex(t *testing.T) {
testInvalid(v, c)
}
func TestValidator_complexDeepRequired(t *testing.T) {
v := &Validator{
Required: []string{
"foo",
"nested.*.foo",
},
}
var c *terraform.ResourceConfig
// Valid
c = testConfig(t, map[string]interface{}{
"foo": "bar",
"nested": []map[string]interface{}{
map[string]interface{}{"foo": "bar"},
},
})
testValid(v, c)
// Valid
c = testConfig(t, map[string]interface{}{
"foo": "bar",
})
testValid(v, c)
// Not a nested structure
c = testConfig(t, map[string]interface{}{
"foo": "bar",
"nested": "baa",
})
testInvalid(v, c)
}
func testConfig(
t *testing.T,
c map[string]interface{}) *terraform.ResourceConfig {
@ -97,6 +130,10 @@ func testValid(v *Validator, c *terraform.ResourceConfig) {
panic(fmt.Sprintf("bad: %#v", ws))
}
if len(es) > 0 {
panic(fmt.Sprintf("bad: %#v", es))
estrs := make([]string, len(es))
for i, e := range es {
estrs[i] = e.Error()
}
panic(fmt.Sprintf("bad: %#v", estrs))
}
}