diff --git a/field_level.go b/field_level.go index c5b62cc..f0e2a9a 100644 --- a/field_level.go +++ b/field_level.go @@ -25,6 +25,7 @@ type FieldLevel interface { // returns param for validation against current field Param() string + // GetTag returns the current validations tag name GetTag() string // ExtractType gets the actual underlying type of field value. @@ -74,7 +75,7 @@ func (v *validate) FieldName() string { return v.cf.altName } -// GetTag returns the tag name of field +// GetTag returns the current validations tag name func (v *validate) GetTag() string { return v.ct.tag } diff --git a/validator_test.go b/validator_test.go index fcad8f9..2c8b96b 100644 --- a/validator_test.go +++ b/validator_test.go @@ -8984,3 +8984,22 @@ func TestRequiredWithoutAllPointers(t *testing.T) { errs = val.Struct(lookup) Equal(t, errs, nil) } + +func TestGetTag(t *testing.T) { + var tag string + + type Test struct { + String string `validate:"mytag"` + } + + val := New() + val.RegisterValidation("mytag", func(fl FieldLevel) bool { + tag = fl.GetTag() + return true + }) + + var test Test + errs := val.Struct(test) + Equal(t, errs, nil) + Equal(t, tag, "mytag") +}