added two new methods. CTag and CField for FieldLevel #282

pull/283/head
s4l1h 8 years ago
parent 27158c7a84
commit 4476beeb56
  1. 15
      cache.go
  2. 14
      field_level.go
  3. 5
      validator.go
  4. 38
      validator_test.go

@ -82,6 +82,16 @@ type cField struct {
cTags *cTag
}
// Name return field name
func (cf *cField) Name() string {
return cf.name
}
// AltName return field altname
func (cf *cField) AltName() string {
return cf.altName
}
type cTag struct {
tag string
aliasTag string
@ -94,6 +104,11 @@ type cTag struct {
next *cTag
}
// Param return tag param
func (ct *cTag) Param() string {
return ct.param
}
func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStruct {
v.structCache.lock.Lock()

@ -31,6 +31,10 @@ type FieldLevel interface {
// NOTE: when not successful ok will be false, this can happen when a nested struct is nil and so the field
// could not be retrieved because it didn't exist.
GetStructFieldOK() (reflect.Value, reflect.Kind, bool)
// return cache field
CField() *cField
// return cache tag
CTag() *cTag
}
var _ FieldLevel = new(validate)
@ -49,3 +53,13 @@ func (v *validate) Param() string {
func (v *validate) GetStructFieldOK() (reflect.Value, reflect.Kind, bool) {
return v.getStructFieldOKInternal(v.slflParent, v.flParam)
}
// Param returns cache field
func (v *validate) CField() *cField {
return v.cf
}
// Param returns cache tag
func (v *validate) CTag() *cTag {
return v.ct
}

@ -25,7 +25,8 @@ type validate struct {
flField reflect.Value
flParam string
fldIsPointer bool
cf *cField
ct *cTag
// misc reusable values
misc []byte
str1 string
@ -98,6 +99,8 @@ func (v *validate) validateStruct(parent reflect.Value, current reflect.Value, t
// traverseField validates any field, be it a struct or single field, ensures it's validity and passes it along to be validated via it's tag options
func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns []byte, structNs []byte, cf *cField, ct *cTag) {
v.cf = cf
v.ct = ct
var typ reflect.Type
var kind reflect.Kind

@ -7013,3 +7013,41 @@ func TestMapStructNamespace(t *testing.T) {
Equal(t, len(ve), 1)
AssertError(t, errs, "children[1].name", "Children[1].Name", "name", "Name", "required")
}
func TestFieldLevel_CField(t *testing.T) {
validate := New()
validate.RegisterValidation("notEqualDesc", func(fl FieldLevel) bool {
return fl.CField().Name() != "Desc" // check field name is not Desc"
})
type Foo struct {
Desc string `json:"name" validate:"notEqualDesc"` // field name need not equal Desc
}
errs := validate.Struct(&Foo{})
NotEqual(t, errs, nil)
ve := errs.(ValidationErrors)
Equal(t, len(ve), 1)
}
func TestFieldLevel_CTag(t *testing.T) {
validate := New()
validate.RegisterValidation("fieldNameEqualParam", func(fl FieldLevel) bool {
return fl.CField().Name() == fl.CTag().Param() // check field name equal tag param"
})
type Foo struct {
Desc string `json:"name" validate:"fieldNameEqualParam=Asc"` // field name need equal tag param
}
errs := validate.Struct(&Foo{})
NotEqual(t, errs, nil)
ve := errs.(ValidationErrors)
Equal(t, len(ve), 1)
}

Loading…
Cancel
Save