converting tests

pull/256/head
joeybloggs 8 years ago
parent 7e731afde9
commit 3bf69e2bbd
  1. 32
      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
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)
if len(altName) == 0 {
altName = fieldName
if len(structFieldName) == 0 {
structFieldName = fieldName
}
ns := append(v.slNs, fieldName...)
nsActual := append(v.slStructNs, altName...)
nsStruct := append(v.slStructNs, structFieldName...)
switch kind {
case reflect.Invalid:
@ -116,11 +116,11 @@ func (v *validate) ReportError(field interface{}, fieldName, altName, tag string
tag: tag,
actualTag: tag,
ns: string(ns),
structNs: string(nsActual),
structNs: string(nsStruct),
field: fieldName,
structField: altName,
param: "",
kind: kind,
structField: structFieldName,
// param: "",
kind: kind,
},
)
@ -131,13 +131,13 @@ func (v *validate) ReportError(field interface{}, fieldName, altName, tag string
tag: tag,
actualTag: tag,
ns: string(ns),
structNs: string(nsActual),
structNs: string(nsStruct),
field: fieldName,
structField: altName,
structField: structFieldName,
value: fv.Interface(),
param: "",
kind: kind,
typ: fv.Type(),
// param: "",
kind: kind,
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.
//
// 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
for i := 0; i < len(errs); i++ {
err = errs[i].(*fieldError)
err.ns = string(append(append(v.slNs, err.ns...), err.field...))
err.structNs = string(append(append(v.slStructNs, err.structNs...), err.structField...))
err.ns = string(append(append(v.slNs, relativeNamespace...), err.ns...))
err.structNs = string(append(append(v.slStructNs, relativeStructNamespace...), err.structNs...))
v.errs = append(v.errs, err)
}

@ -57,7 +57,7 @@ func (v *validate) validateStruct(parent reflect.Value, current reflect.Value, t
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) {
continue
@ -110,8 +110,8 @@ func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns
&fieldError{
tag: ct.aliasTag,
actualTag: ct.tag,
ns: string(append(ns, cf.Name...)),
structNs: string(append(structNs, cf.AltName...)),
ns: string(append(ns, cf.AltName...)),
structNs: string(append(structNs, cf.Name...)),
field: cf.AltName,
structField: cf.Name,
param: ct.param,
@ -126,8 +126,8 @@ func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns
&fieldError{
tag: ct.aliasTag,
actualTag: ct.tag,
ns: string(append(ns, cf.Name...)),
structNs: string(append(structNs, cf.AltName...)),
ns: string(append(ns, cf.AltName...)),
structNs: string(append(structNs, cf.Name...)),
field: cf.AltName,
structField: cf.Name,
value: current.Interface(),
@ -154,7 +154,7 @@ func (v *validate) traverseField(parent reflect.Value, current reflect.Value, ns
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
}
}
@ -256,8 +256,8 @@ OUTER:
&fieldError{
tag: ct.aliasTag,
actualTag: ct.actualAliasTag,
ns: string(append(ns, cf.Name...)),
structNs: string(append(structNs, cf.AltName...)),
ns: string(append(ns, cf.AltName...)),
structNs: string(append(structNs, cf.Name...)),
field: cf.AltName,
structField: cf.Name,
value: current.Interface(),
@ -273,8 +273,8 @@ OUTER:
&fieldError{
tag: errTag[1:],
actualTag: errTag[1:],
ns: string(append(ns, cf.Name...)),
structNs: string(append(structNs, cf.AltName...)),
ns: string(append(ns, cf.AltName...)),
structNs: string(append(structNs, cf.Name...)),
field: cf.AltName,
structField: cf.Name,
value: current.Interface(),
@ -304,8 +304,8 @@ OUTER:
&fieldError{
tag: ct.aliasTag,
actualTag: ct.tag,
ns: string(append(ns, cf.Name...)),
structNs: string(append(structNs, cf.AltName...)),
ns: string(append(ns, cf.AltName...)),
structNs: string(append(structNs, cf.Name...)),
field: cf.AltName,
structField: cf.Name,
value: current.Interface(),

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