Skip to content

Commit c2546fb

Browse files
author
Dean Karn
committed
Add test + docs for FieldLevel.GetTag
1 parent f8a081f commit c2546fb

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

field_level.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type FieldLevel interface {
2525
// returns param for validation against current field
2626
Param() string
2727

28+
// GetTag returns the current validations tag name
2829
GetTag() string
2930

3031
// ExtractType gets the actual underlying type of field value.
@@ -74,7 +75,7 @@ func (v *validate) FieldName() string {
7475
return v.cf.altName
7576
}
7677

77-
// GetTag returns the tag name of field
78+
// GetTag returns the current validations tag name
7879
func (v *validate) GetTag() string {
7980
return v.ct.tag
8081
}

validator_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8984,3 +8984,22 @@ func TestRequiredWithoutAllPointers(t *testing.T) {
89848984
errs = val.Struct(lookup)
89858985
Equal(t, errs, nil)
89868986
}
8987+
8988+
func TestGetTag(t *testing.T) {
8989+
var tag string
8990+
8991+
type Test struct {
8992+
String string `validate:"mytag"`
8993+
}
8994+
8995+
val := New()
8996+
val.RegisterValidation("mytag", func(fl FieldLevel) bool {
8997+
tag = fl.GetTag()
8998+
return true
8999+
})
9000+
9001+
var test Test
9002+
errs := val.Struct(test)
9003+
Equal(t, errs, nil)
9004+
Equal(t, tag, "mytag")
9005+
}

0 commit comments

Comments
 (0)