add reflect struct field

pull/979/head
Thanakrit Nawasaykaew 2 years ago
parent 9e2ea40380
commit 3714556d17
  1. 24
      cache.go
  2. 9
      errors.go
  3. 6
      validator.go

@ -75,11 +75,12 @@ type cStruct struct {
}
type cField struct {
idx int
name string
altName string
namesEqual bool
cTags *cTag
idx int
name string
altName string
namesEqual bool
cTags *cTag
structField reflect.StructField
}
type cTag struct {
@ -120,7 +121,7 @@ func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStr
var fld reflect.StructField
var tag string
var customName string
for i := 0; i < numFields; i++ {
fld = typ.Field(i)
@ -160,11 +161,12 @@ func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStr
}
cs.fields = append(cs.fields, &cField{
idx: i,
name: fld.Name,
altName: customName,
cTags: ctag,
namesEqual: fld.Name == customName,
idx: i,
name: fld.Name,
altName: customName,
cTags: ctag,
namesEqual: fld.Name == customName,
structField: fld,
})
}
v.structCache.Set(typ, cs)

@ -149,6 +149,8 @@ type FieldError interface {
// eg. time.Time's type is time.Time
Type() reflect.Type
ReflectStructField() reflect.StructField
// Translate returns the FieldError's translated error
// from the provided 'ut.Translator' and registered 'TranslationFunc'
//
@ -179,6 +181,7 @@ type fieldError struct {
param string
kind reflect.Kind
typ reflect.Type
structField reflect.StructField
}
// Tag returns the validation tag that failed.
@ -227,6 +230,12 @@ func (fe *fieldError) StructField() string {
return fe.structNs[len(fe.structNs)-int(fe.structfieldLen):]
}
// ReflectStructField get reflect struct field
func (fe *fieldError) ReflectStructField() reflect.StructField {
// return fe.structField
return fe.structField
}
// Value returns the actual field's value in case needed for creating the error
// message
func (fe *fieldError) Value() interface{} {

@ -129,6 +129,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
structfieldLen: uint8(len(cf.name)),
param: ct.param,
kind: kind,
structField: cf.structField,
},
)
return
@ -154,6 +155,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
param: ct.param,
kind: kind,
typ: current.Type(),
structField: cf.structField,
},
)
return
@ -199,6 +201,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
param: ct.param,
kind: kind,
typ: typ,
structField: cf.structField,
},
)
return
@ -413,6 +416,7 @@ OUTER:
param: ct.param,
kind: kind,
typ: typ,
structField: cf.structField,
},
)
@ -433,6 +437,7 @@ OUTER:
param: ct.param,
kind: kind,
typ: typ,
structField: cf.structField,
},
)
}
@ -474,6 +479,7 @@ OUTER:
param: ct.param,
kind: kind,
typ: typ,
structField: cf.structField,
},
)

Loading…
Cancel
Save