From 8a8f811b7101094320545890960b96469c705233 Mon Sep 17 00:00:00 2001 From: Josh Barker Date: Wed, 1 Dec 2021 10:54:36 +1100 Subject: [PATCH] fix: add path to error when list has too many or few items --- helper/schema/schema.go | 4 +- helper/schema/schema_test.go | 72 +++++++++++++++++++++++++++++++++++- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/helper/schema/schema.go b/helper/schema/schema.go index 25c2aa088d..22fe196b77 100644 --- a/helper/schema/schema.go +++ b/helper/schema/schema.go @@ -1946,7 +1946,7 @@ func (m schemaMap) validateList( return append(diags, diag.Diagnostic{ Severity: diag.Error, Summary: "Too many list items", - Detail: fmt.Sprintf("Attribute supports %d item maximum, but config has %d declared.", schema.MaxItems, rawV.Len()), + Detail: fmt.Sprintf("Attribute %s supports %d item maximum, but config has %d declared.", k, schema.MaxItems, rawV.Len()), AttributePath: path, }) } @@ -1955,7 +1955,7 @@ func (m schemaMap) validateList( return append(diags, diag.Diagnostic{ Severity: diag.Error, Summary: "Not enough list items", - Detail: fmt.Sprintf("Attribute requires %d item minimum, but config has only %d declared.", schema.MinItems, rawV.Len()), + Detail: fmt.Sprintf("Attribute %s requires %d item minimum, but config has only %d declared.", k, schema.MinItems, rawV.Len()), AttributePath: path, }) } diff --git a/helper/schema/schema_test.go b/helper/schema/schema_test.go index a39250fe14..0dec6bb27e 100644 --- a/helper/schema/schema_test.go +++ b/helper/schema/schema_test.go @@ -6841,7 +6841,7 @@ func TestSchemaSet_ValidateMaxItems(t *testing.T) { Diff: nil, Err: true, Errors: []error{ - fmt.Errorf("Error: Too many list items: Attribute supports 1 item maximum, but config has 2 declared."), + fmt.Errorf("Error: Too many list items: Attribute aliases supports 1 item maximum, but config has 2 declared."), }, }, "#1": { @@ -6877,6 +6877,40 @@ func TestSchemaSet_ValidateMaxItems(t *testing.T) { Err: false, Errors: nil, }, + "#3": { + Schema: map[string]*Schema{ + "service_account": { + Type: TypeList, + Optional: true, + ForceNew: true, + Elem: &Resource{ + Schema: map[string]*Schema{ + "aliases": { + Type: TypeSet, + Optional: true, + MinItems: 2, + Elem: &Schema{Type: TypeString}, + }, + }, + }, + }, + }, + + State: nil, + + Config: map[string]interface{}{ + "service_account": []interface{}{ + map[string]interface{}{ + "aliases": []interface{}{"foo"}, + }, + }, + }, + Diff: nil, + Err: true, + Errors: []error{ + fmt.Errorf("Error: Not enough list items: Attribute service_account.0.aliases requires 2 item minimum, but config has only 1 declared."), + }, + }, } for tn, tc := range cases { @@ -6963,7 +6997,41 @@ func TestSchemaSet_ValidateMinItems(t *testing.T) { Diff: nil, Err: true, Errors: []error{ - fmt.Errorf("Error: Not enough list items: Attribute requires 2 item minimum, but config has only 1 declared."), + fmt.Errorf("Error: Not enough list items: Attribute aliases requires 2 item minimum, but config has only 1 declared."), + }, + }, + "#3": { + Schema: map[string]*Schema{ + "service_account": { + Type: TypeList, + Optional: true, + ForceNew: true, + Elem: &Resource{ + Schema: map[string]*Schema{ + "aliases": { + Type: TypeSet, + Optional: true, + MinItems: 2, + Elem: &Schema{Type: TypeString}, + }, + }, + }, + }, + }, + + State: nil, + + Config: map[string]interface{}{ + "service_account": []interface{}{ + map[string]interface{}{ + "aliases": []interface{}{"foo"}, + }, + }, + }, + Diff: nil, + Err: true, + Errors: []error{ + fmt.Errorf("Error: Not enough list items: Attribute service_account.0.aliases requires 2 item minimum, but config has only 1 declared."), }, }, }