From aea28f5b3de6fa879636958d509773ab0cdf03de Mon Sep 17 00:00:00 2001 From: Matt Good Date: Mon, 7 Nov 2022 13:45:19 -0800 Subject: [PATCH 1/2] Return all errors from Map KeysAre validators Fixes #73 --- mapvalidator/keys_are.go | 3 --- mapvalidator/keys_are_test.go | 37 ++++++++++++++++++++++------------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/mapvalidator/keys_are.go b/mapvalidator/keys_are.go index 5bdf7bf..a646669 100644 --- a/mapvalidator/keys_are.go +++ b/mapvalidator/keys_are.go @@ -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 - } } } } diff --git a/mapvalidator/keys_are_test.go b/mapvalidator/keys_are_test.go index 4004b5d..0eecda1 100644 --- a/mapvalidator/keys_are_test.go +++ b/mapvalidator/keys_are_test.go @@ -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{ @@ -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{ @@ -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{ @@ -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{ @@ -93,7 +106,7 @@ func TestKeysAreValidator(t *testing.T) { keysAreValidators: []tfsdk.AttributeValidator{ stringvalidator.LengthAtLeast(3), }, - expectError: false, + expectErrorsCount: 0, }, } @@ -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) } }) } From 96c0628b4e8e0061d17ae2a813d8d5d0fe10ff59 Mon Sep 17 00:00:00 2001 From: Matt Good Date: Mon, 7 Nov 2022 13:58:26 -0800 Subject: [PATCH 2/2] add changelog entry --- .changelog/74.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/74.txt diff --git a/.changelog/74.txt b/.changelog/74.txt new file mode 100644 index 0000000..6d79c6f --- /dev/null +++ b/.changelog/74.txt @@ -0,0 +1,3 @@ +```release-note:bug +mapvalidator: Updated `KeysAre()` to return all errors instead of just the first +```