Skip to content

Commit

Permalink
Add test + docs for FieldLevel.GetTag
Browse files Browse the repository at this point in the history
  • Loading branch information
deankarn committed Dec 25, 2019
1 parent f8a081f commit c2546fb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion field_level.go
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
19 changes: 19 additions & 0 deletions validator_test.go
Expand Up @@ -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")
}

0 comments on commit c2546fb

Please sign in to comment.