Repository navigation
Expand file tree
/
Copy pathgvalid_error.go
More file actions
155 lines (141 loc) · 3.78 KB
/
Copy pathgvalid_error.go
File metadata and controls
155 lines (141 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
8000
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
// Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gvalid
import (
"github.com/gogf/gf/text/gregex"
"strings"
)
// Error is the validation error for validation result.
type Error struct {
rules []string // Rules by sequence, which is used for keeping error sequence.
errors ErrorMap // Error map.
firstKey string // The first error rule key(nil in default).
firstItem map[string]string // The first error rule value(nil in default).
}
// ErrorMap is the validation error map:
// map[field]map[rule]message
type ErrorMap map[string]map[string]string
// newError creates and returns a validation error.
func newError(rules []string, errors map[string]map[string]string) *Error {
for field, m := range errors {
for k, v := range m {
v = strings.Replace(v, ":attribute", field, -1)
m[k], _ = gregex.ReplaceString(`\s{2,}`, ` `, v)
}
errors[field] = m
}
return &Error{
rules: rules,
errors: errors,
}
}
// newErrorStr creates and returns a validation error by string.
func newErrorStr(key, err string) *Error {
return newError(nil, map[string]map[string]string{
"__gvalid__": {
key: err,
},
})
}
// Map returns the first error message as map.
func (e *Error) Map() map[string]string {
_, m := e.FirstItem()
return m
}
// Maps returns all error messages as map.
func (e *Error) Maps() ErrorMap {
return e.errors
}
// FirstItem returns the field name and error messages for the first validation rule error.
func (e *Error) FirstItem() (key string, messages map[string]string) {
if e.firstItem != nil {
return e.firstKey, e.firstItem
}
// By sequence.
if len(e.rules) > 0 {
for _, v := range e.rules {
name, _, _ := parseSequenceTag(v)
if m, ok := e.errors[name]; ok {
e.firstKey = name
e.firstItem = m
return name, m
}
}
}
// No sequence.
for k, m := range e.errors {
e.firstKey = k
e.firstItem = m
return k, m
}
return "", nil
}
// FirstRule returns the first error rule and message string.
func (e *Error) FirstRule() (rule string, err string) {
// By sequence.
if len(e.rules) > 0 {
for _, v := range e.rules {
name, rule, _ := parseSequenceTag(v)
if m, ok := e.errors[name]; ok {
for _, rule := range strings.Split(rule, "|") {
array := strings.Split(rule, ":")
rule = strings.TrimSpace(array[0])
if err, ok := m[rule]; ok {
return rule, err
}
}
}
}
}
// No sequence.
for _, m := range e.errors {
for k, v := range m {
return k, v
}
}
return "", ""
}
// FirstString returns the first error message as string.
// Note that the returned message might be different if it has no sequence.
func (e *Error) FirstString() (err string) {
_, err = e.FirstRule()
return
}
// String returns all error messages as string, multiple error messages joined using char ';'.
func (e *Error) String() string {
return strings.Join(e.Strings(), "; ")
}
// Error implements interface of error.Error.
func (e *Error) Error() string {
return e.String()
}
// Strings returns all error messages as string array.
func (e *Error) Strings() (errs []string) {
errs = make([]string, 0)
// By sequence.
if len(e.rules) > 0 {
for _, v := range e.rules {
name, rule, _ := parseSequenceTag(v)
if m, ok := e.errors[name]; ok {
for _, rule := range strings.Split(rule, "|") {
array := strings.Split(rule, ":")
rule = strings.TrimSpace(array[0])
if err, ok := m[rule]; ok {
errs = append(errs, err)
}
}
}
}
return errs
}
// No sequence.
for _, m := range e.errors {
for _, err := range m {
errs = append(errs, err)
}
}
return
}