From b2c8f66e7b6fb88f2a54fbc8f558440340e7499b Mon Sep 17 00:00:00 2001 From: joeybloggs Date: Sat, 6 Aug 2016 20:18:19 -0400 Subject: [PATCH] why pass the Field names, they already belong to the namespaces... --- errors.go | 30 +++++++++------- struct_level.go | 36 +++++++++---------- validator.go | 96 ++++++++++++++++++++++++------------------------- 3 files changed, 83 insertions(+), 79 deletions(-) diff --git a/errors.go b/errors.go index 993424a..b279b92 100644 --- a/errors.go +++ b/errors.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) } diff --git a/struct_level.go b/struct_level.go index 3440969..aaaf327 100644 --- a/struct_level.go +++ b/struct_level.go @@ -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(), }, ) } diff --git a/validator.go b/validator.go index f1cde17..c42e6db 100644 --- a/validator.go +++ b/validator.go @@ -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, }, )