Skip to content

Commit 8e0c86c

Browse files
Minor fix to stringvalidator.LengthBetween() (#157)
* 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]>
1 parent 9ddcff9 commit 8e0c86c

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

stringvalidator/length_between.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ func (v lengthBetweenValidator) ValidateString(ctx context.Context, request vali
4747
}
4848
}
4949

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

stringvalidator/length_between_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,26 @@ func TestLengthBetweenValidator(t *testing.T) {
3939
minLength: 1,
4040
maxLength: 3,
4141
},
42+
"valid minimum": {
43+
val: types.StringValue("ok"),
44+
minLength: 2,
45+
maxLength: 3,
46+
},
47+
"valid maximum": {
48+
val: types.StringValue("ok"),
49+
minLength: 1,
50+
maxLength: 2,
51+
},
52+
"valid minimum maximum equal": {
53+
val: types.StringValue("ok"),
54+
minLength: 2,
55+
maxLength: 2,
56+
},
57+
"valid minimum maximum zero": {
58+
val: types.StringValue(""),
59+
minLength: 0,
60+
maxLength: 0,
61+
},
4262
"too long": {
4363
val: types.StringValue("not ok"),
4464
minLength: 1,

0 commit comments

Comments
 (0)