Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add reflect struct field on validate error #979

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
22 changes: 12 additions & 10 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ type cStruct struct {
}

type cField struct {
idx int
name string
altName string
namesEqual bool
cTags *cTag
idx int
name string
altName string
namesEqual bool
cTags *cTag
structField reflect.StructField
}

type cTag struct {
Expand Down Expand Up @@ -161,11 +162,12 @@ func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStr
}

cs.fields = append(cs.fields, &cField{
idx: i,
name: fld.Name,
altName: customName,
cTags: ctag,
namesEqual: fld.Name == customName,
idx: i,
name: fld.Name,
altName: customName,
cTags: ctag,
namesEqual: fld.Name == customName,
structField: fld,
})
}
v.structCache.Set(typ, cs)
Expand Down
9 changes: 9 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ type FieldError interface {
// eg. time.Time's type is time.Time
Type() reflect.Type

ReflectStructField() reflect.StructField

// Translate returns the FieldError's translated error
// from the provided 'ut.Translator' and registered 'TranslationFunc'
//
Expand Down Expand Up @@ -176,6 +178,7 @@ type fieldError struct {
param string
kind reflect.Kind
typ reflect.Type
structField reflect.StructField
}

// Tag returns the validation tag that failed.
Expand Down Expand Up @@ -224,6 +227,12 @@ func (fe *fieldError) StructField() string {
return fe.structNs[len(fe.structNs)-int(fe.structfieldLen):]
}

// ReflectStructField get reflect struct field
func (fe *fieldError) ReflectStructField() reflect.StructField {
// return fe.structField
return fe.structField
}

// Value returns the actual field's value in case needed for creating the error
// message
func (fe *fieldError) Value() interface{} {
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,
structField: cf.structField,
},
)
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(),
structField: cf.structField,
},
)
return
Expand Down Expand Up @@ -415,6 +417,7 @@ OUTER:
param: ct.param,
kind: kind,
typ: typ,
structField: cf.structField,
},
)

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

Expand Down
22 changes: 22 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13622,6 +13622,28 @@ func TestMultiOrOperatorGroup(t *testing.T) {
}
}

type testTag struct {
CreatedAt *time.Time `validate:"required" langKey:"test.created_at"`
String string `validate:"required" langKey:"test.string"`
Int int `validate:"required" langKey:"test.int"`
Uint uint `validate:"required" langKey:"test.uint"`
Float float64 `validate:"required" langKey:"test.float"`
Array []string `validate:"required" langKey:"test.array"`
}

func TestReflectStructField(t *testing.T) {
validate := New()
var test testTag
err := validate.Struct(test)
NotEqual(t, err, nil)

errs, ok := err.(ValidationErrors)
Equal(t, ok, true)

fe := errs[0]
Equal(t, fe.ReflectStructField().Tag.Get("langKey"), "test.created_at")
}

func TestCronExpressionValidation(t *testing.T) {
tests := []struct {
value string `validate:"cron"`
Expand Down