Skip to content

Float64 Amount Validation (AtLeast / AtMost / Between) #1

@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.Float64 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 float64validator package, create three new unexported types that satisfy the tfsdk.AttributeValidator interface:

var _ atLeastValidator = tfsdk.AttributeValidator

type atLeastValidator struct {
  min float64
}

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 float64
}

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 float64
  min float64
}

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 float64) AttributeValidator {/*...*/}
func AtMost(max float64) AttributeValidator {/*...*/}
func Between(min float64, max float64) AttributeValidator {/*...*/}

This would allow provider developers to declare attributes such as:

tfsdk.Attribute{
  // ... other fields ...
  Type: types.Float64,
  Validators: tfsdk.AttributeValidators{
    float64validator.AtLeast(1.2),
  },
},

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/float64types.Float64 validators

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions