Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ func (a *alertGroupResource) Schema(_ context.Context, _ resource.SchemaRequest,
mapvalidator.KeysAre(stringvalidator.LengthAtMost(200)),
mapvalidator.ValueStringsAre(stringvalidator.LengthAtMost(200)),
mapvalidator.SizeAtMost(5),
mapvalidator.ValueStringsAre(validate.NoLeadingOrTrailingWhitespace()),
},
},
"record": schema.StringAttribute{
Expand Down
27 changes: 27 additions & 0 deletions stackit/internal/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"
"time"
_ "time/tzdata"
"unicode"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
Expand Down Expand Up @@ -394,3 +395,29 @@ func IsLowercased() *Validator {
},
}
}

// NoLeadingOrTrailingWhitespace returns a Validator that checks if the input string has leading or trailing whitespace.
// Examples:
// - "example": valid
// - "": valid, empty value
// - " example": invalid, leading whitespace
// - "example ": invalid, trailing whitespace
func NoLeadingOrTrailingWhitespace() *Validator {
description := "Value must not have leading or trailing whitespace, as defined by gos unicode.IsSpace(rune)."
return &Validator{
description: description,
validate: func(_ context.Context, request validator.StringRequest, response *validator.StringResponse) {
val := request.ConfigValue.ValueString()
if val == "" {
return
}
if unicode.IsSpace(rune(val[0])) || unicode.IsSpace(rune(val[len(val)-1])) {
response.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
request.Path,
description,
val,
))
}
},
}
}
57 changes: 57 additions & 0 deletions stackit/internal/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,3 +1008,60 @@ func TestIsLowercased(t *testing.T) {
})
}
}

func TestNoLeadingOrtTrailingWhitespace(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
wantErr bool
}{
{
name: "empty",
input: "",
},
{
name: "valid",
input: "valid",
},
{
name: "single char, valid",
input: "a",
},
{
name: "leading whitespace",
input: " leading",
wantErr: true,
},
{
name: "trailing whitespace",
input: "trailing ",
wantErr: true,
},
{
name: "leading and trailing whitespace",
input: " leading and trailing ",
wantErr: true,
},
{
name: "single space",
input: " ",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
r := validator.StringResponse{}
NoLeadingOrTrailingWhitespace().ValidateString(context.Background(), validator.StringRequest{
ConfigValue: types.StringValue(tt.input),
}, &r)
if tt.wantErr && !r.Diagnostics.HasError() {
t.Fatalf("Expected validation to fail for input: %q", tt.input)
}
if !tt.wantErr && r.Diagnostics.HasError() {
t.Fatalf("Expected validation to succeed for input: %q, but got errors: %v", tt.input, r.Diagnostics.Errors())
}
})
}
}
Loading