1 package validator
2
3 import (
4 "context"
5 "reflect"
6 )
7
8
9 type StructLevelFunc func(sl StructLevel)
10
11
12
13 type StructLevelFuncCtx func(ctx context.Context, sl StructLevel)
14
15
16 func wrapStructLevelFunc(fn StructLevelFunc) StructLevelFuncCtx {
17 return func(ctx context.Context, sl StructLevel) {
18 fn(sl)
19 }
20 }
21
22
23
24 type StructLevel interface {
25
26
27
28
29 Validator() *Validate
30
31
32 Top() reflect.Value
33
34
35 Parent() reflect.Value
36
37
38 Current() reflect.Value
39
40
41
42
43 ExtractType(field reflect.Value) (value reflect.Value, kind reflect.Kind, nullable bool)
44
45
46
47
48
49
50
51
52
53
54
55 ReportError(field interface{}, fieldName, structFieldName string, tag, param string)
56
57
58
59
60
61
62
63
64
65
66 ReportValidationErrors(relativeNamespace, relativeActualNamespace string, errs ValidationErrors)
67 }
68
69 var _ StructLevel = new(validate)
70
71
72
73
74
75
76
77
78 func (v *validate) Top() reflect.Value {
79 return v.top
80 }
81
82
83
84
85
86
87
88
89 func (v *validate) Parent() reflect.Value {
90 return v.slflParent
91 }
92
93
94 func (v *validate) Current() reflect.Value {
95 return v.slCurrent
96 }
97
98
99 func (v *validate) Validator() *Validate {
100 return v.v
101 }
102
103
104 func (v *validate) ExtractType(field reflect.Value) (reflect.Value, reflect.Kind, bool) {
105 return v.extractTypeInternal(field, false)
106 }
107
108
109 func (v *validate) ReportError(field interface{}, fieldName, structFieldName, tag, param string) {
110
111 fv, kind, _ := v.extractTypeInternal(reflect.ValueOf(field), false)
112
113 if len(structFieldName) == 0 {
114 structFieldName = fieldName
115 }
116
117 v.str1 = string(append(v.ns, fieldName...))
118
119 if v.v.hasTagNameFunc || fieldName != structFieldName {
120 v.str2 = string(append(v.actualNs, structFieldName...))
121 } else {
122 v.str2 = v.str1
123 }
124
125 if kind == reflect.Invalid {
126
127 v.errs = append(v.errs,
128 &fieldError{
129 v: v.v,
130 tag: tag,
131 actualTag: tag,
132 ns: v.str1,
133 structNs: v.str2,
134 fieldLen: uint8(len(fieldName)),
135 structfieldLen: uint8(len(structFieldName)),
136 param: param,
137 kind: kind,
138 },
139 )
140 return
141 }
142
143 v.errs = append(v.errs,
144 &fieldError{
145 v: v.v,
146 tag: tag,
147 actualTag: tag,
148 ns: v.str1,
149 structNs: v.str2,
150 fieldLen: uint8(len(fieldName)),
151 structfieldLen: uint8(len(structFieldName)),
152 value: fv.Interface(),
153 param: param,
154 kind: kind,
155 typ: fv.Type(),
156 },
157 )
158 }
159
160
161
162
163 func (v *validate) ReportValidationErrors(relativeNamespace, relativeStructNamespace string, errs ValidationErrors) {
164
165 var err *fieldError
166
167 for i := 0; i < len(errs); i++ {
168
169 err = errs[i].(*fieldError)
170 err.ns = string(append(append(v.ns, relativeNamespace...), err.ns...))
171 err.structNs = string(append(append(v.actualNs, relativeStructNamespace...), err.structNs...))
172
173 v.errs = append(v.errs, err)
174 }
175 }
176
View as plain text