Skip to content

Float64 Enumeration Validation (OneOf / NoneOf) #7

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.Float64 values are part of (or not part of) a defined set of values. For example:

  • Whether a known value is contained in a defined set of values
  • Whether a known value is not contained in a defined set of values

Proposal

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

var _ oneOfValidator = tfsdk.AttributeValidator

type oneOfValidator struct {
  values []float64
}

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

var _ noneOfValidator = tfsdk.AttributeValidator

type noneOfValidator struct {
  values []float64
}

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

Then, create exported functions that return these:

func OneOf(values float64...) AttributeValidator {/*...*/}
func NoneOf(values float64...) AttributeValidator {/*...*/}

This would allow provider developers to declare attributes such as:

tfsdk.Attribute{
  // ... other fields ...
  Type: types.Float64,
  Validators: tfsdk.AttributeValidators{
    float64validator.OneOf(1.2, 2.4, 4.8), // or OneOf([]float64{1.2, 2.4, 4.8}...)
  },
},

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

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions