Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ _testmain.go
cover.html
README.html
.idea
.vscode
15 changes: 15 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,16 @@ type FieldError interface {
Error() string
}

type TopFieldError interface {
FieldError

// Top returns the actual field's parent value in case needed for creating custom the error
Top() reflect.Value
}

// compile time interface checks
var _ FieldError = new(fieldError)
var _ TopFieldError = new(fieldError)
var _ error = new(fieldError)

// fieldError contains a single field's validation error along
Expand All @@ -176,6 +184,7 @@ type fieldError struct {
param string
kind reflect.Kind
typ reflect.Type
top reflect.Value
}

// Tag returns the validation tag that failed.
Expand Down Expand Up @@ -274,3 +283,9 @@ func (fe *fieldError) Translate(ut ut.Translator) string {

return fn(ut, fe)
}

// Top returns the actual field's parent value in case needed for creating custom the error
// message
func (fe *fieldError) Top() reflect.Value {
return fe.top
}
2 changes: 2 additions & 0 deletions struct_level.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func (v *validate) ReportError(field interface{}, fieldName, structFieldName, ta
structfieldLen: uint8(len(structFieldName)),
param: param,
kind: kind,
top: v.top,
},
)
return
Expand All @@ -153,6 +154,7 @@ func (v *validate) ReportError(field interface{}, fieldName, structFieldName, ta
param: param,
kind: kind,
typ: fv.Type(),
top: v.top,
},
)
}
Expand Down
5 changes: 5 additions & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
structfieldLen: uint8(len(cf.name)),
param: ct.param,
kind: kind,
top: v.top,
},
)
return
Expand All @@ -161,6 +162,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
param: ct.param,
kind: kind,
typ: current.Type(),
top: v.top,
},
)
return
Expand Down Expand Up @@ -415,6 +417,7 @@ OUTER:
param: ct.param,
kind: kind,
typ: typ,
top: v.top,
},
)

Expand All @@ -435,6 +438,7 @@ OUTER:
param: ct.param,
kind: kind,
typ: typ,
top: v.top,
},
)
}
Expand Down Expand Up @@ -475,6 +479,7 @@ OUTER:
param: ct.param,
kind: kind,
typ: typ,
top: v.top,
},
)

Expand Down
6 changes: 5 additions & 1 deletion validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func TestStructLevelInvalidError(t *testing.T) {
validate.RegisterStructValidation(StructLevelInvalidError, StructLevelInvalidErr{})

var test StructLevelInvalidErr

val := reflect.ValueOf(test)
err := validate.Struct(test)
NotEqual(t, err, nil)

Expand All @@ -371,6 +371,10 @@ func TestStructLevelInvalidError(t *testing.T) {
Equal(t, fe.ActualTag(), "required")
Equal(t, fe.Kind(), reflect.Invalid)
Equal(t, fe.Type(), reflect.TypeOf(nil))
top, ok := fe.(TopFieldError)
Equal(t, ok, true)
Equal(t, top.Top().Kind(), val.Kind())
Equal(t, top.Top().Type().Name(), val.Type().Name())
}

func TestNameNamespace(t *testing.T) {
Expand Down