Skip to content

Commit 6318540

Browse files
authored
Merge pull request #1 from VDHewei/feat/v10.22.1/custom_msg
Go Playground/validator 扩展 PR 提案分析
2 parents c3fc72e + 5e95a93 commit 6318540

5 files changed

Lines changed: 28 additions & 1 deletion

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ _testmain.go
3030
cover.html
3131
README.html
3232
.idea
33+
.vscode

errors.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,16 @@ type FieldError interface {
153153
Error() string
154154
}
155155

156+
type TopFieldError interface {
157+
FieldError
158+
159+
// Top returns the actual field's parent value in case needed for creating custom the error
160+
Top() reflect.Value
161+
}
162+
156163
// compile time interface checks
157164
var _ FieldError = new(fieldError)
165+
var _ TopFieldError = new(fieldError)
158166
var _ error = new(fieldError)
159167

160168
// fieldError contains a single field's validation error along
@@ -172,6 +180,7 @@ type fieldError struct {
172180
param string
173181
kind reflect.Kind
174182
typ reflect.Type
183+
top reflect.Value
175184
}
176185

177186
// Tag returns the validation tag that failed.
@@ -269,3 +278,9 @@ func (fe *fieldError) Translate(ut ut.Translator) string {
269278

270279
return fn(ut, fe)
271280
}
281+
282+
// Top returns the actual field's parent value in case needed for creating custom the error
283+
// message
284+
func (fe *fieldError) Top() reflect.Value {
285+
return fe.top
286+
}

struct_level.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func (v *validate) ReportError(field interface{}, fieldName, structFieldName, ta
133133
structfieldLen: uint8(len(structFieldName)),
134134
param: param,
135135
kind: kind,
136+
top: v.top,
136137
},
137138
)
138139
return
@@ -151,6 +152,7 @@ func (v *validate) ReportError(field interface{}, fieldName, structFieldName, ta
151152
param: param,
152153
kind: kind,
153154
typ: fv.Type(),
155+
top: v.top,
154156
},
155157
)
156158
}

validator.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
133133
structfieldLen: uint8(len(cf.name)),
134134
param: ct.param,
135135
kind: kind,
136+
top: v.top,
136137
},
137138
)
138139
return
@@ -158,6 +159,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
158159
param: ct.param,
159160
kind: kind,
160161
typ: current.Type(),
162+
top: v.top,
161163
},
162164
)
163165
return
@@ -419,6 +421,7 @@ OUTER:
419421
param: ct.param,
420422
kind: kind,
421423
typ: typ,
424+
top: v.top,
422425
},
423426
)
424427
} else {
@@ -437,6 +440,7 @@ OUTER:
437440
param: ct.param,
438441
kind: kind,
439442
typ: typ,
443+
top: v.top,
440444
},
441445
)
442446
}
@@ -477,6 +481,7 @@ OUTER:
477481
param: ct.param,
478482
kind: kind,
479483
typ: typ,
484+
top: v.top,
480485
},
481486
)
482487

validator_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func TestStructLevelInvalidError(t *testing.T) {
354354
validate.RegisterStructValidation(StructLevelInvalidError, StructLevelInvalidErr{})
355355

356356
var test StructLevelInvalidErr
357-
357+
val := reflect.ValueOf(test)
358358
err := validate.Struct(test)
359359
NotEqual(t, err, nil)
360360

@@ -370,6 +370,10 @@ func TestStructLevelInvalidError(t *testing.T) {
370370
Equal(t, fe.ActualTag(), "required")
371371
Equal(t, fe.Kind(), reflect.Invalid)
372372
Equal(t, fe.Type(), reflect.TypeOf(nil))
373+
top, ok := fe.(TopFieldError)
374+
Equal(t, ok, true)
375+
Equal(t, top.Top().Kind(), val.Kind())
376+
Equal(t, top.Top().Type().Name(), val.Type().Name())
373377
}
374378

375379
func TestNameNamespace(t *testing.T) {

0 commit comments

Comments
 (0)