Skip to content

Commit fa8d313

Browse files
authored
fix: add path to error when list has too many or few items (#826)
1 parent 38f7358 commit fa8d313

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

helper/schema/schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,7 +1946,7 @@ func (m schemaMap) validateList(
19461946
return append(diags, diag.Diagnostic{
19471947
Severity: diag.Error,
19481948
Summary: "Too many list items",
1949-
Detail: fmt.Sprintf("Attribute supports %d item maximum, but config has %d declared.", schema.MaxItems, rawV.Len()),
1949+
Detail: fmt.Sprintf("Attribute %s supports %d item maximum, but config has %d declared.", k, schema.MaxItems, rawV.Len()),
19501950
AttributePath: path,
19511951
})
19521952
}
@@ -1955,7 +1955,7 @@ func (m schemaMap) validateList(
19551955
return append(diags, diag.Diagnostic{
19561956
Severity: diag.Error,
19571957
Summary: "Not enough list items",
1958-
Detail: fmt.Sprintf("Attribute requires %d item minimum, but config has only %d declared.", schema.MinItems, rawV.Len()),
1958+
Detail: fmt.Sprintf("Attribute %s requires %d item minimum, but config has only %d declared.", k, schema.MinItems, rawV.Len()),
19591959
AttributePath: path,
19601960
})
19611961
}

helper/schema/schema_test.go

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6841,7 +6841,7 @@ func TestSchemaSet_ValidateMaxItems(t *testing.T) {
68416841
Diff: nil,
68426842
Err: true,
68436843
Errors: []error{
6844-
fmt.Errorf("Error: Too many list items: Attribute supports 1 item maximum, but config has 2 declared."),
6844+
fmt.Errorf("Error: Too many list items: Attribute aliases supports 1 item maximum, but config has 2 declared."),
68456845
},
68466846
},
68476847
"#1": {
@@ -6877,6 +6877,40 @@ func TestSchemaSet_ValidateMaxItems(t *testing.T) {
68776877
Err: false,
68786878
Errors: nil,
68796879
},
6880+
"#3": {
6881+
Schema: map[string]*Schema{
6882+
"service_account": {
6883+
Type: TypeList,
6884+
Optional: true,
6885+
ForceNew: true,
6886+
Elem: &Resource{
6887+
Schema: map[string]*Schema{
6888+
"aliases": {
6889+
Type: TypeSet,
6890+
Optional: true,
6891+
MinItems: 2,
6892+
Elem: &Schema{Type: TypeString},
6893+
},
6894+
},
6895+
},
6896+
},
6897+
},
6898+
6899+
State: nil,
6900+
6901+
Config: map[string]interface{}{
6902+
"service_account": []interface{}{
6903+
map[string]interface{}{
6904+
"aliases": []interface{}{"foo"},
6905+
},
6906+
},
6907+
},
6908+
Diff: nil,
6909+
Err: true,
6910+
Errors: []error{
6911+
fmt.Errorf("Error: Not enough list items: Attribute service_account.0.aliases requires 2 item minimum, but config has only 1 declared."),
6912+
},
6913+
},
68806914
}
68816915

68826916
for tn, tc := range cases {
@@ -6963,7 +6997,41 @@ func TestSchemaSet_ValidateMinItems(t *testing.T) {
69636997
Diff: nil,
69646998
Err: true,
69656999
Errors: []error{
6966-
fmt.Errorf("Error: Not enough list items: Attribute requires 2 item minimum, but config has only 1 declared."),
7000+
fmt.Errorf("Error: Not enough list items: Attribute aliases requires 2 item minimum, but config has only 1 declared."),
7001+
},
7002+
},
7003+
"#3": {
7004+
Schema: map[string]*Schema{
7005+
"service_account": {
7006+
Type: TypeList,
7007+
Optional: true,
7008+
ForceNew: true,
7009+
Elem: &Resource{
7010+
Schema: map[string]*Schema{
7011+
"aliases": {
7012+
Type: TypeSet,
7013+
Optional: true,
7014+
MinItems: 2,
7015+
Elem: &Schema{Type: TypeString},
7016+
},
7017+
},
7018+
},
7019+
},
7020+
},
7021+
7022+
State: nil,
7023+
7024+
Config: map[string]interface{}{
7025+
"service_account": []interface{}{
7026+
map[string]interface{}{
7027+
"aliases": []interface{}{"foo"},
7028+
},
7029+
},
7030+
},
7031+
Diff: nil,
7032+
Err: true,
7033+
Errors: []error{
7034+
fmt.Errorf("Error: Not enough list items: Attribute service_account.0.aliases requires 2 item minimum, but config has only 1 declared."),
69677035
},
69687036
},
69697037
}

0 commit comments

Comments
 (0)