|
| 1 | +package stringvalidator |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework-validators/validatordiag" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/tfsdk" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 10 | +) |
| 11 | + |
| 12 | +var _ tfsdk.AttributeValidator = lengthAtMostValidator{} |
| 13 | + |
| 14 | +// lengthAtMostValidator validates that a string Attribute's length is at most a certain value. |
| 15 | +type lengthAtMostValidator struct { |
| 16 | + maxLength int |
| 17 | +} |
| 18 | + |
| 19 | +// Description describes the validation in plain text formatting. |
| 20 | +func (validator lengthAtMostValidator) Description(_ context.Context) string { |
| 21 | + return fmt.Sprintf("string length must be at most %d", validator.maxLength) |
| 22 | +} |
| 23 | + |
| 24 | +// MarkdownDescription describes the validation in Markdown formatting. |
| 25 | +func (validator lengthAtMostValidator) MarkdownDescription(ctx context.Context) string { |
| 26 | + return validator.Description(ctx) |
| 27 | +} |
| 28 | + |
| 29 | +// Validate performs the validation. |
| 30 | +func (validator lengthAtMostValidator) Validate(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) { |
| 31 | + s, ok := validateString(ctx, request, response) |
| 32 | + |
| 33 | + if !ok { |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + if l := len(s); l > validator.maxLength { |
| 38 | + response.Diagnostics.Append(validatordiag.AttributeValueLengthDiagnostic( |
| 39 | + request.AttributePath, |
| 40 | + validator.Description(ctx), |
| 41 | + fmt.Sprintf("%d", l), |
| 42 | + )) |
| 43 | + |
| 44 | + return |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// LengthAtMost returns an AttributeValidator which ensures that any configured |
| 49 | +// attribute value: |
| 50 | +// |
| 51 | +// - Is a string. |
| 52 | +// - Is of length exclusively less than the given maximum. |
| 53 | +// |
| 54 | +// Null (unconfigured) and unknown (known after apply) values are skipped. |
| 55 | +func LengthAtMost(maxLength int) tfsdk.AttributeValidator { |
| 56 | + if maxLength < 0 { |
| 57 | + return nil |
| 58 | + } |
| 59 | + |
| 60 | + return lengthAtMostValidator{ |
| 61 | + maxLength: maxLength, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// validateString ensures that the request contains a String value. |
| 66 | +func validateString(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (string, bool) { |
| 67 | + var s types.String |
| 68 | + |
| 69 | + diags := tfsdk.ValueAs(ctx, request.AttributeConfig, &s) |
| 70 | + |
| 71 | + if diags.HasError() { |
| 72 | + response.Diagnostics = append(response.Diagnostics, diags...) |
| 73 | + |
| 74 | + return "", false |
| 75 | + } |
| 76 | + |
| 77 | + if s.Unknown || s.Null { |
| 78 | + return "", false |
| 79 | + } |
| 80 | + |
| 81 | + return s.Value, true |
| 82 | +} |
0 commit comments