Skip to content

add validator: StringRuneCountBetween() and StringBytesBetween() instead StringLenBetween() #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
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
11 changes: 11 additions & 0 deletions .changelog/985.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:note
helper/validation: The `StringLenBetween()` function is being deprecated in favor of the `StringBytesBetween()` function
```

```release-note:feature
helper/validation: Added `StringRuneCountBetween()` function for validate string with `number of characters`
```

```release-note:enhancement
helper/validation: Added validation for parameters at `StringLenBetween()` function
```
52 changes: 51 additions & 1 deletion helper/validation/strings.go
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import (
"fmt"
"regexp"
"strings"
"unicode/utf8"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
@@ -66,10 +67,30 @@ func StringIsWhiteSpace(i interface{}, k string) ([]string, []error) {
return nil, nil
}

// Deprecated: Use StringBytesBetween() instead.
// **Recommend StringRuneCountBetween()** in order to count 'String length' correctly
// StringLenBetween returns a SchemaValidateFunc which tests if the provided value
// is of type string and has length between min and max (inclusive)
// is of type string and has 'Byte' length between min and max (inclusive)
func StringLenBetween(min, max int) schema.SchemaValidateFunc {
return StringBytesBetween(min, max)
}

// StringBytesBetween returns a SchemaValidateFunc which tests if the provided value
// is of type string and has 'Byte' length between min and max (inclusive)
// **Recommend StringRuneCountBetween()** in order to count 'String length' correctly
func StringBytesBetween(min, max int) schema.SchemaValidateFunc {
return func(i interface{}, k string) (warnings []string, errors []error) {

if min < 0 {
errors = append(errors, fmt.Errorf("min must be zero or natural number (actual: %d)", min))
}
if max < 0 {
errors = append(errors, fmt.Errorf("max must be zero or natural number (actual: %d)", max))
}
if min > max {
errors = append(errors, fmt.Errorf("min must be less than or equal to max (actual: min=%d, max=%d)", min, max))
}

v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
@@ -84,6 +105,35 @@ func StringLenBetween(min, max int) schema.SchemaValidateFunc {
}
}

// StringRuneCountBetween returns a SchemaValidateFunc which tests if the provided value
// is of type string and has 'Rune' length between min and max (inclusive)
func StringRuneCountBetween(min, max int) schema.SchemaValidateFunc {
return func(i interface{}, k string) (warnings []string, errors []error) {

if min < 0 {
errors = append(errors, fmt.Errorf("min must be zero or natural number (actual: %d)", min))
}
if max < 0 {
errors = append(errors, fmt.Errorf("max must be zero or natural number (actual: %d)", max))
}
if min > max {
errors = append(errors, fmt.Errorf("min must be less than or equal to max (actual: min=%d, max=%d)", min, max))
}

v, ok := i.(string)
if !ok {
errors = append(errors, fmt.Errorf("expected type of %s to be string", k))
return warnings, errors
}

if c := utf8.RuneCountInString(v); c < min || c > max {
errors = append(errors, fmt.Errorf("expected length of %s to be in the range (%d - %d), got %s", k, min, max, v))
}

return warnings, errors
}
}

// StringMatch returns a SchemaValidateFunc which tests if the provided value
// matches a given regexp. Optionally an error message can be provided to
// return something friendlier than "must match some globby regexp".
Loading