Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add path to error when list has too many or few items #826

Merged
merged 1 commit into from Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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