Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changes/unreleased/BUG FIXES-20250903-155819.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: ' tfsdk: Fixed `StringLenBetween` function to correctly count multibyte characters'
time: 2025-09-03T15:58:19.587736995Z
custom:
Issue: "1516"
7 changes: 5 additions & 2 deletions helper/validation/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"fmt"
"regexp"
"strings"
"unicode/utf8"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"golang.org/x/text/unicode/norm"
)

// StringIsNotEmpty is a ValidateFunc that ensures a string is not empty
Expand Down Expand Up @@ -78,8 +80,9 @@ func StringLenBetween(minVal, maxVal int) schema.SchemaValidateFunc {
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
return warnings, errors
}

if len(v) < minVal || len(v) > maxVal {
v = norm.NFC.String(v)
l := utf8.RuneCountInString(v)
if l < minVal || l > maxVal {
errors = append(errors, fmt.Errorf("expected length of %s to be in the range (%d - %d), got %s", k, minVal, maxVal, v))
}

Expand Down
21 changes: 21 additions & 0 deletions helper/validation/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,27 @@ func TestValidationStringLenBetween(t *testing.T) {
f: StringLenBetween(1, 5),
expectedErr: regexp.MustCompile(`expected type of [\w]+ to be string`),
},
{
val: "日", // 3 bytes char, length must be 1
f: StringLenBetween(1, 1),
},
{
val: "😊", // 4 bytes char, length must be 1
f: StringLenBetween(1, 1),
},
{
val: "😊😭", // 4 bytes chars, length must be 2
f: StringLenBetween(1, 2),
},
{
val: "が", // 2 code points
f: StringLenBetween(1, 1),
},
{
val: "👨‍👩‍👧‍👦", // multi code points but can't be normalized
f: StringLenBetween(1, 1),
expectedErr: regexp.MustCompile(`expected length of [\w]+ to be in the range \(1 \- 1\), got 👨‍👩‍👧‍👦`),
},
})
}

Expand Down