-
Notifications
You must be signed in to change notification settings - Fork 2
/
inputcheck.go
63 lines (57 loc) · 1.25 KB
/
inputcheck.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
package main
import (
"errors"
"regexp"
"strings"
"github.com/jaracil/ei"
)
const (
_prefixRegexp = "^\\S*$"
_taskRegexp = "^[^\\s@]\\S*$"
_userMinLen = 3
_userMaxLen = 500
_passwordMinLen = 4
_passwordMaxLen = 500
)
func checkRegexp(i ei.Ei, p ...interface{}) ei.Ei {
s, err := i.String()
if err != nil {
return ei.N(err)
}
if len(p) < 1 {
return ei.N(errors.New("regexp not provided"))
}
if match, err := regexp.MatchString(ei.N(p[0]).StringZ(), s); err != nil || !match {
return ei.N(errors.New("regexp check failed"))
}
return i
}
func checkLen(i ei.Ei, p ...interface{}) ei.Ei {
s, err := i.String()
if err != nil {
return ei.N(err)
}
if len(p) < 2 {
return ei.N(errors.New("minlen and maxlen not provided"))
}
if minlen := ei.N(p[0]).IntZ(); minlen > 0 && len(s) < minlen {
return ei.N(errors.New("minlen exceded"))
}
if maxlen := ei.N(p[1]).IntZ(); maxlen > 0 && len(s) > maxlen {
return ei.N(errors.New("maxlen exceded"))
}
return i
}
func checkNotEmptyLabels(i ei.Ei, p ...interface{}) ei.Ei {
s, err := i.String()
if err != nil {
return ei.N(err)
}
spl := strings.Split(s, ".")
for _, v := range spl {
if v == "" {
return ei.N(errors.New("must not contain empty labels"))
}
}
return i
}