-
Notifications
You must be signed in to change notification settings - Fork 11
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
Adding List validation for SizeAtLeast, SizeAtMost and SizeBetween #41
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
62733e1
Adding List element validation for ValuesAre (#11)
bendbennett 680e7b2
Renaming test file (#11)
bendbennett 013ecc8
Rename test (#11)
bendbennett 4c34410
Fix tests (#11)
bendbennett 02a900d
Updating CHANGELOG (#11)
bendbennett 58598f8
Adding doc.go for listvalidator pkg (#11)
bendbennett d237058
Adding docs and completing all validations before returning (#11)
bendbennett 1ad4392
Rename .changelog file to match PR number and remove updates to CHANG…
bendbennett 1d01994
Adding List validation for SizeAtLeast, SizeAtMost and SizeBetween (#4)
bendbennett f207368
Update following code review (#4)
bendbennett a3d27de
Rename .changelog file to match PR number (#4)
bendbennett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:feature | ||
Introduced `listvalidator` package with `ValuesAre()` validation functions | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:feature | ||
Added `SizeAtLeast()`, `SizeAtMost()` and `SizeBetween` validation functions to `listvalidator` package | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package listvalidator provides validators for types.List attributes. | ||
package listvalidator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package listvalidator | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag" | ||
) | ||
|
||
var _ tfsdk.AttributeValidator = sizeAtLeastValidator{} | ||
|
||
// sizeAtLeastValidator validates that list contains at least min elements. | ||
type sizeAtLeastValidator struct { | ||
min int | ||
} | ||
|
||
// Description describes the validation in plain text formatting. | ||
func (v sizeAtLeastValidator) Description(ctx context.Context) string { | ||
return fmt.Sprintf("list must contain at least %d elements", v.min) | ||
} | ||
|
||
// MarkdownDescription describes the validation in Markdown formatting. | ||
func (v sizeAtLeastValidator) MarkdownDescription(ctx context.Context) string { | ||
return v.Description(ctx) | ||
} | ||
|
||
// Validate performs the validation. | ||
func (v sizeAtLeastValidator) Validate(ctx context.Context, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) { | ||
elems, ok := validateList(ctx, req, resp) | ||
if !ok { | ||
return | ||
} | ||
|
||
if len(elems) < v.min { | ||
resp.Diagnostics.Append(validatordiag.AttributeValueDiagnostic( | ||
req.AttributePath, | ||
v.Description(ctx), | ||
fmt.Sprintf("%d", len(elems)), | ||
)) | ||
|
||
return | ||
} | ||
} | ||
|
||
// SizeAtLeast returns an AttributeValidator which ensures that any configured | ||
// attribute value: | ||
// | ||
// - Is a List. | ||
// - Contains at least min elements. | ||
// | ||
// Null (unconfigured) and unknown (known after apply) values are skipped. | ||
func SizeAtLeast(min int) tfsdk.AttributeValidator { | ||
return sizeAtLeastValidator{ | ||
min: min, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package listvalidator | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
) | ||
|
||
func TestSizeAtLeastValidator(t *testing.T) { | ||
t.Parallel() | ||
|
||
type testCase struct { | ||
val attr.Value | ||
min int | ||
expectError bool | ||
} | ||
tests := map[string]testCase{ | ||
"not a List": { | ||
val: types.Bool{Value: true}, | ||
expectError: true, | ||
}, | ||
"List unknown": { | ||
val: types.List{ | ||
Unknown: true, | ||
ElemType: types.StringType, | ||
}, | ||
expectError: false, | ||
}, | ||
"List null": { | ||
val: types.List{ | ||
Null: true, | ||
ElemType: types.StringType, | ||
}, | ||
expectError: false, | ||
}, | ||
"List size greater than min": { | ||
val: types.List{ | ||
ElemType: types.StringType, | ||
Elems: []attr.Value{ | ||
types.String{Value: "first"}, | ||
types.String{Value: "second"}, | ||
}, | ||
}, | ||
min: 1, | ||
expectError: false, | ||
}, | ||
"List size equal to min": { | ||
val: types.List{ | ||
ElemType: types.StringType, | ||
Elems: []attr.Value{ | ||
types.String{Value: "first"}, | ||
}, | ||
}, | ||
min: 1, | ||
expectError: false, | ||
}, | ||
"List size less than min": { | ||
val: types.List{ | ||
ElemType: types.StringType, | ||
Elems: []attr.Value{}, | ||
}, | ||
min: 1, | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
request := tfsdk.ValidateAttributeRequest{ | ||
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), | ||
AttributeConfig: test.val, | ||
} | ||
response := tfsdk.ValidateAttributeResponse{} | ||
SizeAtLeast(test.min).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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package listvalidator | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag" | ||
) | ||
|
||
var _ tfsdk.AttributeValidator = sizeAtMostValidator{} | ||
|
||
// sizeAtMostValidator validates that list contains at most max elements. | ||
type sizeAtMostValidator struct { | ||
max int | ||
} | ||
|
||
// Description describes the validation in plain text formatting. | ||
func (v sizeAtMostValidator) Description(ctx context.Context) string { | ||
return fmt.Sprintf("list must contain at most %d elements", v.max) | ||
} | ||
|
||
// MarkdownDescription describes the validation in Markdown formatting. | ||
func (v sizeAtMostValidator) MarkdownDescription(ctx context.Context) string { | ||
return v.Description(ctx) | ||
} | ||
|
||
// Validate performs the validation. | ||
func (v sizeAtMostValidator) Validate(ctx context.Context, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) { | ||
elems, ok := validateList(ctx, req, resp) | ||
if !ok { | ||
return | ||
} | ||
|
||
if len(elems) > v.max { | ||
resp.Diagnostics.Append(validatordiag.AttributeValueDiagnostic( | ||
req.AttributePath, | ||
v.Description(ctx), | ||
fmt.Sprintf("%d", len(elems)), | ||
)) | ||
|
||
return | ||
} | ||
} | ||
|
||
// SizeAtMost returns an AttributeValidator which ensures that any configured | ||
// attribute value: | ||
// | ||
// - Is a List. | ||
// - Contains at most max elements. | ||
// | ||
// Null (unconfigured) and unknown (known after apply) values are skipped. | ||
func SizeAtMost(max int) tfsdk.AttributeValidator { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Go documentation 😉 |
||
return sizeAtMostValidator{ | ||
max: max, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package listvalidator | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
) | ||
|
||
func TestSizeAtMostValidator(t *testing.T) { | ||
t.Parallel() | ||
|
||
type testCase struct { | ||
val attr.Value | ||
max int | ||
expectError bool | ||
} | ||
tests := map[string]testCase{ | ||
"not a List": { | ||
val: types.Bool{Value: true}, | ||
expectError: true, | ||
}, | ||
"List unknown": { | ||
val: types.List{ | ||
Unknown: true, | ||
ElemType: types.StringType, | ||
}, | ||
expectError: false, | ||
}, | ||
"List null": { | ||
val: types.List{ | ||
Null: true, | ||
ElemType: types.StringType, | ||
}, | ||
expectError: false, | ||
}, | ||
"List size less than max": { | ||
val: types.List{ | ||
ElemType: types.StringType, | ||
Elems: []attr.Value{ | ||
types.String{Value: "first"}, | ||
}, | ||
}, | ||
max: 2, | ||
expectError: false, | ||
}, | ||
"List size equal to max": { | ||
val: types.List{ | ||
ElemType: types.StringType, | ||
Elems: []attr.Value{ | ||
types.String{Value: "first"}, | ||
types.String{Value: "second"}, | ||
}, | ||
}, | ||
max: 2, | ||
expectError: false, | ||
}, | ||
"List size greater than max": { | ||
val: types.List{ | ||
ElemType: types.StringType, | ||
Elems: []attr.Value{ | ||
types.String{Value: "first"}, | ||
types.String{Value: "second"}, | ||
types.String{Value: "third"}, | ||
}}, | ||
max: 2, | ||
expectError: true, | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
request := tfsdk.ValidateAttributeRequest{ | ||
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"), | ||
AttributeConfig: test.val, | ||
} | ||
response := tfsdk.ValidateAttributeResponse{} | ||
SizeAtMost(test.max).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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package listvalidator | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/tfsdk" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag" | ||
) | ||
|
||
var _ tfsdk.AttributeValidator = sizeBetweenValidator{} | ||
|
||
// sizeBetweenValidator validates that list contains at least min elements | ||
// and at most max elements. | ||
type sizeBetweenValidator struct { | ||
min int | ||
max int | ||
} | ||
|
||
// Description describes the validation in plain text formatting. | ||
func (v sizeBetweenValidator) Description(ctx context.Context) string { | ||
return fmt.Sprintf("list must contain at least %d elements and at most %d elements", v.min, v.max) | ||
} | ||
|
||
// MarkdownDescription describes the validation in Markdown formatting. | ||
func (v sizeBetweenValidator) MarkdownDescription(ctx context.Context) string { | ||
return v.Description(ctx) | ||
} | ||
|
||
// Validate performs the validation. | ||
func (v sizeBetweenValidator) Validate(ctx context.Context, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) { | ||
elems, ok := validateList(ctx, req, resp) | ||
if !ok { | ||
return | ||
} | ||
|
||
if len(elems) < v.min || len(elems) > v.max { | ||
resp.Diagnostics.Append(validatordiag.AttributeValueDiagnostic( | ||
req.AttributePath, | ||
v.Description(ctx), | ||
fmt.Sprintf("%d", len(elems)), | ||
)) | ||
|
||
return | ||
} | ||
} | ||
|
||
// SizeBetween returns an AttributeValidator which ensures that any configured | ||
// attribute value: | ||
// | ||
// - Is a List. | ||
// - Contains at least min elements and at most max elements. | ||
// | ||
// Null (unconfigured) and unknown (known after apply) values are skipped. | ||
func SizeBetween(min, max int) tfsdk.AttributeValidator { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Go documentation 😉 |
||
return sizeBetweenValidator{ | ||
min: min, | ||
max: max, | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Go documentation 😉