converting tests

pull/256/head
joeybloggs 8 years ago
parent 7e731afde9
commit 3bf69e2bbd
  1. 26
      struct_level.go
  2. 24
      validator.go
  3. 1173
      validator_test.go

@ -97,16 +97,16 @@ func (v *validate) ExtractType(field reflect.Value) (reflect.Value, reflect.Kind
} }
// ReportError reports an error just by passing the field and tag information // ReportError reports an error just by passing the field and tag information
func (v *validate) ReportError(field interface{}, fieldName, altName, tag string) { func (v *validate) ReportError(field interface{}, fieldName, structFieldName, tag string) {
fv, kind, _ := v.extractTypeInternal(reflect.ValueOf(field), false) fv, kind, _ := v.extractTypeInternal(reflect.ValueOf(field), false)
if len(altName) == 0 { if len(structFieldName) == 0 {
altName = fieldName structFieldName = fieldName
} }
ns := append(v.slNs, fieldName...) ns := append(v.slNs, fieldName...)
nsActual := append(v.slStructNs, altName...) nsStruct := append(v.slStructNs, structFieldName...)
switch kind { switch kind {
case reflect.Invalid: case reflect.Invalid:
@ -116,10 +116,10 @@ func (v *validate) ReportError(field interface{}, fieldName, altName, tag string
tag: tag, tag: tag,
actualTag: tag, actualTag: tag,
ns: string(ns), ns: string(ns),
structNs: string(nsActual), structNs: string(nsStruct),
field: fieldName, field: fieldName,
structField: altName, structField: structFieldName,
param: "", // param: "",
kind: kind, kind: kind,
}, },
) )
@ -131,11 +131,11 @@ func (v *validate) ReportError(field interface{}, fieldName, altName, tag string
tag: tag, tag: tag,
actualTag: tag, actualTag: tag,
ns: string(ns), ns: string(ns),
structNs: string(nsActual), structNs: string(nsStruct),
field: fieldName, field: fieldName,
structField: altName, structField: structFieldName,
value: fv.Interface(), value: fv.Interface(),
param: "", // param: "",
kind: kind, kind: kind,
typ: fv.Type(), typ: fv.Type(),
}, },
@ -146,15 +146,15 @@ func (v *validate) ReportError(field interface{}, fieldName, altName, tag string
// ReportValidationErrors reports ValidationErrors obtained from running validations within the Struct Level validation. // ReportValidationErrors reports ValidationErrors obtained from running validations within the Struct Level validation.
// //
// NOTE: this function prepends the current namespace to the relative ones. // NOTE: this function prepends the current namespace to the relative ones.
func (v *validate) ReportValidationErrors(relativeNamespace, relativeActualNamespace string, errs ValidationErrors) { func (v *validate) ReportValidationErrors(relativeNamespace, relativeStructNamespace string, errs ValidationErrors) {
var err *fieldError var err *fieldError
for i := 0; i < len(errs); i++ { for i := 0; i < len(errs); i++ {
err = errs[i].(*fieldError) err = errs[i].(*fieldError)
err.ns = string(append(append(v.slNs, err.ns...), err.field...)) err.ns = string(append(append(v.slNs, relativeNamespace...), err.ns...))
err.structNs = string(append(append(v.slStructNs, err.structNs...), err.structField...)) err.structNs = string(append(append(v.slStructNs, relativeStructNamespace...), err.structNs...))
v.errs = append(v.errs, err) v.errs = append(v.errs, err)
} }

@ -57,7 +57,7 @@ func (v *validate) validateStruct(parent reflect.Value, current reflect.Value, t
if v.isPartial { if v.isPartial {
_, ok = v.includeExclude[string(append(ns, f.Name...))] _, ok = v.includeExclude[string(append(structNs, f.Name...))]
if (ok && v.hasExcludes) || (!ok && !v.hasExcludes) { if (ok && v.hasExcludes) || (!ok && !v.hasExcludes) {
continue continue
@ -110,8 +110,8 @@ func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns
&fieldError{ &fieldError{
tag: ct.aliasTag, tag: ct.aliasTag,
actualTag: ct.tag, actualTag: ct.tag,
ns: string(append(ns, cf.Name...)), ns: string(append(ns, cf.AltName...)),
structNs: string(append(structNs, cf.AltName...)), structNs: string(append(structNs, cf.Name...)),
field: cf.AltName, field: cf.AltName,
structField: cf.Name, structField: cf.Name,
param: ct.param, param: ct.param,
@ -126,8 +126,8 @@ func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns
&fieldError{ &fieldError{
tag: ct.aliasTag, tag: ct.aliasTag,
actualTag: ct.tag, actualTag: ct.tag,
ns: string(append(ns, cf.Name...)), ns: string(append(ns, cf.AltName...)),
structNs: string(append(structNs, cf.AltName...)), structNs: string(append(structNs, cf.Name...)),
field: cf.AltName, field: cf.AltName,
structField: cf.Name, structField: cf.Name,
value: current.Interface(), value: current.Interface(),
@ -154,7 +154,7 @@ func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns
return return
} }
v.validateStruct(current, current, typ, append(append(ns, cf.Name...), '.'), append(append(structNs, cf.AltName...), '.'), ct) v.validateStruct(current, current, typ, append(append(ns, cf.AltName...), '.'), append(append(structNs, cf.Name...), '.'), ct)
return return
} }
} }
@ -256,8 +256,8 @@ OUTER:
&fieldError{ &fieldError{
tag: ct.aliasTag, tag: ct.aliasTag,
actualTag: ct.actualAliasTag, actualTag: ct.actualAliasTag,
ns: string(append(ns, cf.Name...)), ns: string(append(ns, cf.AltName...)),
structNs: string(append(structNs, cf.AltName...)), structNs: string(append(structNs, cf.Name...)),
field: cf.AltName, field: cf.AltName,
structField: cf.Name, structField: cf.Name,
value: current.Interface(), value: current.Interface(),
@ -273,8 +273,8 @@ OUTER:
&fieldError{ &fieldError{
tag: errTag[1:], tag: errTag[1:],
actualTag: errTag[1:], actualTag: errTag[1:],
ns: string(append(ns, cf.Name...)), ns: string(append(ns, cf.AltName...)),
structNs: string(append(structNs, cf.AltName...)), structNs: string(append(structNs, cf.Name...)),
field: cf.AltName, field: cf.AltName,
structField: cf.Name, structField: cf.Name,
value: current.Interface(), value: current.Interface(),
@ -304,8 +304,8 @@ OUTER:
&fieldError{ &fieldError{
tag: ct.aliasTag, tag: ct.aliasTag,
actualTag: ct.tag, actualTag: ct.tag,
ns: string(append(ns, cf.Name...)), ns: string(append(ns, cf.AltName...)),
structNs: string(append(structNs, cf.AltName...)), structNs: string(append(structNs, cf.Name...)),
field: cf.AltName, field: cf.AltName,
structField: cf.Name, structField: cf.Name,
value: current.Interface(), value: current.Interface(),

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save