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 // with other properties that may be needed for error message creation
// it complies with the FieldError interface // it complies with the FieldError interface
type fieldError struct { type fieldError struct {
tag string tag string
actualTag string actualTag string
ns string ns string
structNs string structNs string
field string fieldLen int
structField string structfieldLen int
value interface{} // field string
param string // structField string
kind reflect.Kind value interface{}
typ reflect.Type param string
kind reflect.Kind
typ reflect.Type
} }
// Tag returns the validation tag that failed. // 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 // Field returns the fields name with the tag name taking precedence over the
// fields actual name. // fields actual name.
func (fe *fieldError) Field() string { 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. // returns the fields actual name from the struct, when able to determine.
func (fe *fieldError) StructField() string { 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 // 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 // Error returns the fieldError's error message
func (fe *fieldError) Error() string { 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, v.errs = append(v.errs,
&fieldError{ &fieldError{
tag: tag, tag: tag,
actualTag: tag, actualTag: tag,
ns: v.str1, ns: v.str1,
structNs: v.str2, structNs: v.str2,
field: fieldName, fieldLen: len(fieldName),
structField: structFieldName, structfieldLen: len(structFieldName),
param: param, param: param,
kind: kind, kind: kind,
}, },
) )
return return
@ -132,16 +132,16 @@ func (v *validate) ReportError(field interface{}, fieldName, structFieldName, ta
v.errs = append(v.errs, v.errs = append(v.errs,
&fieldError{ &fieldError{
tag: tag, tag: tag,
actualTag: tag, actualTag: tag,
ns: v.str1, ns: v.str1,
structNs: v.str2, structNs: v.str2,
field: fieldName, fieldLen: len(fieldName),
structField: structFieldName, structfieldLen: len(structFieldName),
value: fv.Interface(), value: fv.Interface(),
param: param, param: param,
kind: kind, kind: kind,
typ: fv.Type(), typ: fv.Type(),
}, },
) )
} }

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

Loading…
Cancel
Save