Skip to content

Commit 8a1890c

Browse files
committed
Add MinLen validator
1 parent a502837 commit 8a1890c

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

ui/options.go

Lines changed: 7 additions & 1 deletion
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+
// WithValidateMinLen adds a custom validation function to a prompt that
147+
// checks the input string meets the minimum length requirement.
148+
func WithValidateMinLen(length int) Option {
149+
return WithValidateFunc(MinLen(length))
150+
}
151+
146152
// WithValidateYesNo adds a custom validation function to a prompt for a Yes/No
147153
// prompt.
148154
func WithValidateYesNo() Option {

ui/validators.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66
"net"
7+
"regexp"
78
"strings"
89

910
"github.com/manifoldco/promptui"
@@ -73,3 +74,19 @@ func YesNo() promptui.ValidateFunc {
7374
}
7475
}
7576
}
77+
78+
// MinLen 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 MinLen(length int) promptui.ValidateFunc {
81+
return func(s string) error {
82+
if length <= 0 {
83+
return nil
84+
}
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)
89+
}
90+
return nil
91+
}
92+
}

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 TestMinLen(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-whitespace-characters",
133+
length: 15,
134+
input: " p \t\n#$%@#$ a s s ",
135+
wantErr: true,
136+
},
137+
{
138+
name: "ignore-whitespace-characters-ok",
139+
length: 10,
140+
input: " p \t\n#$%@#$ a s s ",
141+
wantErr: false,
142+
},
143+
}
144+
for _, tt := range tests {
145+
t.Run(tt.name, func(t *testing.T) {
146+
gotErr := MinLen(tt.length)(tt.input) != nil
147+
if gotErr != tt.wantErr {
148+
t.Errorf("MinLen(%v)(%s) = %v, want %v", tt.length, tt.input, gotErr, tt.wantErr)
149+
}
150+
})
151+
}
152+
}

0 commit comments

Comments
 (0)