Closed
Description
Terraform CLI and Framework Versions
Any Terraform CLI version; terraform-plugin-framework v0.8.0
Use Cases or Problem Statement
Provider developers should be able to generically validate types.Map
values for their element count (size). For example:
- Whether a known value is equal or more than a certain number of elements, but not constrained
- Whether a known value is equal or less than a certain number of elements, but not constrained
- Whether a value is equal or more than a certain number of elements and equal or less than another number of elements
Proposal
Inside a mapvalidator
package, create three new unexported types that satisfy the tfsdk.AttributeValidator
interface:
var _ sizeAtLeastValidator = tfsdk.AttributeValidator
type sizeAtLeastValidator struct {
min int
}
func (v sizeAtLeastValidator) Description(ctx context.Context) string {/*...*/}
func (v sizeAtLeastValidator) MarkdownDescription(ctx context.Context) string {/*...*/}
func (v sizeAtLeastValidator) Validate(ctx context.Context, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) {/*...*/}
var _ sizeAtMostValidator = tfsdk.AttributeValidator
type sizeAtMostValidator struct {
max int
}
func (v sizeAtMostValidator) Description(ctx context.Context) string {/*...*/}
func (v sizeAtMostValidator) MarkdownDescription(ctx context.Context) string {/*...*/}
func (v sizeAtMostValidator) Validate(ctx context.Context, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) {/*...*/}
var _ sizeBetweenValidator = tfsdk.AttributeValidator
type sizeBetweenValidator struct {
max int
min int
}
func (v sizeBetweenValidator) Description(ctx context.Context) string {/*...*/}
func (v sizeBetweenValidator) MarkdownDescription(ctx context.Context) string {/*...*/}
func (v sizeBetweenValidator) Validate(ctx context.Context, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) {/*...*/}
Then, create exported functions that return these:
func SizeAtLeast(min int) AttributeValidator {/*...*/}
func SizeAtMost(max int) AttributeValidator {/*...*/}
func SizeBetween(min int, max int) AttributeValidator {/*...*/}
This would allow provider developers to declare attributes such as:
tfsdk.Attribute{
// ... other fields ...
Type: types.Map{
ElemType: types.String,
},
Validators: tfsdk.AttributeValidators{
mapvalidator.SizeAtLeast(123),
},
},
Additional Information
No response
Code of Conduct
- I agree to follow this project's Code of Conduct