why pass the Field names, they already belong to the namespaces...

pull/256/head
joeybloggs 8 years ago
parent ea09db9292
commit b2c8f66e7b
  1. 30
      errors.go
  2. 36
      struct_level.go
  3. 96
      validator.go

@ -128,16 +128,18 @@ var _ error = new(fieldError)
// with other properties that may be needed for error message creation
// it complies with the FieldError interface
type fieldError struct {
tag string
actualTag string
ns string
structNs string
field string
structField string
value interface{}
param string
kind reflect.Kind
typ reflect.Type
tag string
actualTag string
ns string
structNs string
fieldLen int
structfieldLen int
// field string
// structField string
value interface{}
param string
kind reflect.Kind
typ reflect.Type
}
// Tag returns the validation tag that failed.
@ -166,12 +168,14 @@ func (fe *fieldError) StructNamespace() string {
// Field returns the fields name with the tag name taking precedence over the
// fields actual name.
func (fe *fieldError) Field() string {
return fe.field
// return fe.field
return fe.ns[len(fe.ns)-fe.fieldLen:]
}
// returns the fields actual name from the struct, when able to determine.
func (fe *fieldError) StructField() string {
return fe.structField
// return fe.structField
return fe.structNs[len(fe.structNs)-fe.structfieldLen:]
}
// Value returns the actual fields value in case needed for creating the error
@ -198,5 +202,5 @@ func (fe *fieldError) Type() reflect.Type {
// Error returns the fieldError's error message
func (fe *fieldError) Error() string {
return fmt.Sprintf(fieldErrMsg, fe.ns, fe.field, fe.tag)
return fmt.Sprintf(fieldErrMsg, fe.ns, fe.Field(), fe.tag)
}

@ -117,14 +117,14 @@ func (v *validate) ReportError(field interface{}, fieldName, structFieldName, ta
v.errs = append(v.errs,
&fieldError{
tag: tag,
actualTag: tag,
ns: v.str1,
structNs: v.str2,
field: fieldName,
structField: structFieldName,
param: param,
kind: kind,
tag: tag,
actualTag: tag,
ns: v.str1,
structNs: v.str2,
fieldLen: len(fieldName),
structfieldLen: len(structFieldName),
param: param,
kind: kind,
},
)
return
@ -132,16 +132,16 @@ func (v *validate) ReportError(field interface{}, fieldName, structFieldName, ta
v.errs = append(v.errs,
&fieldError{
tag: tag,
actualTag: tag,
ns: v.str1,
structNs: v.str2,
field: fieldName,
structField: structFieldName,
value: fv.Interface(),
param: param,
kind: kind,
typ: fv.Type(),
tag: tag,
actualTag: tag,
ns: v.str1,
structNs: v.str2,
fieldLen: len(fieldName),
structfieldLen: len(structFieldName),
value: fv.Interface(),
param: param,
kind: kind,
typ: fv.Type(),
},
)
}

@ -113,14 +113,14 @@ func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns
v.errs = append(v.errs,
&fieldError{
tag: ct.aliasTag,
actualTag: ct.tag,
ns: v.str1,
structNs: v.str2,
field: cf.altName,
structField: cf.name,
param: ct.param,
kind: kind,
tag: ct.aliasTag,
actualTag: ct.tag,
ns: v.str1,
structNs: v.str2,
fieldLen: len(cf.altName),
structfieldLen: len(cf.name),
param: ct.param,
kind: kind,
},
)
@ -129,16 +129,16 @@ func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns
v.errs = append(v.errs,
&fieldError{
tag: ct.aliasTag,
actualTag: ct.tag,
ns: v.str1,
structNs: v.str2,
field: cf.altName,
structField: cf.name,
value: current.Interface(),
param: ct.param,
kind: kind,
typ: current.Type(),
tag: ct.aliasTag,
actualTag: ct.tag,
ns: v.str1,
structNs: v.str2,
fieldLen: len(cf.altName),
structfieldLen: len(cf.name),
value: current.Interface(),
param: ct.param,
kind: kind,
typ: current.Type(),
},
)
@ -323,16 +323,16 @@ OUTER:
v.errs = append(v.errs,
&fieldError{
tag: ct.aliasTag,
actualTag: ct.actualAliasTag,
ns: v.str1,
structNs: v.str2,
field: cf.altName,
structField: cf.name,
value: current.Interface(),
param: ct.param,
kind: kind,
typ: typ,
tag: ct.aliasTag,
actualTag: ct.actualAliasTag,
ns: v.str1,
structNs: v.str2,
fieldLen: len(cf.altName),
structfieldLen: len(cf.name),
value: current.Interface(),
param: ct.param,
kind: kind,
typ: typ,
},
)
@ -342,16 +342,16 @@ OUTER:
v.errs = append(v.errs,
&fieldError{
tag: tVal,
actualTag: tVal,
ns: v.str1,
structNs: v.str2,
field: cf.altName,
structField: cf.name,
value: current.Interface(),
param: ct.param,
kind: kind,
typ: typ,
tag: tVal,
actualTag: tVal,
ns: v.str1,
structNs: v.str2,
fieldLen: len(cf.altName),
structfieldLen: len(cf.name),
value: current.Interface(),
param: ct.param,
kind: kind,
typ: typ,
},
)
}
@ -381,16 +381,16 @@ OUTER:
v.errs = append(v.errs,
&fieldError{
tag: ct.aliasTag,
actualTag: ct.tag,
ns: v.str1,
structNs: v.str2,
field: cf.altName,
structField: cf.name,
value: current.Interface(),
param: ct.param,
kind: kind,
typ: typ,
tag: ct.aliasTag,
actualTag: ct.tag,
ns: v.str1,
structNs: v.str2,
fieldLen: len(cf.altName),
structfieldLen: len(cf.name),
value: current.Interface(),
param: ct.param,
kind: kind,
typ: typ,
},
)

Loading…
Cancel
Save