Skip to content

String Enumeration Validation (OneOf / NoneOf) #9

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.String 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 stringvalidator package, create two new unexported types that satisfy the tfsdk.AttributeValidator interface:

var _ oneOfValidator = tfsdk.AttributeValidator

type oneOfValidator struct {
  values []string
}

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 []string
}

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 string...) AttributeValidator {/*...*/}
func NoneOf(values string...) AttributeValidator {/*...*/}

This would allow provider developers to declare attributes such as:

tfsdk.Attribute{
  // ... other fields ...
  Type: types.String,
  Validators: tfsdk.AttributeValidators{
    stringvalidator.OneOf("one", "two", "three"), // or OneOf([]string{"one", "two", "three"}...)
  },
},

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/stringtypes.String validators

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions