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

List Size Validation (SizeAtLeast, SizeAtMost, SizeBetween) #4

Closed
1 task done
bflad opened this issue May 16, 2022 · 1 comment · Fixed by #41
Closed
1 task done

List Size Validation (SizeAtLeast, SizeAtMost, SizeBetween) #4

bflad opened this issue May 16, 2022 · 1 comment · Fixed by #41
Assignees
Labels
enhancement New feature or request type/list types.List validators
Milestone

Comments

@bflad
Copy link
Contributor

bflad commented May 16, 2022

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.List 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 listvalidator 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.List{
    ElemType: types.String,
  },
  Validators: tfsdk.AttributeValidators{
    listvalidator.SizeAtLeast(123),
  },
},

Additional Information

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@bflad bflad added enhancement New feature or request type/list types.List validators labels May 16, 2022
@bendbennett bendbennett self-assigned this Jun 20, 2022
@bflad bflad added this to the v0.3.0 milestone Jun 22, 2022
bendbennett added a commit that referenced this issue Jun 22, 2022
bendbennett added a commit that referenced this issue Jun 22, 2022
bendbennett added a commit that referenced this issue Jun 23, 2022
bendbennett added a commit that referenced this issue Jun 23, 2022
* Adding List validation for SizeAtLeast, SizeAtMost and SizeBetween (#4)

* Update following code review (#4)

* Rename .changelog file to match PR number (#4)
@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request type/list types.List validators
Projects
None yet
2 participants