-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstr.go
194 lines (167 loc) · 4.59 KB
/
str.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package vaddie
import (
"fmt"
"strings"
"unicode"
)
// StrNotEmpty validates that a given string is not empty.
func StrNotEmpty() ValidateValue[string] {
return func(value string) error {
if value == "" {
return &ValidationError{
Message: "is empty",
}
}
return nil
}
}
// StrMin validates our value is at least a minimum length.
func StrMin(minLength int) ValidateValue[string] {
return func(value string) error {
length := len(value)
if length < minLength {
return &ValidationError{
Message: "too short",
Help: fmt.Sprintf("%d < %d", length, minLength),
}
}
return nil
}
}
// StrMax validates our value is no more then a maximum length.
func StrMax(maxLength int) ValidateValue[string] {
return func(value string) error {
length := len(value)
if length > maxLength {
return &ValidationError{
Message: "too long",
Help: fmt.Sprintf("%d > %d", length, maxLength),
}
}
return nil
}
}
// StrLetters validates every rune is a letter.
func StrLetters() ValidateValue[string] {
return func(value string) error {
for i, v := range value {
if !unicode.IsLetter(v) {
return &ValidationError{
Message: "non-letter rune",
Help: fmt.Sprintf("'%v' at index %d", v, i),
}
}
}
return nil
}
}
// StrAscii validates every rune is an ascii value.
func StrAscii() ValidateValue[string] {
return func(value string) error {
for i, v := range value {
if v > unicode.MaxASCII {
return &ValidationError{
Message: "non-ascii rune",
Help: fmt.Sprintf("'%v' at index %d", v, i),
}
}
}
return nil
}
}
// StrHasPrefix validates our string has the provided prefix.
func StrHasPrefix(prefix string) ValidateValue[string] {
return func(value string) error {
if !strings.HasPrefix(value, prefix) {
return &ValidationError{
Message: "does not have prefix",
Help: fmt.Sprintf("'%v' does not have expected prefix '%s'", value, prefix),
}
}
return nil
}
}
// StrNotHasPrefix validates our string does not have the provided prefix.
func StrNotHasPrefix(prefix string) ValidateValue[string] {
return func(value string) error {
if strings.HasPrefix(value, prefix) {
return &ValidationError{
Message: "does have prefix",
Help: fmt.Sprintf("'%v' does have unexpected prefix '%s'", value, prefix),
}
}
return nil
}
}
// StrHasSuffix validates our string has the provided suffix.
func StrHasSuffix(suffix string) ValidateValue[string] {
return func(value string) error {
if !strings.HasSuffix(value, suffix) {
return &ValidationError{
Message: "does not have suffix",
Help: fmt.Sprintf("'%v' does not have expected suffix '%s'", value, suffix),
}
}
return nil
}
}
// StrNotHasSuffix validates our string does not have the provided suffix.
func StrNotHasSuffix(suffix string) ValidateValue[string] {
return func(value string) error {
if strings.HasSuffix(value, suffix) {
return &ValidationError{
Message: "does have suffix",
Help: fmt.Sprintf("'%v' does have unexpected suffix '%s'", value, suffix),
}
}
return nil
}
}
// StrContains validates our string contains the provided substring.
func StrContains(substr string) ValidateValue[string] {
return func(value string) error {
if !strings.Contains(value, substr) {
return &ValidationError{
Message: "does not have substr",
Help: fmt.Sprintf("'%v' does not have expected substr '%s'", value, substr),
}
}
return nil
}
}
// StrNotContains validates our string does not contain the provided substring.
func StrNotContains(substr string) ValidateValue[string] {
return func(value string) error {
if strings.Contains(value, substr) {
return &ValidationError{
Message: "does have substr",
Help: fmt.Sprintf("'%v' does have unexpected substr '%s'", value, substr),
}
}
return nil
}
}
// StrContainsAny validates whether any Unicode code points in chars are within value.
func StrContainsAny(chars string) ValidateValue[string] {
return func(value string) error {
if !strings.ContainsAny(value, chars) {
return &ValidationError{
Message: "does not have chars",
Help: fmt.Sprintf("'%v' does not have any of the chars '%s'", value, chars),
}
}
return nil
}
}
// StrNotContainsAny validates whether all Unicode code points in chars are not within value.
func StrNotContainsAny(chars string) ValidateValue[string] {
return func(value string) error {
if strings.ContainsAny(value, chars) {
return &ValidationError{
Message: "does have chars",
Help: fmt.Sprintf("'%v' does have unexpected chars '%s'", value, chars),
}
}
return nil
}
}