Skip to content

Commit

Permalink
Minor fix to stringvalidator.LengthBetween() (#157)
Browse files Browse the repository at this point in the history
* Minor fix to `LengthBetween()`

The Go doc comment now more accurately reflects the validator behavior.

An unnecessary validation (max cannot be less than zero) was removed.

* add test cases for `stringvalidator.LengthBetween`'s inclusive behavior

* Update stringvalidator/length_between_test.go

Suggested change from @bendbennett

Co-authored-by: Benjamin Bennett <[email protected]>

* Update stringvalidator/length_between_test.go

Suggested change from @bendbennett

Co-authored-by: Benjamin Bennett <[email protected]>

---------

Co-authored-by: Benjamin Bennett <[email protected]>
  • Loading branch information
chrismarget and bendbennett authored Aug 21, 2023
1 parent 9ddcff9 commit 8e0c86c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 5 additions & 5 deletions stringvalidator/length_between.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ func (v lengthBetweenValidator) ValidateString(ctx context.Context, request vali
}
}

// LengthBetween returns an validator which ensures that any configured
// attribute value is of single-byte character length greater than the given
// minimum and less than the given maximum. Null (unconfigured) and unknown
// (known after apply) values are skipped.
// LengthBetween returns a validator which ensures that any configured
// attribute value is of single-byte character length greater than or equal
// to the given minimum and less than or equal to the given maximum. Null
// (unconfigured) and unknown (known after apply) values are skipped.
//
// Use UTF8LengthBetween for checking multiple-byte characters.
func LengthBetween(minLength, maxLength int) validator.String {
if minLength < 0 || maxLength < 0 || minLength > maxLength {
if minLength < 0 || minLength > maxLength {
return nil
}

Expand Down
20 changes: 20 additions & 0 deletions stringvalidator/length_between_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,26 @@ func TestLengthBetweenValidator(t *testing.T) {
minLength: 1,
maxLength: 3,
},
"valid minimum": {
val: types.StringValue("ok"),
minLength: 2,
maxLength: 3,
},
"valid maximum": {
val: types.StringValue("ok"),
minLength: 1,
maxLength: 2,
},
"valid minimum maximum equal": {
val: types.StringValue("ok"),
minLength: 2,
maxLength: 2,
},
"valid minimum maximum zero": {
val: types.StringValue(""),
minLength: 0,
maxLength: 0,
},
"too long": {
val: types.StringValue("not ok"),
minLength: 1,
Expand Down

0 comments on commit 8e0c86c

Please sign in to comment.