Skip to content

Commit

Permalink
fix: add path to error when list has too many or few items (#826)
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-barker committed Aug 29, 2022
1 parent 38f7358 commit fa8d313
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 @@ -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,
})
}
Expand All @@ -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,
})
}
Expand Down
72 changes: 70 additions & 2 deletions helper/schema/schema_test.go
Expand Up @@ -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": {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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."),
},
},
}
Expand Down

0 comments on commit fa8d313

Please sign in to comment.