Closed
Description
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 for their length. For example:
- Whether a known value is equal or more than a certain length, but not constrained
- Whether a known value is equal or less than a certain length, but not constrained
- Whether a value is equal or more than a certain length and equal or less than another length
Proposal
Inside a stringvalidator
package, create three new unexported types that satisfy the tfsdk.AttributeValidator
interface:
var _ lengthAtLeastValidator = tfsdk.AttributeValidator
type lengthAtLeastValidator struct {
min int
}
func (v lengthAtLeastValidator) Description(ctx context.Context) string {/*...*/}
func (v lengthAtLeastValidator) MarkdownDescription(ctx context.Context) string {/*...*/}
func (v lengthAtLeastValidator) Validate(ctx context.Context, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) {/*...*/}
var _ lengthAtMostValidator = tfsdk.AttributeValidator
type lengthAtMostValidator struct {
max int
}
func (v lengthAtMostValidator) Description(ctx context.Context) string {/*...*/}
func (v lengthAtMostValidator) MarkdownDescription(ctx context.Context) string {/*...*/}
func (v lengthAtMostValidator) Validate(ctx context.Context, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) {/*...*/}
var _ lengthBetweenValidator = tfsdk.AttributeValidator
type lengthBetweenValidator struct {
max int
min int
}
func (v lengthBetweenValidator) Description(ctx context.Context) string {/*...*/}
func (v lengthBetweenValidator) MarkdownDescription(ctx context.Context) string {/*...*/}
func (v lengthBetweenValidator) Validate(ctx context.Context, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) {/*...*/}
Then, create exported functions that return these:
func LengthAtLeast(min int) AttributeValidator {/*...*/}
func LengthAtMost(max int) AttributeValidator {/*...*/}
func LengthBetween(min int, max int) AttributeValidator {/*...*/}
This would allow provider developers to declare attributes such as:
tfsdk.Attribute{
// ... other fields ...
Type: types.String,
Validators: tfsdk.AttributeValidators{
stringvalidator.AtLeast(123),
},
},
Additional Information
No response
Code of Conduct
- I agree to follow this project's Code of Conduct