-
Notifications
You must be signed in to change notification settings - Fork 122
/
validator.go
224 lines (198 loc) · 6.4 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
package govalidator
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"reflect"
"strings"
)
const (
tagIdentifier = "json" //tagName idetify the struct tag for govalidator
tagSeparator = "|" //tagSeparator use to separate tags in struct
defaultFormSize int64 = 1024 * 1024 * 1
)
type (
// MapData represents basic data structure for govalidator Rules and Messages
MapData map[string][]string
// Options describes configuration option for validator
Options struct {
Data interface{} // Data represents structure for JSON body
Request *http.Request
RequiredDefault bool // RequiredDefault represents if all the fields are by default required or not
Rules MapData // Rules represents rules for form-data/x-url-encoded/query params data
Messages MapData // Messages represents custom/localize message for rules
TagIdentifier string // TagIdentifier represents struct tag identifier, e.g: json or validate etc
FormSize int64 //Form represents the multipart forom data max memory size in bytes
}
// Validator represents a validator with options
Validator struct {
Opts Options // Opts contains all the options for validator
}
)
// New return a new validator object using provided options
func New(opts Options) *Validator {
return &Validator{Opts: opts}
}
// getMessage return if a custom message exist against the field name and rule
// if not available it return an empty string
func (v *Validator) getCustomMessage(field, rule string) string {
if msgList, ok := v.Opts.Messages[field]; ok {
for _, m := range msgList {
//if rules has params, remove params. e.g: between:3,5 would be between
if strings.Contains(rule, ":") {
rule = strings.Split(rule, ":")[0]
}
if strings.HasPrefix(m, rule+":") {
return strings.TrimPrefix(m, rule+":")
}
}
}
return ""
}
// SetDefaultRequired change the required behavior of fields
// Default value if false
// If SetDefaultRequired set to true then it will mark all the field in the rules list as required
func (v *Validator) SetDefaultRequired(required bool) {
v.Opts.RequiredDefault = required
}
// SetTagIdentifier change the default tag identifier (json) to your custom tag.
func (v *Validator) SetTagIdentifier(identifier string) {
v.Opts.TagIdentifier = identifier
}
// Validate validate request data like form-data, x-www-form-urlencoded and query params
// see example in README.md file
// ref: https://github.com/thedevsaddam/govalidator#example
func (v *Validator) Validate() url.Values {
// if request object and rules not passed rise a panic
if len(v.Opts.Rules) == 0 || v.Opts.Request == nil {
panic(errValidateArgsMismatch)
}
errsBag := url.Values{}
// get non required rules
nr := v.getNonRequiredFields()
for field, rules := range v.Opts.Rules {
if _, ok := nr[field]; ok {
continue
}
for _, rule := range rules {
if !isRuleExist(rule) {
panic(fmt.Errorf("govalidator: %s is not a valid rule", rule))
}
msg := v.getCustomMessage(field, rule)
// validate file
if strings.HasPrefix(field, "file:") {
fld := strings.TrimPrefix(field, "file:")
file, fh, _ := v.Opts.Request.FormFile(fld)
if file != nil && fh.Filename != "" {
validateFiles(v.Opts.Request, fld, rule, msg, errsBag)
validateCustomRules(fld, rule, msg, file, errsBag)
} else {
validateCustomRules(fld, rule, msg, nil, errsBag)
}
} else {
// validate if custom rules exist
reqVal := strings.TrimSpace(v.Opts.Request.Form.Get(field))
validateCustomRules(field, rule, msg, reqVal, errsBag)
}
}
}
return errsBag
}
// getNonRequiredFields remove non required rules fields from rules if requiredDefault field is false
// and if the input data is empty for this field
func (v *Validator) getNonRequiredFields() map[string]struct{} {
if v.Opts.FormSize > 0 {
_ = v.Opts.Request.ParseMultipartForm(v.Opts.FormSize)
} else {
_ = v.Opts.Request.ParseMultipartForm(defaultFormSize)
}
inputs := v.Opts.Request.Form
nr := make(map[string]struct{})
if !v.Opts.RequiredDefault {
for k, r := range v.Opts.Rules {
isFile := strings.HasPrefix(k, "file:")
if _, ok := inputs[k]; !ok && !isFile {
if !isContainRequiredField(r) {
nr[k] = struct{}{}
}
}
}
}
return nr
}
// ValidateJSON validate request data from JSON body to Go struct
// see example in README.md file
func (v *Validator) ValidateJSON() url.Values {
if len(v.Opts.Rules) == 0 || v.Opts.Request == nil {
panic(errValidateArgsMismatch)
}
if reflect.TypeOf(v.Opts.Data).Kind() != reflect.Ptr {
panic(errRequirePtr)
}
return v.internalValidateStruct()
}
func (v *Validator) ValidateStruct() url.Values {
if len(v.Opts.Rules) == 0 {
panic(errRequireRules)
}
if v.Opts.Request != nil {
panic(errRequestNotAccepted)
}
if v.Opts.Data != nil && reflect.TypeOf(v.Opts.Data).Kind() != reflect.Ptr {
panic(errRequirePtr)
}
if v.Opts.Data == nil {
panic(errRequireData)
}
return v.internalValidateStruct()
}
func (v *Validator) internalValidateStruct() url.Values {
errsBag := url.Values{}
if v.Opts.Request != nil && v.Opts.Request.Body != http.NoBody {
defer v.Opts.Request.Body.Close()
err := json.NewDecoder(v.Opts.Request.Body).Decode(v.Opts.Data)
if err != nil {
errsBag.Add("_error", err.Error())
return errsBag
}
}
r := roller{}
r.setTagIdentifier(tagIdentifier)
if v.Opts.TagIdentifier != "" {
r.setTagIdentifier(v.Opts.TagIdentifier)
}
r.setTagSeparator(tagSeparator)
r.start(v.Opts.Data)
//clean if the key is not exist or value is empty or zero value
nr := v.getNonRequiredJSONFields(r.getFlatMap())
for field, rules := range v.Opts.Rules {
if _, ok := nr[field]; ok {
continue
}
value, _ := r.getFlatVal(field)
for _, rule := range rules {
if !isRuleExist(rule) {
panic(fmt.Errorf("govalidator: %s is not a valid rule", rule))
}
msg := v.getCustomMessage(field, rule)
validateCustomRules(field, rule, msg, value, errsBag)
}
}
return errsBag
}
// getNonRequiredJSONFields get non required rules fields from rules if requiredDefault field is false
// and if the input data is empty for this field
func (v *Validator) getNonRequiredJSONFields(inputs map[string]interface{}) map[string]struct{} {
nr := make(map[string]struct{})
if !v.Opts.RequiredDefault {
for k, r := range v.Opts.Rules {
if val := inputs[k]; isEmpty(val) {
if !isContainRequiredField(r) {
nr[k] = struct{}{}
}
}
}
}
return nr
}