forked from lgrees/resy-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
surveyHelpers.go
115 lines (90 loc) · 1.96 KB
/
surveyHelpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package surveyHelpers
import (
"errors"
"regexp"
"strings"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/fanniva/resy-cli/internal/utils/date"
)
func CreateRegexValidator(s, e string) survey.Validator {
return func(val interface{}) error {
str, ok := val.(string)
if !ok {
return errors.New("input must be a string")
}
match, _ := regexp.MatchString(s, str)
if !match {
return errors.New(e)
}
return nil
}
}
func DateValidator(val interface{}) error {
str, ok := val.(string)
if !ok {
return errors.New("input must be a string")
}
t, err := date.ParseDate(str)
if err != nil {
return errors.New("input must be a valid date (YYYY-MM-DD)")
}
if !t.After(time.Now().Local()) {
return errors.New("the date selected should be in the future")
}
return nil
}
func TimeValidatorL(val interface{}) error {
str, ok := val.(string)
if !ok {
return errors.New("input must be a string")
}
_, err := date.ParseTimeL(str)
if err != nil {
return errors.New("input must be a valid time (HH:MM:SS)")
}
return nil
}
func TimeValidatorS(val interface{}) error {
str, ok := val.(string)
if !ok {
return errors.New("input must be a string")
}
_, err := date.ParseTimeS(str)
if err != nil {
return errors.New("input must be a valid time (HH:MM)")
}
return nil
}
func TimesValidator(val interface{}) error {
in, ok := val.(string)
if !ok {
return errors.New("input must be a string")
}
arr := strings.Split(in, "\n")
for _, v := range arr {
err := TimeValidatorS(v)
if err != nil {
return err
}
}
return nil
}
func VenueValidator(val interface{}) error {
str, ok := val.(string)
if !ok {
return errors.New("input must be a string")
}
arr := strings.Split(str, " | ")
if len(arr) < 5 {
return errors.New("please tab to search and select a venue")
}
return nil
}
func TransformLowerCase(in interface{}) (out interface{}) {
str, ok := in.(string)
if !ok {
return ""
}
return strings.ToLower(str)
}