This repository has been archived by the owner on Feb 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
validators.go
212 lines (189 loc) · 4.57 KB
/
validators.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
package gforms
import (
"errors"
"fmt"
"reflect"
"regexp"
)
type Validator interface {
Name() string
Validate(*FieldInstance, *FormInstance) error
}
type Validators []Validator
type required struct {
Message string
Validator
}
// Returns error if the field is not provided.
func Required(message ...string) required {
vl := required{}
if len(message) > 0 {
vl.Message = message[0]
} else {
vl.Message = "This field is required."
}
return vl
}
func (vl required) Validate(fi *FieldInstance, fo *FormInstance) error {
v := fi.V
if v.IsNil || (v.Kind == reflect.String && v.Value == "") {
return errors.New(vl.Message)
}
return nil
}
type maxLengthValidator struct {
Length int
Message string
Validator
}
// Returns error if the length of value is greater than length argument.
func MaxLengthValidator(length int, message ...string) maxLengthValidator {
vl := maxLengthValidator{}
vl.Length = length
if len(message) > 0 {
vl.Message = message[0]
} else {
vl.Message = fmt.Sprintf("Ensure this value has at most %v characters.", vl.Length)
}
return vl
}
func (vl maxLengthValidator) Validate(fi *FieldInstance, fo *FormInstance) error {
v := fi.V
if v.IsNil || v.Kind != reflect.String || v.Value == "" {
return nil
}
s := v.Value.(string)
if len(s) > vl.Length {
return errors.New(vl.Message)
}
return nil
}
type minLengthValidator struct {
Length int
Message string
Validator
}
// Returns error if the length of value is less than length argument.
func MinLengthValidator(length int, message ...string) minLengthValidator {
vl := minLengthValidator{}
vl.Length = length
if len(message) > 0 {
vl.Message = message[0]
} else {
vl.Message = fmt.Sprintf("Ensure this value has at least %v characters", vl.Length)
}
return vl
}
func (vl minLengthValidator) Validate(fi *FieldInstance, fo *FormInstance) error {
v := fi.V
if v.IsNil || v.Kind != reflect.String || v.Value == "" {
return nil
}
s := v.Value.(string)
if len(s) < vl.Length {
return errors.New(vl.Message)
}
return nil
}
type maxValueValidator struct {
Value int
Message string
Validator
}
func MaxValueValidator(value int, message ...string) maxValueValidator {
vl := maxValueValidator{}
vl.Value = value
if len(message) > 0 {
vl.Message = message[0]
} else {
vl.Message = fmt.Sprintf("Ensure this value is less than or equal to %v.", vl.Value)
}
return vl
}
func (vl maxValueValidator) Validate(fi *FieldInstance, fo *FormInstance) error {
v := fi.V
if v.IsNil || v.Kind != reflect.Int {
return nil
}
iv := v.Value.(int)
if iv > vl.Value {
return errors.New(vl.Message)
}
return nil
}
type minValueValidator struct {
Value int
Message string
Validator
}
func MinValueValidator(value int, message ...string) minValueValidator {
vl := minValueValidator{}
vl.Value = value
if len(message) > 0 {
vl.Message = message[0]
} else {
vl.Message = fmt.Sprintf("Ensure this value is greater than or equal to %v.", vl.Value)
}
return vl
}
func (vl minValueValidator) Validate(fi *FieldInstance, fo *FormInstance) error {
v := fi.V
if v.IsNil || v.Kind != reflect.Int {
return nil
}
iv := v.Value.(int)
if iv < vl.Value {
return errors.New(vl.Message)
}
return nil
}
type regexpValidator struct {
re *regexp.Regexp
Message string
Validator
}
func (vl regexpValidator) Validate(fi *FieldInstance, fo *FormInstance) error {
v := fi.V
if v.IsNil || v.Kind != reflect.String || v.Value == "" {
return nil
}
sv := v.Value.(string)
if !vl.re.MatchString(sv) {
return errors.New(vl.Message)
}
return nil
}
// The regular expression pattern to search for the provided value.
// Returns error if regxp#MatchString is False.
func RegexpValidator(regex string, message ...string) regexpValidator {
vl := regexpValidator{}
re, err := regexp.Compile(regex)
if err != nil {
panic(err)
}
vl.re = re
if len(message) > 0 {
vl.Message = message[0]
} else {
vl.Message = fmt.Sprintf("Enter a valid value.")
}
return vl
}
// An EmailValidator that ensures a value looks like an email address.
func EmailValidator(message ...string) regexpValidator {
regex := `(?i)^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`
if len(message) > 0 {
return RegexpValidator(regex, message[0])
} else {
return RegexpValidator(regex, "Enter a valid email address.")
}
}
// An URLValidator that ensures a value looks like an url.
func URLValidator(message ...string) regexpValidator {
regex := `^(https?|ftp)(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)$`
if len(message) > 0 {
return RegexpValidator(regex, message[0])
} else {
return RegexpValidator(regex, "Enter a valid url.")
}
}