Skip to content

Commit 9815049

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

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
@@ -8959,3 +8959,22 @@ func TestRequiredWithoutAllPointers(t *testing.T) {
89598959
errs = val.Struct(lookup)
89608960
Equal(t, errs, nil)
89618961
}
8962+
8963+
func TestGetTag(t *testing.T) {
8964+
var tag string
8965+
8966+
type Test struct {
8967+
String string `validate:"mytag"`
8968+
}
8969+
8970+
val := New()
8971+
val.RegisterValidation("mytag", func(fl FieldLevel) bool {
8972+
tag = fl.GetTag()
8973+
return true
8974+
})
8975+
8976+
var test Test
8977+
errs := val.Struct(test)
8978+
Equal(t, errs, nil)
8979+
Equal(t, tag, "mytag")
8980+
}

0 commit comments

Comments
 (0)