Skip to content

Commit b6de2e8

Browse files
authored
Return all errors from Map KeysAre validators (#74)
* Return all errors from Map KeysAre validators Fixes #73 * add changelog entry Co-authored-by: Matt Good <[email protected]>
1 parent 24f2c3e commit b6de2e8

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

.changelog/74.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
mapvalidator: Updated `KeysAre()` to return all errors instead of just the first
3+
```

mapvalidator/keys_are.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ func (v keysAreValidator) Validate(ctx context.Context, req tfsdk.ValidateAttrib
5252

5353
for _, validator := range v.keyValidators {
5454
validator.Validate(ctx, request, resp)
55-
if resp.Diagnostics.HasError() {
56-
return
57-
}
5855
}
5956
}
6057
}

mapvalidator/keys_are_test.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,28 @@ func TestKeysAreValidator(t *testing.T) {
1919
type testCase struct {
2020
val attr.Value
2121
keysAreValidators []tfsdk.AttributeValidator
22-
expectError bool
22+
expectErrorsCount int
2323
}
2424
tests := map[string]testCase{
2525
"not Map": {
2626
val: types.List{
2727
ElemType: types.StringType,
2828
},
29-
expectError: true,
29+
expectErrorsCount: 1,
3030
},
3131
"Map unknown": {
3232
val: types.Map{
3333
Unknown: true,
3434
ElemType: types.StringType,
3535
},
36-
expectError: false,
36+
expectErrorsCount: 0,
3737
},
3838
"Map null": {
3939
val: types.Map{
4040
Null: true,
4141
ElemType: types.StringType,
4242
},
43-
expectError: false,
43+
expectErrorsCount: 0,
4444
},
4545
"Map key invalid": {
4646
val: types.Map{
@@ -53,7 +53,7 @@ func TestKeysAreValidator(t *testing.T) {
5353
keysAreValidators: []tfsdk.AttributeValidator{
5454
stringvalidator.LengthAtLeast(4),
5555
},
56-
expectError: true,
56+
expectErrorsCount: 2,
5757
},
5858
"Map key invalid for second validator": {
5959
val: types.Map{
@@ -67,7 +67,7 @@ func TestKeysAreValidator(t *testing.T) {
6767
stringvalidator.LengthAtLeast(2),
6868
stringvalidator.LengthAtLeast(6),
6969
},
70-
expectError: true,
70+
expectErrorsCount: 2,
7171
},
7272
"Map keys wrong type for validator": {
7373
val: types.Map{
@@ -80,7 +80,20 @@ func TestKeysAreValidator(t *testing.T) {
8080
keysAreValidators: []tfsdk.AttributeValidator{
8181
int64validator.AtLeast(6),
8282
},
83-
expectError: true,
83+
expectErrorsCount: 1,
84+
},
85+
"Map keys for invalid multiple validators": {
86+
val: types.Map{
87+
ElemType: types.StringType,
88+
Elems: map[string]attr.Value{
89+
"one": types.String{Value: "first"},
90+
},
91+
},
92+
keysAreValidators: []tfsdk.AttributeValidator{
93+
stringvalidator.LengthAtLeast(5),
94+
stringvalidator.LengthAtLeast(6),
95+
},
96+
expectErrorsCount: 2,
8497
},
8598
"Map keys valid": {
8699
val: types.Map{
@@ -93,7 +106,7 @@ func TestKeysAreValidator(t *testing.T) {
93106
keysAreValidators: []tfsdk.AttributeValidator{
94107
stringvalidator.LengthAtLeast(3),
95108
},
96-
expectError: false,
109+
expectErrorsCount: 0,
97110
},
98111
}
99112

@@ -108,12 +121,8 @@ func TestKeysAreValidator(t *testing.T) {
108121
response := tfsdk.ValidateAttributeResponse{}
109122
KeysAre(test.keysAreValidators...).Validate(context.TODO(), request, &response)
110123

111-
if !response.Diagnostics.HasError() && test.expectError {
112-
t.Fatal("expected error, got no error")
113-
}
114-
115-
if response.Diagnostics.HasError() && !test.expectError {
116-
t.Fatalf("got unexpected error: %s", response.Diagnostics)
124+
if response.Diagnostics.ErrorsCount() != test.expectErrorsCount {
125+
t.Fatalf("expected %d errors, but got %d: %s", test.expectErrorsCount, response.Diagnostics.ErrorsCount(), response.Diagnostics)
117126
}
118127
})
119128
}

0 commit comments

Comments
 (0)