-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathvalidation.go
314 lines (265 loc) · 9.3 KB
/
validation.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package valgo
import (
"fmt"
"strconv"
"strings"
)
// The [Validation] session in Valgo is the main structure for validating one or
// more values. It is called Validation in code.
//
// A [Validation] session will contain one or more Validators, where each [Validator]
// will have the responsibility to validate a value with one or more rules.
//
// There are multiple functions to create a [Validation] session, depending on the
// requirements:
//
// - [New]()
// - [Is](...)
// - [In](...)
// - [InRow](...)
// - [Check](...)
// - [AddErrorMessage](...)
//
// the function [Is](...) is likely to be the most frequently used function in your
// validations. When [Is](...) is called, the function creates a validation and
// receives a validator at the same time.
type Validation struct {
valid bool
_locale *Locale
errors map[string]*valueError
currentIndex int
marshalJsonFunc func(e *Error) ([]byte, error)
}
// Options struct is used to specify options when creating a new [Validation]
// session with the [New()] function.
//
// It contains parameters for specifying a specific locale code, modify or add a
// locale, and set a custom JSON marshaler for [Error].
type Options struct {
localeCodeDefaultFromFactory string // Only specified by the factory
localesFromFactory map[string]*Locale // Only specified by the factory
// A string field that represents the locale code to use by the [Validation]
// session
LocaleCode string
// A map field that allows to modify or add a new [Locale]
Locale *Locale
// A function field that allows to set a custom JSON marshaler for [Error]
MarshalJsonFunc func(e *Error) ([]byte, error)
}
// Add one or more validators to a [Validation] session.
func (validation *Validation) Is(validators ...Validator) *Validation {
for _, v := range validators {
validation = v.Context().validateIs(validation)
}
return validation
}
// Add one or more validators to a [Validation] session. But unlike [Is()],
// the validators are not short-circuited.
func (validation *Validation) Check(validators ...Validator) *Validation {
for _, v := range validators {
validation = v.Context().validateCheck(validation)
}
return validation
}
// A [Validation] session provides this function which returns either true if
// all their validators are valid or false if any one of them is invalid.
//
// In the following example, even though the [Validator] for age is valid, the
// [Validator] for status is invalid, making the entire Validator session
// invalid.
func (validation *Validation) Valid() bool {
return validation.valid
}
// Add a map namespace to a [Validation] session.
func (validation *Validation) In(name string, _validation *Validation) *Validation {
return validation.merge(name, _validation)
}
// Add an indexed namespace to a [Validation] session.
func (validation *Validation) InRow(name string, index int, _validation *Validation) *Validation {
return validation.merge(fmt.Sprintf("%s[%v]", name, index), _validation)
}
// Using [Merge](...) you can merge two [Validation] sessions. When two
// validations are merged, errors with the same value name will be merged. It is
// useful for reusing validation logic.
//
// The following example merges the [Validation] session returned by the
// validatePreStatus function. Since both [Validation] sessions validate a value
// with the name status, the error returned will return two error messages, and
// without duplicate the Not().Blank() error message rule.
func (validation *Validation) Merge(_validation *Validation) *Validation {
return validation.merge("", _validation)
}
func (validation *Validation) merge(prefix string, _validation *Validation) *Validation {
var _prefix string
if len(strings.TrimSpace(prefix)) > 0 {
_prefix = prefix + "."
}
LOOP1:
for _field, _err := range _validation.Errors() {
for field, err := range validation.Errors() {
if _prefix+_field == field {
LOOP2:
for _, _errMsg := range _err.Messages() {
for _, errMsg := range err.Messages() {
if _errMsg == errMsg {
continue LOOP2
}
}
validation.AddErrorMessage(_prefix+_field, _errMsg)
}
continue LOOP1
}
}
for _, _errMsg := range _err.Messages() {
validation.AddErrorMessage(_prefix+_field, _errMsg)
}
}
return validation
}
// Add an error message to the [Validation] session without executing a field
// validator. By adding this error message, the [Validation] session will be
// marked as invalid.
func (v *Validation) AddErrorMessage(name string, message string) *Validation {
if v.errors == nil {
v.errors = map[string]*valueError{}
}
v.valid = false
ev := v.getOrCreateValueError(name, nil)
ev.errorMessages = append(ev.errorMessages, message)
return v
}
func (v *Validation) mergeError(prefix string, err *Error) *Validation {
if err != nil && len(err.errors) > 0 {
if v.errors == nil {
v.errors = map[string]*valueError{}
}
v.valid = false
var _prefix string
if len(strings.TrimSpace(prefix)) > 0 {
_prefix = prefix + "."
}
for name, _ev := range err.errors {
for _, message := range _ev.Messages() {
v.AddErrorMessage(_prefix+name, message)
}
}
}
return v
}
// MergeError allows merging Valgo errors from an already validated [Validation] session.
// The function takes an Valgo [Error] pointer as an argument and returns a [Validation] pointer.
func (v *Validation) MergeError(err *Error) *Validation {
return v.mergeError("", err)
}
// MergeErrorIn allows merging Valgo errors from already validated [Validation] sessions
// within a map namespace. The function takes a namespace name and an [Error] pointer
// as arguments and returns a [Validation] pointer.
func (v *Validation) MergeErrorIn(name string, err *Error) *Validation {
return v.mergeError(name, err)
}
// MergeErrorInRow allows merging Valgo errors from already validated [Validation] sessions
// within an indexed namespace. The function takes a namespace name, an index, and an [Error] pointer
// as arguments and returns a [Validation] pointer.
func (v *Validation) MergeErrorInRow(name string, index int, err *Error) *Validation {
return v.mergeError(fmt.Sprintf("%s[%v]", name, index), err)
}
func (validation *Validation) invalidate(name *string, title *string, fragment *validatorFragment) {
if validation.errors == nil {
validation.errors = map[string]*valueError{}
}
validation.valid = false
var _name string
if name == nil {
_name = concatString("value_", strconv.Itoa(validation.currentIndex-1))
} else {
_name = *name
}
ev := validation.getOrCreateValueError(_name, title)
errorKey := fragment.errorKey
if !fragment.boolOperation {
errorKey = concatString("not_", errorKey)
}
if _, ok := ev.errorTemplates[errorKey]; !ok {
ev.errorTemplates[errorKey] = &errorTemplate{
key: errorKey,
}
}
et := ev.errorTemplates[errorKey]
if len(fragment.template) > 0 {
et.template = &fragment.template[0]
}
et.params = fragment.templateParams
}
// Return a map with the information for each invalid field validator
// in the Validation session.
func (session *Validation) Errors() map[string]*valueError {
return session.errors
}
// Return an error object that encapsulates the validation errors created during
// the Validation session. If the session is valid, it returns nil.
//
// An optional JSON marshaling function can be passed to customize how the
// validation errors are serialized into JSON. If no function is provided, a
// default marshaling behavior is used.
func (validation *Validation) Error(marshalJsonFun ...func(e *Error) ([]byte, error)) error {
if !validation.valid {
fn := validation.marshalJsonFunc
if len(marshalJsonFun) > 0 {
fn = marshalJsonFun[0]
}
return &Error{
errors: validation.errors,
marshalJsonFunc: fn,
}
}
return nil
}
// Return true if a specific field validator is valid.
func (validation *Validation) IsValid(name string) bool {
if _, isNotValid := validation.errors[name]; isNotValid {
return false
}
return true
}
func (validation *Validation) getOrCreateValueError(name string, title *string) *valueError {
if _, ok := validation.errors[name]; !ok {
validation.errors[name] = &valueError{
name: &name,
title: title,
errorTemplates: map[string]*errorTemplate{},
errorMessages: []string{},
validator: validation,
}
}
ev := validation.errors[name]
ev.dirty = true
return ev
}
func newValidation(options ...Options) *Validation {
v := &Validation{
valid: true,
}
if len(options) == 0 {
v._locale = getLocale(localeCodeDefault)
} else {
_options := options[0]
// If the factory has default locale specified, we try to use it as fallback
if options[0].localeCodeDefaultFromFactory != "" {
// Skipping default option will return nil, so we can use the factory
// locale default
v._locale = getLocaleAndSkipDefaultOption(_options.LocaleCode, options[0].localesFromFactory)
if v._locale == nil {
v._locale = getLocale(options[0].localeCodeDefaultFromFactory, options[0].localesFromFactory)
}
} else {
v._locale = getLocale(_options.LocaleCode, options[0].localesFromFactory)
}
// If locale entries were specified, then we merge it with the calculated
// Locale from the options localeCode
if _options.Locale != nil {
v._locale.merge(_options.Locale)
}
v.marshalJsonFunc = _options.MarshalJsonFunc
}
return v
}