-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
237 lines (189 loc) · 5.08 KB
/
validator.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package goravel
import (
"fmt"
"net/url"
"strconv"
"strings"
"time"
"github.com/asaskevich/govalidator"
)
type ErrorsMap map[string][]string
// Add adds an error message to a given Validation field
func (e ErrorsMap) Add(field, message string) {
e[field] = append(e[field], message)
}
// Get returns the first error message for a given field
func (e ErrorsMap) Get(field string) string {
es := e[field]
if len(es) == 0 {
return ""
}
return es[0]
}
// Validation creates a custom Validation struct, embeds a url.Values object
type Validation struct {
// Validation Struct will contain all the key value pairs of the url.Values
// object(field name and value pairs of html Validation that was submitted by the user)
Data url.Values // map[string][]string
Errors ErrorsMap // map[string][]string
}
// New initializes a custom Validation struct
func (g *Goravel) Validator(data url.Values) *Validation {
return &Validation{
Data: data,
Errors: ErrorsMap(map[string][]string{}),
}
}
// Valid returns true if there are no errors, otherwise false
func (v *Validation) IsValid() bool {
return len(v.Errors) == 0
}
// Check adds an error message to the validation object if the condition is not met
func (v *Validation) Check(ok bool, field, message string) {
if !ok {
v.Errors.Add(field, message)
}
}
// To check if a Validation field in POST request is empty or not
func (v *Validation) HasValue(field string, value ...string) bool {
validationValue := ""
if len(value) > 0 {
validationValue = value[0]
} else {
validationValue = v.Data.Get(field)
}
if validationValue == "" {
v.Errors.Add(field, "This field can't be blank")
}
return validationValue != ""
}
// To check if any of the Validation field in POST request is empty or not
func (v *Validation) HasRequired(requiredFields ...string) {
// every required field should be present in the url.Values object
for _, field := range requiredFields {
value := v.Data.Get(field)
if strings.TrimSpace(value) == "" {
v.Errors.Add(field, "This field can't be blank")
}
}
}
func (v *Validation) HasMinLength(field string, length int, value ...string) bool {
validationValue := ""
if len(value) > 0 {
validationValue = value[0]
} else {
validationValue = v.Data.Get(field)
}
if len(validationValue) < length {
v.Errors.Add(field, fmt.Sprintf("This field must be %d charcaters long or more", length))
return false
}
return true
}
func (v *Validation) IsValidEmail(field string, value ...string) bool {
validationValue := ""
if len(value) > 0 {
validationValue = value[0]
} else {
validationValue = v.Data.Get(field)
}
if !govalidator.IsEmail(validationValue) {
v.Errors.Add(field, "Invaid Email")
return false
}
return true
}
func (v *Validation) IsValidUrl(field string, value ...string) bool {
validationValue := ""
if len(value) > 0 {
validationValue = value[0]
} else {
validationValue = v.Data.Get(field)
}
if !govalidator.IsURL(validationValue) {
v.Errors.Add(field, "Invaid URL")
return false
}
return true
}
func (v *Validation) IsValidPassword(field string, value ...string) bool {
validationValue := ""
if len(value) > 0 {
validationValue = value[0]
} else {
validationValue = v.Data.Get(field)
}
if !govalidator.IsByteLength(validationValue, 8, 50) {
v.Errors.Add(field, "Password must be between 8 and 50 characters")
return false
}
return true
}
func (v *Validation) IsValidUsername(field string, value ...string) bool {
validationValue := ""
if len(value) > 0 {
validationValue = value[0]
} else {
validationValue = v.Data.Get(field)
}
if !govalidator.IsAlphanumeric(validationValue) {
v.Errors.Add(field, "Username must be alphanumeric")
return false
}
return true
}
func (v *Validation) IsInt(field string, value ...string) bool {
validationValue := ""
if len(value) > 0 {
validationValue = value[0]
} else {
validationValue = v.Data.Get(field)
}
_, err := strconv.Atoi(validationValue)
if err != nil {
v.Errors.Add(field, "This field must be an integer")
return false
}
return true
}
func (v *Validation) IsFloat(field string, value ...string) bool {
validationValue := ""
if len(value) > 0 {
validationValue = value[0]
} else {
validationValue = v.Data.Get(field)
}
_, err := strconv.ParseFloat(validationValue, 64)
if err != nil {
v.Errors.Add(field, "This field must be a float")
return false
}
return true
}
func (v *Validation) IsDateISO(field string, value ...string) bool {
validationValue := ""
if len(value) > 0 {
validationValue = value[0]
} else {
validationValue = v.Data.Get(field)
}
_, err := time.Parse("2006-01-02", validationValue)
if err != nil {
v.Errors.Add(field, "This field must be a date in the format YYYY-MM-DD")
return false
}
return true
}
func (v *Validation) NoWhitespace(field string, value ...string) bool {
validationValue := ""
if len(value) > 0 {
validationValue = value[0]
} else {
validationValue = v.Data.Get(field)
}
if govalidator.HasWhitespace(validationValue) {
v.Errors.Add(field, "This field must not contain any whitespace")
return false
}
return true
}