don't append nil elements to a diagnostic cty.Path

If a set element is nil in validateConfigNulls, we don't want to
append that element to the diagnostic path, since it doesn't offer any
useful info to the user.
This commit is contained in:
James Bardin 2019-06-25 18:05:51 -04:00
parent 2bd86cf8bb
commit d4b0788854
2 changed files with 20 additions and 1 deletions

View File

@ -1374,11 +1374,19 @@ func validateConfigNulls(v cty.Value, path cty.Path) []*proto.Diagnostic {
for it.Next() {
kv, ev := it.Element()
if ev.IsNull() {
// if this is a set, the kv is also going to be null which
// isn't a valid path element, so we can't append it to the
// diagnostic.
p := path
if !kv.IsNull() {
p = append(p, cty.IndexStep{Key: kv})
}
diags = append(diags, &proto.Diagnostic{
Severity: proto.Diagnostic_ERROR,
Summary: "Null value found in list",
Detail: "Null values are not allowed for this attribute value.",
Attribute: convert.PathToAttributePath(append(path, cty.IndexStep{Key: kv})),
Attribute: convert.PathToAttributePath(p),
})
continue
}

View File

@ -1352,6 +1352,17 @@ func TestValidateNulls(t *testing.T) {
}),
Err: true,
},
{
Cfg: cty.ObjectVal(map[string]cty.Value{
"object": cty.ObjectVal(map[string]cty.Value{
"list": cty.SetVal([]cty.Value{
cty.StringVal("string"),
cty.NullVal(cty.String),
}),
}),
}),
Err: true,
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
d := validateConfigNulls(tc.Cfg, nil)