finalized array error handling

for #78
pull/82/head
joeybloggs 10 years ago
parent 1ba858eec1
commit 689d3e9989
  1. 11
      validator.go
  2. 106
      validator_test.go

@ -209,21 +209,14 @@ func (e *StructErrors) Error() string {
for _, err := range e.Errors {
buff.WriteString(err.Error())
buff.WriteString("\n")
}
var i uint64
for _, err := range e.StructErrors {
if i != 0 {
buff.WriteString("\n")
}
buff.WriteString(err.Error())
i++
}
return buff.String()
return strings.TrimSpace(buff.String())
}
// Flatten flattens the StructErrors hierarchical structure into a flat namespace style field name

@ -231,49 +231,49 @@ func TestMapDiveValidation(t *testing.T) {
func TestArrayDiveValidation(t *testing.T) {
// type Test struct {
// Errs []string `validate:"gt=0,dive,required"`
// }
// test := &Test{
// Errs: []string{"ok", "", "ok"},
// }
// errs := validate.Struct(test)
// NotEqual(t, errs, nil)
// Equal(t, len(errs.Errors), 1)
// fieldErr, ok := errs.Errors["Errs"]
// Equal(t, ok, true)
// Equal(t, fieldErr.IsPlaceholderErr, true)
// Equal(t, fieldErr.IsSliceOrArray, true)
// Equal(t, len(fieldErr.SliceOrArrayErrs), 1)
// innerErr, ok := fieldErr.SliceOrArrayErrs[1].(*FieldError)
// Equal(t, ok, true)
// Equal(t, innerErr.Tag, required)
// Equal(t, innerErr.IsPlaceholderErr, false)
// Equal(t, innerErr.Field, "Errs")
// test = &Test{
// Errs: []string{"ok", "ok", ""},
// }
// errs = validate.Struct(test)
// NotEqual(t, errs, nil)
// Equal(t, len(errs.Errors), 1)
// fieldErr, ok = errs.Errors["Errs"]
// Equal(t, ok, true)
// Equal(t, fieldErr.IsPlaceholderErr, true)
// Equal(t, fieldErr.IsSliceOrArray, true)
// Equal(t, len(fieldErr.SliceOrArrayErrs), 1)
// innerErr, ok = fieldErr.SliceOrArrayErrs[2].(*FieldError)
// Equal(t, ok, true)
// Equal(t, innerErr.Tag, required)
// Equal(t, innerErr.IsPlaceholderErr, false)
// Equal(t, innerErr.Field, "Errs")
type Test struct {
Errs []string `validate:"gt=0,dive,required"`
}
test := &Test{
Errs: []string{"ok", "", "ok"},
}
errs := validate.Struct(test)
NotEqual(t, errs, nil)
Equal(t, len(errs.Errors), 1)
fieldErr, ok := errs.Errors["Errs"]
Equal(t, ok, true)
Equal(t, fieldErr.IsPlaceholderErr, true)
Equal(t, fieldErr.IsSliceOrArray, true)
Equal(t, len(fieldErr.SliceOrArrayErrs), 1)
innerErr, ok := fieldErr.SliceOrArrayErrs[1].(*FieldError)
Equal(t, ok, true)
Equal(t, innerErr.Tag, required)
Equal(t, innerErr.IsPlaceholderErr, false)
Equal(t, innerErr.Field, "Errs[1]")
test = &Test{
Errs: []string{"ok", "ok", ""},
}
errs = validate.Struct(test)
NotEqual(t, errs, nil)
Equal(t, len(errs.Errors), 1)
fieldErr, ok = errs.Errors["Errs"]
Equal(t, ok, true)
Equal(t, fieldErr.IsPlaceholderErr, true)
Equal(t, fieldErr.IsSliceOrArray, true)
Equal(t, len(fieldErr.SliceOrArrayErrs), 1)
innerErr, ok = fieldErr.SliceOrArrayErrs[2].(*FieldError)
Equal(t, ok, true)
Equal(t, innerErr.Tag, required)
Equal(t, innerErr.IsPlaceholderErr, false)
Equal(t, innerErr.Field, "Errs[2]")
type TestMultiDimensional struct {
Errs [][]string `validate:"gt=0,dive,dive,required"`
@ -283,25 +283,17 @@ func TestArrayDiveValidation(t *testing.T) {
errArray = append(errArray, []string{"ok", "", ""})
errArray = append(errArray, []string{"ok", "", ""})
// fmt.Println(len(errArray))
// errArray = append(errArray, []string{"", "ok", "ok"})
// errArray = append(errArray, []string{"", "", "ok"})
// errArray = append(errArray, []string{"", "", "ok"})
tm := &TestMultiDimensional{
Errs: errArray,
}
errs := validate.Struct(tm)
fmt.Println(errs)
// validate.Struct(tm)
// fmt.Printf("%#v\n", errs.Errors["Errs"].SliceOrArrayErrs)
errs = validate.Struct(tm)
NotEqual(t, errs, nil)
Equal(t, len(errs.Errors), 1)
fieldErr, ok := errs.Errors["Errs"]
fieldErr, ok = errs.Errors["Errs"]
Equal(t, ok, true)
Equal(t, fieldErr.IsPlaceholderErr, true)
Equal(t, fieldErr.IsSliceOrArray, true)
@ -319,14 +311,6 @@ func TestArrayDiveValidation(t *testing.T) {
Equal(t, innerSliceError1.Tag, required)
Equal(t, innerSliceError1.IsSliceOrArray, false)
Equal(t, len(innerSliceError1.SliceOrArrayErrs), 0)
// fmt.Println(fieldErr.SliceOrArrayErrs)
// Equal(t, fieldErr.IsPlaceholderErr, true)
// Equal(t, fieldErr.IsSliceOrArray, true)
// Equal(t, len(fieldErr.SliceOrArrayErrs), 3)
// fmt.Println(fieldErr.SliceOrArrayErrs)
// fmt.Println(len(fieldErr.SliceOrArrayErrs))
}
func TestNilStructPointerValidation(t *testing.T) {

Loading…
Cancel
Save