-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.go
More file actions
87 lines (70 loc) · 1.54 KB
/
Copy pathstrings.go
File metadata and controls
87 lines (70 loc) · 1.54 KB
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
package validate
import (
"net/mail"
"regexp"
"strings"
"unicode"
)
func Email(value string) error {
_, merr := mail.ParseAddress(string(value))
if merr != nil {
return &Violation{Code: CodeEmail}
}
return nil
}
func Regex(re *regexp.Regexp) Validator[string] {
return func(value string) error {
if !re.MatchString(value) {
return &Violation{Code: CodeRegex, Args: Args{"pattern": re.String()}}
}
return nil
}
}
func MinString(length int) Validator[string] {
return func(value string) error {
if len(value) < length {
return &Violation{Code: CodeStringMin, Args: Args{"min": length}}
}
return nil
}
}
func MaxString(length int) Validator[string] {
return func(value string) error {
if len(value) > length {
return &Violation{Code: CodeStringMax, Args: Args{"max": length}}
}
return nil
}
}
func Lowercase(value string) error {
for _, r := range value {
if unicode.IsUpper(r) {
return &Violation{Code: CodeLowercase}
}
}
return nil
}
func Uppercase(value string) error {
for _, r := range value {
if unicode.IsLower(r) {
return &Violation{Code: CodeUppercase}
}
}
return nil
}
func Prefix(prefix string) Validator[string] {
return func(value string) error {
if !strings.HasPrefix(value, prefix) {
return &Violation{Code: "prefix", Args: Args{"prefix": prefix}}
}
return nil
}
}
func Suffix(suffix string) Validator[string] {
return func(value string) error {
if !strings.HasSuffix(value, suffix) {
return &Violation{Code: "suffix", Args: Args{"suffix": suffix}}
}
return nil
}
}