-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.go
98 lines (85 loc) · 2.23 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
package valider
import (
"regexp"
"time"
)
type Str struct {
value string
field string
errors Errors
}
func (v *Validator) Str(value, field string) *Str {
return &Str{value, field, v.Errors}
}
func (str *Str) Required() *Str {
if str.value == "" {
str.errors[str.field] = append(str.errors[str.field], Error{ErrRequired, CodeRequired, nil})
}
return str
}
func (str *Str) Equal(eq string) *Str {
if str.value != "" && str.value != eq {
str.errors[str.field] = append(str.errors[str.field], Error{ErrNotEqual, CodeNotEqual, eq})
}
return str
}
func (str *Str) NotEqual(eq string) *Str {
if str.value != "" && str.value == eq {
str.errors[str.field] = append(str.errors[str.field], Error{ErrNotEqual, CodeNotEqual, eq})
}
return str
}
func (str *Str) Len(num int) *Str {
if str.value != "" && len(str.value) != num {
str.errors[str.field] = append(str.errors[str.field], Error{ErrLen, CodeLen, num})
}
return str
}
func (str *Str) Range(min, max int) *Str {
len := len(str.value)
if str.value != "" && len < min || len > max {
str.errors[str.field] = append(str.errors[str.field], Error{ErrOutRange, CodeOutRange, []int{min, max}})
}
return str
}
func (str *Str) In(values ...string) *Str {
if str.value != "" {
for _, value := range values {
if str.value == value {
return str
}
}
str.errors[str.field] = append(str.errors[str.field], Error{ErrIn, CodeIn, values})
}
return str
}
func (str *Str) Date(layout string) *Str {
if str.value != "" {
if _, err := time.Parse(layout, str.value); err != nil {
str.errors[str.field] = append(str.errors[str.field], Error{ErrDate, CodeDate, layout})
}
}
return str
}
func (str *Str) Email() *Str {
if str.value == "" {
return str
}
return str.RegExp(PatternEmail)
}
func (str *Str) URL() *Str {
if str.value != "" {
return str.RegExp(PatternURL)
}
return str
}
func (str *Str) RegExp(pattern string) *Str {
if str.value != "" {
if matched, err := regexp.MatchString(pattern, str.value); err != nil {
str.errors[str.field] = append(str.errors[str.field], Error{ErrBadParameter, CodeBadParameter, pattern})
} else if !matched {
str.errors[str.field] = append(str.errors[str.field], Error{ErrNotMatched, CodeNotMatched, pattern})
}
}
return str
}