-
Notifications
You must be signed in to change notification settings - Fork 1
/
string.go
146 lines (128 loc) · 2.73 KB
/
string.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package hvalid
import (
"errors"
"net/url"
"regexp"
"strconv"
"strings"
)
func ContainsStr(substr string, errMsg ...string) ValidatorFunc[string] {
return ValidatorFunc[string](func(field string) error {
ok := strings.Contains(field, substr)
if !ok {
if len(errMsg) > 0 {
return errors.New(errMsg[0])
}
return errors.New("not contain the sub string")
}
return nil
})
}
func IsIPv4(errMsg ...string) ValidatorFunc[string] {
return ValidatorFunc[string](func(field string) error {
if checkIPv4(field) {
return nil
}
if len(errMsg) > 0 {
return errors.New(errMsg[0])
}
return errors.New("the value not is ipv4 address")
})
}
func IsIPv6(errMsg ...string) ValidatorFunc[string] {
return ValidatorFunc[string](func(field string) error {
if checkIPv6(field) {
return nil
}
if len(errMsg) > 0 {
return errors.New(errMsg[0])
}
return errors.New("the value not is ipv6 address")
})
}
func IsUrl(errMsg ...string) ValidatorFunc[string] {
return ValidatorFunc[string](func(field string) error {
err := errors.New("the value not is url")
if len(errMsg) > 0 {
err = errors.New(errMsg[0])
}
_, parseErr := url.ParseRequestURI(field)
if parseErr != nil {
return err
}
u, parseErr := url.Parse(field)
if parseErr != nil {
return err
}
if u.Scheme == "" && u.Host == "" {
return err
}
return nil
})
}
func IsEmail(errMsg ...string) ValidatorFunc[string] {
return ValidatorFunc[string](func(field string) error {
return Regexp(`^([\w\.\_\-]{2,10})@(\w{1,}).([a-z]{2,4})$`, errMsg...)(field)
})
}
func Regexp(pattern string, errMsg ...string) ValidatorFunc[string] {
return ValidatorFunc[string](func(field string) error {
result, _ := regexp.MatchString(pattern, field)
if !result {
if len(errMsg) > 0 {
return errors.New(errMsg[0])
}
return errors.New("not an email address")
}
return nil
})
}
func checkIPv4(IP string) bool {
// 字符串这样切割
strs := strings.Split(IP, ".")
if len(strs) != 4 {
return false
}
for _, s := range strs {
if len(s) == 0 || (len(s) > 1 && s[0] == '0') {
return false
}
// 直接访问字符串的值
if s[0] < '0' || s[0] > '9' {
return false
}
// 字符串转数字
n, err := strconv.Atoi(s)
if err != nil {
return false
}
if n < 0 || n > 255 {
return false
}
}
return true
}
func checkIPv6(IP string) bool {
strs := strings.Split(IP, ":")
if len(strs) != 8 {
return false
}
for _, s := range strs {
if len(s) <= 0 || len(s) > 4 {
return false
}
for i := 0; i < len(s); i++ {
if s[i] >= '0' && s[i] <= '9' {
continue
}
if s[i] >= 'A' && s[i] <= 'F' {
continue
}
if s[i] >= 'a' && s[i] <= 'f' {
continue
}
return false
}
}
return true
}