Add test + docs for FieldLevel.GetTag

v9
Dean Karn 5 years ago
parent f1d020490a
commit 98150497be
  1. 3
      field_level.go
  2. 19
      validator_test.go

@ -25,6 +25,7 @@ type FieldLevel interface {
// returns param for validation against current field // returns param for validation against current field
Param() string Param() string
// GetTag returns the current validations tag name
GetTag() string GetTag() string
// ExtractType gets the actual underlying type of field value. // ExtractType gets the actual underlying type of field value.
@ -74,7 +75,7 @@ func (v *validate) FieldName() string {
return v.cf.altName return v.cf.altName
} }
// GetTag returns the tag name of field // GetTag returns the current validations tag name
func (v *validate) GetTag() string { func (v *validate) GetTag() string {
return v.ct.tag return v.ct.tag
} }

@ -8959,3 +8959,22 @@ func TestRequiredWithoutAllPointers(t *testing.T) {
errs = val.Struct(lookup) errs = val.Struct(lookup)
Equal(t, errs, nil) 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