-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
76 lines (63 loc) · 1.75 KB
/
errors.go
File metadata and controls
76 lines (63 loc) · 1.75 KB
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
package validator
import (
"regexp"
"strings"
)
func (v Validate) hasErrorAlready(errField string) (bool, []string) {
val, ok := v.Errors[errField]
return ok && len(val) > 0, val
}
func (v Validate) hasErrorKeyAlready(errKey, fieldKey string) bool {
for key, value := range v.Errors {
for _, v := range value {
return key == fieldKey && strings.Contains(v, errKey)
}
}
return false
}
func (v Validate) hasNamedErrorAlready(errField, errKey string) bool {
val, ok := v.Errors[errField]
if ok {
for _, value := range val {
if strings.Contains(value, errKey) {
return true
}
}
}
return false
}
func (v *Validate) addError(errField string, errKey string, keys []string) {
messages := v.Internalization.Messages()
errMessage := messages[errKey]
errMessage = errored(errMessage, keys)
v.Errors[errField] = append(v.Errors[errField], errMessage)
}
func (v Validate) GetErrors() map[string][]string {
return v.Errors
}
func (v Validate) GetError(key string) []string {
_, val := v.hasErrorAlready(key)
return val
}
func errored(v string, k []string) string {
var prepared = v
// Let's image that we've a string like this:
// :::> {0} is required.
// we need to group tokens familiar with '{0}' this
re := regexp.MustCompile(`({\d})`)
mustReplace := re.FindAllString(v, -1)
// grouped tokens can be replaced with
// token values which are real value
// for 'v'.
mustReplaceWith := k
mustReplaceWithLen := len(mustReplaceWith)
for key, value := range mustReplace {
// we've to check lengths of slices
// because if both of them's lengths are not equal,
// errors can be generated at runtime.
if key >= 0 && mustReplaceWithLen > key {
prepared = strings.Replace(prepared, value, mustReplaceWith[key], -1)
}
}
return prepared
}