Skip to content

Commit 7e731af

Browse files
joeybloggsjoeybloggs
joeybloggs
authored and
joeybloggs
committed
field name updates
1 parent e0e1af6 commit 7e731af

File tree

4 files changed

+111
-81
lines changed

4 files changed

+111
-81
lines changed

errors.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ type FieldError interface {
8686
//
8787
// NOTE: this field can be blank when validating a single primitive field
8888
// using validate.Field(...) as there is no way to extract it's name
89-
ActualNamespace() string
89+
StructNamespace() string
9090

9191
// returns the fields name with the tag name taking precedence over the
9292
// fields actual name.
@@ -95,11 +95,11 @@ type FieldError interface {
9595
// see ActualField for comparison
9696
Field() string
9797

98-
// returns the fields actual name.
98+
// returns the fields actual name from the struct, when able to determine.
9999
//
100100
// eq. "FirstName"
101101
// see Field for comparison
102-
ActualField() string
102+
StructField() string
103103

104104
// returns the actual fields value in case needed for creating the error
105105
// message
@@ -131,9 +131,9 @@ type fieldError struct {
131131
tag string
132132
actualTag string
133133
ns string
134-
actualNs string
134+
structNs string
135135
field string
136-
actualField string
136+
structField string
137137
value interface{}
138138
param interface{}
139139
kind reflect.Kind
@@ -157,10 +157,10 @@ func (fe *fieldError) Namespace() string {
157157
return fe.ns
158158
}
159159

160-
// ActualNamespace returns the namespace for the field error, with the fields
160+
// StructNamespace returns the namespace for the field error, with the fields
161161
// actual name.
162-
func (fe *fieldError) ActualNamespace() string {
163-
return fe.actualNs
162+
func (fe *fieldError) StructNamespace() string {
163+
return fe.structNs
164164
}
165165

166166
// Field returns the fields name with the tag name taking precedence over the
@@ -169,9 +169,9 @@ func (fe *fieldError) Field() string {
169169
return fe.field
170170
}
171171

172-
// ActualField returns the fields actual name.
173-
func (fe *fieldError) ActualField() string {
174-
return fe.actualField
172+
// returns the fields actual name from the struct, when able to determine.
173+
func (fe *fieldError) StructField() string {
174+
return fe.structField
175175
}
176176

177177
// Value returns the actual fields value in case needed for creating the error

struct_level.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ type StructLevelFunc func(sl StructLevel)
1010
type StructLevel interface {
1111

1212
// returns the main validation object, in case one want to call validations internally.
13+
// this is so you don;t have to use anonymous functoins to get access to the validate
14+
// instance.
1315
Validator() *Validate
1416

1517
// returns the top level struct, if any
@@ -104,7 +106,7 @@ func (v *validate) ReportError(field interface{}, fieldName, altName, tag string
104106
}
105107

106108
ns := append(v.slNs, fieldName...)
107-
nsActual := append(v.slActualNs, altName...)
109+
nsActual := append(v.slStructNs, altName...)
108110

109111
switch kind {
110112
case reflect.Invalid:
@@ -114,9 +116,9 @@ func (v *validate) ReportError(field interface{}, fieldName, altName, tag string
114116
tag: tag,
115117
actualTag: tag,
116118
ns: string(ns),
117-
actualNs: string(nsActual),
119+
structNs: string(nsActual),
118120
field: fieldName,
119-
actualField: altName,
121+
structField: altName,
120122
param: "",
121123
kind: kind,
122124
},
@@ -129,9 +131,9 @@ func (v *validate) ReportError(field interface{}, fieldName, altName, tag string
129131
tag: tag,
130132
actualTag: tag,
131133
ns: string(ns),
132-
actualNs: string(nsActual),
134+
structNs: string(nsActual),
133135
field: fieldName,
134-
actualField: altName,
136+
structField: altName,
135137
value: fv.Interface(),
136138
param: "",
137139
kind: kind,
@@ -152,7 +154,7 @@ func (v *validate) ReportValidationErrors(relativeNamespace, relativeActualNames
152154

153155
err = errs[i].(*fieldError)
154156
err.ns = string(append(append(v.slNs, err.ns...), err.field...))
155-
err.actualNs = string(append(append(v.slActualNs, err.actualNs...), err.actualField...))
157+
err.structNs = string(append(append(v.slStructNs, err.structNs...), err.structField...))
156158

157159
v.errs = append(v.errs, err)
158160
}

validator.go

+20-20
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ type validate struct {
2525
slflParent reflect.Value
2626
slCurrent reflect.Value
2727
slNs []byte
28-
slActualNs []byte
28+
slStructNs []byte
2929
flField reflect.Value
3030
flParam string
3131
}
3232

3333
// parent and current will be the same the first run of validateStruct
34-
func (v *validate) validateStruct(parent reflect.Value, current reflect.Value, typ reflect.Type, ns []byte, actualNs []byte, ct *cTag) {
34+
func (v *validate) validateStruct(parent reflect.Value, current reflect.Value, typ reflect.Type, ns []byte, structNs []byte, ct *cTag) {
3535

3636
first := len(ns) == 0
3737

@@ -45,8 +45,8 @@ func (v *validate) validateStruct(parent reflect.Value, current reflect.Value, t
4545
ns = append(ns, cs.Name...)
4646
ns = append(ns, '.')
4747

48-
actualNs = append(actualNs, cs.Name...)
49-
actualNs = append(actualNs, '.')
48+
structNs = append(structNs, cs.Name...)
49+
structNs = append(structNs, '.')
5050
}
5151

5252
// ct is nil on top level struct, and structs as fields that have no tag info
@@ -64,7 +64,7 @@ func (v *validate) validateStruct(parent reflect.Value, current reflect.Value, t
6464
}
6565
}
6666

67-
v.traverseField(parent, current.Field(f.Idx), ns, actualNs, f, f.cTags)
67+
v.traverseField(parent, current.Field(f.Idx), ns, structNs, f, f.cTags)
6868
}
6969
}
7070

@@ -76,14 +76,14 @@ func (v *validate) validateStruct(parent reflect.Value, current reflect.Value, t
7676
v.slflParent = parent
7777
v.slCurrent = current
7878
v.slNs = ns
79-
v.slActualNs = actualNs
79+
v.slStructNs = structNs
8080

8181
cs.fn(v)
8282
}
8383
}
8484

8585
// traverseField validates any field, be it a struct or single field, ensures it's validity and passes it along to be validated via it's tag options
86-
func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns []byte, actualNs []byte, cf *cField, ct *cTag) {
86+
func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns []byte, structNs []byte, cf *cField, ct *cTag) {
8787

8888
var typ reflect.Type
8989
var kind reflect.Kind
@@ -111,9 +111,9 @@ func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns
111111
tag: ct.aliasTag,
112112
actualTag: ct.tag,
113113
ns: string(append(ns, cf.Name...)),
114-
actualNs: string(append(actualNs, cf.AltName...)),
114+
structNs: string(append(structNs, cf.AltName...)),
115115
field: cf.AltName,
116-
actualField: cf.Name,
116+
structField: cf.Name,
117117
param: ct.param,
118118
kind: kind,
119119
},
@@ -127,9 +127,9 @@ func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns
127127
tag: ct.aliasTag,
128128
actualTag: ct.tag,
129129
ns: string(append(ns, cf.Name...)),
130-
actualNs: string(append(actualNs, cf.AltName...)),
130+
structNs: string(append(structNs, cf.AltName...)),
131131
field: cf.AltName,
132-
actualField: cf.Name,
132+
structField: cf.Name,
133133
value: current.Interface(),
134134
param: ct.param,
135135
kind: kind,
@@ -154,7 +154,7 @@ func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns
154154
return
155155
}
156156

157-
v.validateStruct(current, current, typ, append(append(ns, cf.Name...), '.'), append(append(actualNs, cf.AltName...), '.'), ct)
157+
v.validateStruct(current, current, typ, append(append(ns, cf.Name...), '.'), append(append(structNs, cf.AltName...), '.'), ct)
158158
return
159159
}
160160
}
@@ -201,12 +201,12 @@ OUTER:
201201
case reflect.Slice, reflect.Array:
202202

203203
for i := 0; i < current.Len(); i++ {
204-
v.traverseField(parent, current.Index(i), ns, actualNs, &cField{Name: fmt.Sprintf(arrayIndexFieldName, cf.Name, i), AltName: fmt.Sprintf(arrayIndexFieldName, cf.AltName, i)}, ct)
204+
v.traverseField(parent, current.Index(i), ns, structNs, &cField{Name: fmt.Sprintf(arrayIndexFieldName, cf.Name, i), AltName: fmt.Sprintf(arrayIndexFieldName, cf.AltName, i)}, ct)
205205
}
206206

207207
case reflect.Map:
208208
for _, key := range current.MapKeys() {
209-
v.traverseField(parent, current.MapIndex(key), ns, actualNs, &cField{Name: fmt.Sprintf(mapIndexFieldName, cf.Name, key.Interface()), AltName: fmt.Sprintf(mapIndexFieldName, cf.AltName, key.Interface())}, ct)
209+
v.traverseField(parent, current.MapIndex(key), ns, structNs, &cField{Name: fmt.Sprintf(mapIndexFieldName, cf.Name, key.Interface()), AltName: fmt.Sprintf(mapIndexFieldName, cf.AltName, key.Interface())}, ct)
210210
}
211211

212212
default:
@@ -257,9 +257,9 @@ OUTER:
257257
tag: ct.aliasTag,
258258
actualTag: ct.actualAliasTag,
259259
ns: string(append(ns, cf.Name...)),
260-
actualNs: string(append(actualNs, cf.AltName...)),
260+
structNs: string(append(structNs, cf.AltName...)),
261261
field: cf.AltName,
262-
actualField: cf.Name,
262+
structField: cf.Name,
263263
value: current.Interface(),
264264
param: ct.param,
265265
kind: kind,
@@ -274,9 +274,9 @@ OUTER:
274274
tag: errTag[1:],
275275
actualTag: errTag[1:],
276276
ns: string(append(ns, cf.Name...)),
277-
actualNs: string(append(actualNs, cf.AltName...)),
277+
structNs: string(append(structNs, cf.AltName...)),
278278
field: cf.AltName,
279-
actualField: cf.Name,
279+
structField: cf.Name,
280280
value: current.Interface(),
281281
param: ct.param,
282282
kind: kind,
@@ -305,9 +305,9 @@ OUTER:
305305
tag: ct.aliasTag,
306306
actualTag: ct.tag,
307307
ns: string(append(ns, cf.Name...)),
308-
actualNs: string(append(actualNs, cf.AltName...)),
308+
structNs: string(append(structNs, cf.AltName...)),
309309
field: cf.AltName,
310-
actualField: cf.Name,
310+
structField: cf.Name,
311311
value: current.Interface(),
312312
param: ct.param,
313313
kind: kind,

0 commit comments

Comments
 (0)