Skip to content

stringvalidator: Support for multibyte strings #85

Closed
@sugimotofp

Description

@sugimotofp

Terraform CLI and Framework Versions

❯ terraform version
Terraform v1.2.3
on darwin_arm64
...

❯ go list -m github.com/hashicorp/terraform-plugin-framework
github.com/hashicorp/terraform-plugin-framework v0.17.0

Terraform Plugin Framework Code

[]validator.String{
  stringvalidator.LengthBetween(1, 5),
},

Terraform Configuration

resource "examplecloud_thing" "test" {
  display_name = "あいうえお"
}

Expected Behavior

No errors are returned.

Actual Behavior

│ Error: Invalid Attribute Value Length

│ with examplecloud_thing.test,
│ on ../../example/test.tf line 2, in resource "examplecloud_thing" "test":
│ 2: display_name = "あいうえお"

│ Attribute display_name string length must be between 1 and 5, got: 15

Steps to Reproduce

  1. terraform apply

Logs

No response

Additional Information

len(string) does not count by characters but by bytes, so if a multibyte string is specified for validation, it cannot be validated by characters.
In order to accurately determine the number of characters, utf8.RuneCountInString() should be used.
When developing services for Japan etc, It is convenient to handle the length of multibyte characters.

if l := len(value); l < v.minLength || l > v.maxLength {

ref: https://pkg.go.dev/unicode/utf8#RuneCountInString

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

Labels

documentationImprovements or additions to documentationenhancementNew feature or request

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions