Skip to content

Commit 00c2096

Browse files
committed
Recommended changes from PR review
- MinLen -> MinLength - regex -> strings.TrimSpace
1 parent b7cb765 commit 00c2096

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

ui/options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ func WithValidateNotEmpty() Option {
145145

146146
// WithValidateMinLen adds a custom validation function to a prompt that
147147
// checks the input string meets the minimum length requirement.
148-
func WithValidateMinLen(length int) Option {
149-
return WithValidateFunc(MinLen(length))
148+
func WithValidateMinLength(minLength int) Option {
149+
return WithValidateFunc(MinLength(minLength))
150150
}
151151

152152
// WithValidateYesNo adds a custom validation function to a prompt for a Yes/No

ui/validators.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"fmt"
66
"net"
7-
"regexp"
87
"strings"
98

109
"github.com/manifoldco/promptui"
@@ -75,17 +74,15 @@ func YesNo() promptui.ValidateFunc {
7574
}
7675
}
7776

78-
// MinLen is a validation function that checks for a minimum length.
77+
// MinLength is a validation function that checks for a minimum length.
7978
// An input length <= 0 indicates that the check should not be performed.
80-
func MinLen(length int) promptui.ValidateFunc {
79+
func MinLength(minLength int) promptui.ValidateFunc {
8180
return func(s string) error {
82-
if length <= 0 {
81+
if minLength <= 0 {
8382
return nil
8483
}
85-
re := regexp.MustCompile(`\s+`)
86-
next := re.ReplaceAllString(s, "")
87-
if len(next) < length {
88-
return fmt.Errorf("input does not meet minimum length requirement; must be at least %v characters", length)
84+
if len(strings.TrimSpace(s)) < minLength {
85+
return fmt.Errorf("input does not meet minimum length requirement; must be at least %v characters", minLength)
8986
}
9087
return nil
9188
}

ui/validators_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestDNS(t *testing.T) {
9191
}
9292
}
9393

94-
func TestMinLen(t *testing.T) {
94+
func TestMinLength(t *testing.T) {
9595
tests := []struct {
9696
name string
9797
length int
@@ -129,21 +129,21 @@ func TestMinLen(t *testing.T) {
129129
wantErr: true,
130130
},
131131
{
132-
name: "ignore-whitespace-characters",
133-
length: 15,
134-
input: " p \t\n#$%@#$ a s s ",
132+
name: "ignore-pre-post-whitespace-characters",
133+
length: 5,
134+
input: " pass ",
135135
wantErr: true,
136136
},
137137
{
138138
name: "ignore-whitespace-characters-ok",
139-
length: 10,
140-
input: " p \t\n#$%@#$ a s s ",
139+
length: 4,
140+
input: " pass ",
141141
wantErr: false,
142142
},
143143
}
144144
for _, tt := range tests {
145145
t.Run(tt.name, func(t *testing.T) {
146-
gotErr := MinLen(tt.length)(tt.input) != nil
146+
gotErr := MinLength(tt.length)(tt.input) != nil
147147
if gotErr != tt.wantErr {
148148
t.Errorf("MinLen(%v)(%s) = %v, want %v", tt.length, tt.input, gotErr, tt.wantErr)
149149
}

0 commit comments

Comments
 (0)