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

Return all errors from Map KeysAre validators #74

Merged
merged 2 commits into from
Nov 16, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/74.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
mapvalidator: Updated `KeysAre()` to return all errors instead of just the first
```
3 changes: 0 additions & 3 deletions mapvalidator/keys_are.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ func (v keysAreValidator) Validate(ctx context.Context, req tfsdk.ValidateAttrib

for _, validator := range v.keyValidators {
validator.Validate(ctx, request, resp)
if resp.Diagnostics.HasError() {
return
}
}
}
}
Expand Down
37 changes: 23 additions & 14 deletions mapvalidator/keys_are_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ func TestKeysAreValidator(t *testing.T) {
type testCase struct {
val attr.Value
keysAreValidators []tfsdk.AttributeValidator
expectError bool
expectErrorsCount int
}
tests := map[string]testCase{
"not Map": {
val: types.List{
ElemType: types.StringType,
},
expectError: true,
expectErrorsCount: 1,
},
"Map unknown": {
val: types.Map{
Unknown: true,
ElemType: types.StringType,
},
expectError: false,
expectErrorsCount: 0,
},
"Map null": {
val: types.Map{
Null: true,
ElemType: types.StringType,
},
expectError: false,
expectErrorsCount: 0,
},
"Map key invalid": {
val: types.Map{
Expand All @@ -53,7 +53,7 @@ func TestKeysAreValidator(t *testing.T) {
keysAreValidators: []tfsdk.AttributeValidator{
stringvalidator.LengthAtLeast(4),
},
expectError: true,
expectErrorsCount: 2,
},
"Map key invalid for second validator": {
val: types.Map{
Expand All @@ -67,7 +67,7 @@ func TestKeysAreValidator(t *testing.T) {
stringvalidator.LengthAtLeast(2),
stringvalidator.LengthAtLeast(6),
},
expectError: true,
expectErrorsCount: 2,
},
"Map keys wrong type for validator": {
val: types.Map{
Expand All @@ -80,7 +80,20 @@ func TestKeysAreValidator(t *testing.T) {
keysAreValidators: []tfsdk.AttributeValidator{
int64validator.AtLeast(6),
},
expectError: true,
expectErrorsCount: 1,
},
"Map keys for invalid multiple validators": {
val: types.Map{
ElemType: types.StringType,
Elems: map[string]attr.Value{
"one": types.String{Value: "first"},
},
},
keysAreValidators: []tfsdk.AttributeValidator{
stringvalidator.LengthAtLeast(5),
stringvalidator.LengthAtLeast(6),
},
expectErrorsCount: 2,
},
"Map keys valid": {
val: types.Map{
Expand All @@ -93,7 +106,7 @@ func TestKeysAreValidator(t *testing.T) {
keysAreValidators: []tfsdk.AttributeValidator{
stringvalidator.LengthAtLeast(3),
},
expectError: false,
expectErrorsCount: 0,
},
}

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

if !response.Diagnostics.HasError() && test.expectError {
t.Fatal("expected error, got no error")
}

if response.Diagnostics.HasError() && !test.expectError {
t.Fatalf("got unexpected error: %s", response.Diagnostics)
if response.Diagnostics.ErrorsCount() != test.expectErrorsCount {
t.Fatalf("expected %d errors, but got %d: %s", test.expectErrorsCount, response.Diagnostics.ErrorsCount(), response.Diagnostics)
}
})
}
Expand Down