Skip to content

Commit 3977fe0

Browse files
authored
Add MinLength validator and option (#164)
1 parent 075e5ba commit 3977fe0

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

ui/options.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,17 @@ func WithValidateFunc(fn func(string) error) Option {
138138
}
139139

140140
// WithValidateNotEmpty adds a custom validation function to a prompt that
141-
// checks that the propted string is not empty.
141+
// checks that the prompted string is not empty.
142142
func WithValidateNotEmpty() Option {
143143
return WithValidateFunc(NotEmpty())
144144
}
145145

146+
// WithValidateMinLength adds a custom validation function to a prompt that
147+
// checks the input string meets the minimum length requirement.
148+
func WithValidateMinLength(minLength int) Option {
149+
return WithValidateFunc(MinLength(minLength))
150+
}
151+
146152
// WithValidateYesNo adds a custom validation function to a prompt for a Yes/No
147153
// prompt.
148154
func WithValidateYesNo() Option {
@@ -167,6 +173,6 @@ func WithValidateRegexp(re string) Option {
167173
if rx.MatchString(s) {
168174
return nil
169175
}
170-
return fmt.Errorf("%s does not match the regular expresion %s", s, re)
176+
return fmt.Errorf("%s does not match the regular expression %s", s, re)
171177
})
172178
}

ui/validators.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net"
77
"strings"
8+
"unicode"
89

910
"github.com/manifoldco/promptui"
1011
)
@@ -73,3 +74,17 @@ func YesNo() promptui.ValidateFunc {
7374
}
7475
}
7576
}
77+
78+
// MinLength is a validation function that checks for a minimum length.
79+
// An input length <= 0 indicates that the check should not be performed.
80+
func MinLength(minLength int) promptui.ValidateFunc {
81+
return func(s string) error {
82+
if minLength <= 0 {
83+
return nil
84+
}
85+
if len(strings.TrimRightFunc(s, unicode.IsSpace)) < minLength {
86+
return fmt.Errorf("input does not meet minimum length requirement; must be at least %v characters", minLength)
87+
}
88+
return nil
89+
}
90+
}

ui/validators_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,63 @@ func TestDNS(t *testing.T) {
9090
})
9191
}
9292
}
93+
94+
func TestMinLength(t *testing.T) {
95+
tests := []struct {
96+
name string
97+
length int
98+
input string
99+
wantErr bool
100+
}{
101+
{
102+
name: "negative",
103+
length: -5,
104+
input: "foobar",
105+
wantErr: false,
106+
},
107+
{
108+
name: "zero",
109+
length: 0,
110+
input: "localhost",
111+
wantErr: false,
112+
},
113+
{
114+
name: "greater-than-min-length",
115+
length: 5,
116+
input: "foobar",
117+
wantErr: false,
118+
},
119+
{
120+
name: "equal-min-length",
121+
length: 6,
122+
input: "foobar",
123+
wantErr: false,
124+
},
125+
{
126+
name: "less-than-min-length",
127+
length: 8,
128+
input: "foobar",
129+
wantErr: true,
130+
},
131+
{
132+
name: "ignore-post-whitespace-characters",
133+
length: 7,
134+
input: " pass ",
135+
wantErr: true,
136+
},
137+
{
138+
name: "ignore-post-whitespace-characters-ok",
139+
length: 6,
140+
input: " pass ",
141+
wantErr: false,
142+
},
143+
}
144+
for _, tt := range tests {
145+
t.Run(tt.name, func(t *testing.T) {
146+
gotErr := MinLength(tt.length)(tt.input) != nil
147+
if gotErr != tt.wantErr {
148+
t.Errorf("MinLength(%v)(%s) = %v, want %v", tt.length, tt.input, gotErr, tt.wantErr)
149+
}
150+
})
151+
}
152+
}

0 commit comments

Comments
 (0)