Skip to content

Int64 Amount Validation (AtLeastSumOf, AtMostSumOf, EqualToSumOf) #20

@bendbennett

Description

@bendbennett

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 the sum of one or more attributes, but not constrained
  • Whether a known value is equal or less than the sum of one or more attributes, but not constrained
  • Whether a known value is exactly equal to the sum of one or more attributes, but not constrained

Proposal

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

var _ atLeastSumOfValidator = tfsdk.AttributeValidator

type atLeastSumOfValidator struct {
	attributesToSum []*tftypes.AttributePath
}

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

Then, create exported functions that return these:

func AtLeastSumOf(attributesToSum []*tftypes.AttributePath) AttributeValidator {/*...*/}

This would allow provider developers to declare attributes such as:

tfsdk.Attribute{
  // ... other fields ...
  Type: types.Int64,
  Validators: tfsdk.AttributeValidators{
    int64validator.AtLeastSumOf(
	tftypes.NewAttributePath().WithAttributeName("attrib_one"), 
	tftypes.NewAttributePath().WithAttributeName("attrib_two")),
  },
},

Additional Information

No response

Code of Conduct

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

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requesttype/int64types.Int64 validatorstype/multiMultiple attribute validators

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions