Skip to content

Commit

Permalink
fix: add path to error when list has too many or few items
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-barker committed Nov 30, 2021
1 parent 76b3563 commit 84f1efb
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
4 changes: 2 additions & 2 deletions helper/schema/schema.go
Expand Up @@ -1721,7 +1721,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,
})
}
Expand All @@ -1730,7 +1730,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,
})
}
Expand Down
72 changes: 70 additions & 2 deletions helper/schema/schema_test.go
Expand Up @@ -6583,7 +6583,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": {
Expand Down Expand Up @@ -6619,6 +6619,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 {
Expand Down Expand Up @@ -6705,7 +6739,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."),
},
},
}
Expand Down

0 comments on commit 84f1efb

Please sign in to comment.