Skip to content

Int64 Amount Validation (AtLeast / AtMost / Between) #2

Closed
@bflad

Description

@bflad

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.Int64 values for their amount/size. For example:

  • Whether a known value is equal or more than a certain amount, but not constrained
  • Whether a known value is equal or less than a certain amount, but not constrained
  • Whether a value is equal or more than a certain amount and equal or less than another amount

Proposal

Inside a int64validator package, create three new unexported types that satisfy the tfsdk.AttributeValidator interface:

var _ atLeastValidator = tfsdk.AttributeValidator

type atLeastValidator struct {
  min int64
}

func (v atLeastValidator) Description(ctx context.Context) string {/*...*/}
func (v atLeastValidator) MarkdownDescription(ctx context.Context) string {/*...*/}
func (v atLeastValidator) Validate(ctx context.Context, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) {/*...*/}

var _ atMostValidator = tfsdk.AttributeValidator

type atMostValidator struct {
  max int64
}

func (v atMostValidator) Description(ctx context.Context) string {/*...*/}
func (v atMostValidator) MarkdownDescription(ctx context.Context) string {/*...*/}
func (v atMostValidator) Validate(ctx context.Context, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) {/*...*/}

var _ betweenValidator = tfsdk.AttributeValidator

type betweenValidator struct {
  max int64
  min int64
}

func (v betweenValidator) Description(ctx context.Context) string {/*...*/}
func (v betweenValidator) MarkdownDescription(ctx context.Context) string {/*...*/}
func (v betweenValidator) Validate(ctx context.Context, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) {/*...*/}

Then, create exported functions that return these:

func AtLeast(min int64) AttributeValidator {/*...*/}
func AtMost(max int64) AttributeValidator {/*...*/}
func Between(min int64, max int64) AttributeValidator {/*...*/}

This would allow provider developers to declare attributes such as:

tfsdk.Attribute{
  // ... other fields ...
  Type: types.Int64,
  Validators: tfsdk.AttributeValidators{
    int64validator.AtLeast(123),
  },
},

Additional Information

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesttype/int64types.Int64 validators

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions