-
Notifications
You must be signed in to change notification settings - Fork 95
/
string.go
44 lines (34 loc) · 1.35 KB
/
string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package validators
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
)
var _ validator.String = notEmptyStringValidator{}
// notEmptyValidator validates that a string Attribute's length is at least a certain value.
type notEmptyStringValidator struct{}
// Description describes the validation in plain text formatting.
func (v notEmptyStringValidator) Description(_ context.Context) string {
return "Attribute cannot be empty"
}
// MarkdownDescription describes the validation in Markdown formatting.
func (v notEmptyStringValidator) MarkdownDescription(ctx context.Context) string {
return v.Description(ctx)
}
// ValidateString performs the validation.
func (v notEmptyStringValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
if request.ConfigValue.IsNull() || request.ConfigValue.IsUnknown() {
return
}
value := request.ConfigValue.ValueString()
if value != "" {
return
}
attr, _ := request.PathExpression.Steps().LastStep()
response.Diagnostics.AddAttributeError(request.Path, v.Description(ctx), fmt.Sprintf("Attribute %s cannot be empty", attr))
}
// NotEmptyString returns an validator which ensures that any configured
// attribute value is not an empty string.
func NotEmptyString() validator.String {
return notEmptyStringValidator{}
}