-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
125 lines (108 loc) · 3.53 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
package vld
import (
"regexp"
)
var (
emailRegex = regexp.MustCompile("^(?:(?:(?:(?:[a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+(?:\\.([a-zA-Z]|\\d|[!#\\$%&'\\*\\+\\-\\/=\\?\\^_`{\\|}~]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])+)*)|(?:(?:\\x22)(?:(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(?:\\x20|\\x09)+)?(?:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x7f]|\\x21|[\\x23-\\x5b]|[\\x5d-\\x7e]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:\\(?:[\\x01-\\x09\\x0b\\x0c\\x0d-\\x7f]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}]))))*(?:(?:(?:\\x20|\\x09)*(?:\\x0d\\x0a))?(\\x20|\\x09)+)?(?:\\x22)))@(?:(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|\\d|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.)+(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])|(?:(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])(?:[a-zA-Z]|\\d|-|\\.|_|~|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])*(?:[a-zA-Z]|[\\x{00A0}-\\x{D7FF}\\x{F900}-\\x{FDCF}\\x{FDF0}-\\x{FFEF}])))\\.?$")
)
type stringVld struct {
value string
zero bool
err UnitError
}
func String(value string) *stringVld {
return &stringVld{
value: value,
zero: value == "",
}
}
func StrPtr(value *string) *stringVld {
if value == nil {
return &stringVld{
value: "",
zero: true,
}
}
return &stringVld{
value: *value,
zero: false,
}
}
func (s *stringVld) Required() *stringVld {
if s.err != nil || !s.zero {
return s
}
s.err = NewUnitError(ErrRequired, Args{})
return s
}
func (s *stringVld) GT(length int) *stringVld {
if s.err != nil || s.zero || len(s.value) > length {
return s
}
s.err = NewUnitError(ErrStringGT, Args{length})
return s
}
func (s *stringVld) GTE(length int) *stringVld {
if s.err != nil || s.zero || len(s.value) >= length {
return s
}
s.err = NewUnitError(ErrStringGTE, Args{length})
return s
}
func (s *stringVld) LT(length int) *stringVld {
if s.err != nil || s.zero || len(s.value) < length {
return s
}
s.err = NewUnitError(ErrStringLT, Args{length})
return s
}
func (s *stringVld) LTE(length int) *stringVld {
if s.err != nil || len(s.value) <= length {
return s
}
s.err = NewUnitError(ErrStringLTE, Args{length})
return s
}
func (s *stringVld) Length(min, max int) *stringVld {
if s.err != nil || s.zero || (len(s.value) >= min && len(s.value) <= max) {
return s
}
s.err = NewUnitError(ErrStringLength, Args{min, max})
return s
}
func (s *stringVld) Len(length int) *stringVld {
if s.err != nil || s.zero || len(s.value) == length {
return s
}
s.err = NewUnitError(ErrStringLen, Args{length})
return s
}
func (s *stringVld) Match(rg *regexp.Regexp) *stringVld {
if s.err != nil || s.zero || rg.MatchString(s.value) {
return s
}
s.err = NewUnitError(ErrStringMatch, Args{})
return s
}
func (s *stringVld) OneOf(values []string) *stringVld {
if s.err != nil || s.zero {
return s
}
for _, v := range values {
if v == s.value {
return s
}
}
s.err = NewUnitError(ErrStringOneOf, Args{values})
return s
}
func (s *stringVld) IsEmail() *stringVld {
if s.err != nil || s.zero || emailRegex.MatchString(s.value) {
return s
}
s.err = NewUnitError(ErrStringIsEmail, Args{})
return s
}
func (s *stringVld) Error() UnitError {
return s.err
}