config: Generate error copy from valid types map

Renders as:

```
Variable 'invalid_type' type must be one of [string, map, list] - 'not_a_type' is not a valid type
```
This commit is contained in:
Justin Campbell 2017-04-20 15:41:33 -04:00 committed by Martin Atkins
parent cf775ded0e
commit c1c3127d8e
2 changed files with 11 additions and 2 deletions

View File

@ -1017,7 +1017,16 @@ func (v *Variable) ValidateTypeAndDefault() error {
// If an explicit type is declared, ensure it is valid // If an explicit type is declared, ensure it is valid
if v.DeclaredType != "" { if v.DeclaredType != "" {
if _, ok := typeStringMap[v.DeclaredType]; !ok { if _, ok := typeStringMap[v.DeclaredType]; !ok {
return fmt.Errorf("Variable '%s' must be of type string, map, or list - '%s' is not a valid type", v.Name, v.DeclaredType) validTypes := []string{}
for k := range typeStringMap {
validTypes = append(validTypes, k)
}
return fmt.Errorf(
"Variable '%s' type must be one of [%s] - '%s' is not a valid type",
v.Name,
strings.Join(validTypes, ", "),
v.DeclaredType,
)
} }
} }

View File

@ -684,7 +684,7 @@ func TestLoadFile_badVariableTypes(t *testing.T) {
} }
errorStr := err.Error() errorStr := err.Error()
if !strings.Contains(errorStr, "'bad_type' must be of type string") { if !strings.Contains(errorStr, "'bad_type' type must be one of") {
t.Fatalf("bad: expected error has wrong text: %s", errorStr) t.Fatalf("bad: expected error has wrong text: %s", errorStr)
} }
} }