Add test + docs for FieldLevel.GetTag

pull/560/head
Dean Karn 5 years ago
parent f8a081fd83
commit c2546fb355
  1. 3
      field_level.go
  2. 19
      validator_test.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
}

@ -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")
}

Loading…
Cancel
Save