Skip to content

Commit

Permalink
Merge pull request #22 from hexdigest/master
Browse files Browse the repository at this point in the history
Don't wrap error from custom validator to unknown_field
  • Loading branch information
hexdigest authored Aug 31, 2018
2 parents 540a20a + 0dd8f11 commit 49868b2
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 59 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
language: go

go:
- 1.7
- 1.X
- tip

install:
- go get github.com/pkg/errors
- go get github.com/stretchr/testify/assert
- go get github.com/stretchr/testify/require
- go get github.com/stretchr/testify/require
34 changes: 21 additions & 13 deletions errlist/errlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/pkg/errors"
)

type List []Field
type List []error

// Error implements `error` interface
func (e List) Error() string {
Expand All @@ -31,7 +31,8 @@ func (e *List) Add(err error) *List {

list, ok := err.(List)
if !ok {
return e.AddField(UnknownField, err)
*e = append(*e, err)
return e
}
for _, fe := range list {
*e = append(*e, fe)
Expand All @@ -48,25 +49,32 @@ func (e *List) AddField(field string, err error) *List {
return e
}

switch errTyped := err.(type) {
switch v := err.(type) {
case List:
for _, childErr := range errTyped {
*e = append(*e, Field{Field: field + "." + childErr.Field, Err: childErr.Err})
for _, err := range v {
if fieldErr, ok := err.(Field); ok {
*e = append(*e, Field{Field: field + "." + fieldErr.Field, Err: fieldErr.Err})
} else {
*e = append(*e, err)
}
}
case error:
case Field:
*e = append(*e, errors.Errorf("%s.%v", field, v.Error()))
default:
*e = append(*e, Field{Field: field, Err: err})
}

return e
}

func (e List) HasErrors() bool {
return len(e) > 0
}

func (e List) ErrorOrNil() error {
if e.HasErrors() {
return e
if len(e) == 0 {
return nil
}
return nil

if len(e) == 1 {
return e[0]
}

return e
}
2 changes: 1 addition & 1 deletion errlist/errlist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Test_List_Add(t *testing.T) {
errs1.AddField("1", errors.New("a"))
errs1.Add(errors.New("b"))

assert.Equal(t, `[1: a, unknown: b]`, errs1.Error())
assert.Equal(t, `[1: a, b]`, errs1.Error())
})

t.Run("many errors", func(t *testing.T) {
Expand Down
2 changes: 0 additions & 2 deletions errlist/fielderr.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"github.com/pkg/errors"
)

const UnknownField = "unknown"

type Field struct {
Field string `json:"field"`
Err error `json:"err"`
Expand Down
6 changes: 3 additions & 3 deletions examples/aliases/entities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func Test_User_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[SomePointer: shorter than 20 chars]`, err.Error())
assert.Equal(t, `SomePointer: shorter than 20 chars`, err.Error())
})

t.Run("non_empty_string: using func validator", func(t *testing.T) {
Expand All @@ -73,7 +73,7 @@ func Test_User_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[NonEmptyString: string is empty]`, err.Error())
assert.Equal(t, `NonEmptyString: string is empty`, err.Error())
})

t.Run("SomePointerNullable: valid", func(*testing.T) {
Expand All @@ -92,7 +92,7 @@ func Test_User_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[SomePointerNullable: shorter than 3 chars]`, err.Error())
assert.Equal(t, `SomePointerNullable: shorter than 3 chars`, err.Error())
})
})
}
18 changes: 9 additions & 9 deletions examples/complicated/entities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func Test_Request1_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Name: shorter than 3 chars]`, err.Error())
assert.Equal(t, `Name: shorter than 3 chars`, err.Error())
})

t.Run("last_name: too short", func(t *testing.T) {
Expand All @@ -80,7 +80,7 @@ func Test_Request1_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[LastName: shorter than 1 chars]`, err.Error())
assert.Equal(t, `LastName: shorter than 1 chars`, err.Error())
})

t.Run("last_name: empty", func(t *testing.T) {
Expand All @@ -97,7 +97,7 @@ func Test_Request1_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Age: less than 18]`, err.Error())
assert.Equal(t, `Age: less than 18`, err.Error())
})

t.Run("children_count: not_null rule", func(t *testing.T) {
Expand All @@ -106,7 +106,7 @@ func Test_Request1_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[ChildrenCount: cannot be nil]`, err.Error())
assert.Equal(t, `ChildrenCount: cannot be nil`, err.Error())
})

t.Run("children_count: too much", func(t *testing.T) {
Expand All @@ -116,7 +116,7 @@ func Test_Request1_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[ChildrenCount: more than 15]`, err.Error())
assert.Equal(t, `ChildrenCount: more than 15`, err.Error())
})

t.Run("dog_pointer: nil is ok", func(t *testing.T) {
Expand All @@ -133,7 +133,7 @@ func Test_Request1_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[DogPointer.Name: shorter than 1 chars]`, err.Error())
assert.Equal(t, `DogPointer.Name: shorter than 1 chars`, err.Error())
})

t.Run("alias: alias type validator", func(t *testing.T) {
Expand Down Expand Up @@ -162,7 +162,7 @@ func Test_Request1_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[MapOfMap.key.1: shorter than 3 chars]`, err.Error())
assert.Equal(t, `MapOfMap.key.1: shorter than 3 chars`, err.Error())
})

t.Run("SliceOfMap: invalid key", func(t *testing.T) {
Expand All @@ -175,7 +175,7 @@ func Test_Request1_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[SliceOfMap.0.key[k]: shorter than 3 chars]`, err.Error())
assert.Equal(t, `SliceOfMap.0.key[k]: shorter than 3 chars`, err.Error())
})

t.Run("SliceOfSliceOfSlice: invalid length", func(t *testing.T) {
Expand All @@ -193,7 +193,7 @@ func Test_Request1_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[SliceOfSliceOfSlice.1.0: less items than 1]`, err.Error())
assert.Equal(t, `SliceOfSliceOfSlice.1.0: less items than 1`, err.Error())
})
})
}
2 changes: 1 addition & 1 deletion examples/overriding/ex1_full_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Test_Request1_Validate(t *testing.T) {

err := r.Age.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Value: more than 64]`, err.Error())
assert.Equal(t, `Value: more than 64`, err.Error())
})

t.Run("check min.", func(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions examples/overriding/ex2_by_method_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_Request2_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Age: field Age is less than 10]`, err.Error())
assert.Equal(t, `Age: field Age is less than 10`, err.Error())
})

t.Run("too young, using generated type validator", func(t *testing.T) {
Expand Down Expand Up @@ -61,7 +61,7 @@ func Test_Request2_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Age: field Age is more than 64]`, err.Error())
assert.Equal(t, `Age: field Age is more than 64`, err.Error())
})

t.Run("check min.", func(t *testing.T) {
Expand All @@ -70,7 +70,7 @@ func Test_Request2_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Some: less than 3]`, err.Error())
assert.Equal(t, `Some: less than 3`, err.Error())
})

t.Run("check max.", func(t *testing.T) {
Expand All @@ -79,7 +79,7 @@ func Test_Request2_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Some: more than 64]`, err.Error())
assert.Equal(t, `Some: more than 64`, err.Error())
})

})
Expand Down
10 changes: 5 additions & 5 deletions examples/overriding/ex3_by_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_Request3_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Age: field Age is less than 10]`, err.Error())
assert.Equal(t, `Age: field Age is less than 10`, err.Error())
})

t.Run("too old, using overridden rule", func(t *testing.T) {
Expand All @@ -35,7 +35,7 @@ func Test_Request3_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Age: field Age is more than 64]`, err.Error())
assert.Equal(t, `Age: field Age is more than 64`, err.Error())
})

t.Run("too young, using generated type validator", func(t *testing.T) {
Expand All @@ -52,7 +52,7 @@ func Test_Request3_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Age: field Age is more than 64]`, err.Error())
assert.Equal(t, `Age: field Age is more than 64`, err.Error())
})

t.Run("check min.", func(t *testing.T) {
Expand All @@ -61,7 +61,7 @@ func Test_Request3_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Some: less than 3]`, err.Error())
assert.Equal(t, `Some: less than 3`, err.Error())
})

t.Run("check max.", func(t *testing.T) {
Expand All @@ -70,7 +70,7 @@ func Test_Request3_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Some: more than 64]`, err.Error())
assert.Equal(t, `Some: more than 64`, err.Error())
})

})
Expand Down
8 changes: 4 additions & 4 deletions examples/overriding/ex4_by_method_and_func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Test_Request4_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Age: field Age is less than 10]`, err.Error())
assert.Equal(t, `Age: field Age is less than 10`, err.Error())
})

t.Run("too young, using func validator", func(t *testing.T) {
Expand Down Expand Up @@ -68,7 +68,7 @@ func Test_Request4_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Age: field Age is more than 128]`, err.Error())
assert.Equal(t, `Age: field Age is more than 128`, err.Error())
})

t.Run("check min.", func(t *testing.T) {
Expand All @@ -77,7 +77,7 @@ func Test_Request4_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Some: less than 3]`, err.Error())
assert.Equal(t, `Some: less than 3`, err.Error())
})

t.Run("check max.", func(t *testing.T) {
Expand All @@ -86,7 +86,7 @@ func Test_Request4_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Some: more than 64]`, err.Error())
assert.Equal(t, `Some: more than 64`, err.Error())
})

})
Expand Down
14 changes: 7 additions & 7 deletions examples/overriding/ex5_additional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_Request5_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[unknown: fields Age and Some can't be 10 at the same time]`, err.Error())
assert.Equal(t, `fields Age and Some can't be 10 at the same time`, err.Error())
})

t.Run("too young, using generated type validator", func(t *testing.T) {
Expand All @@ -36,7 +36,7 @@ func Test_Request5_Validate(t *testing.T) {

err := r.Age.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Value: less than 3]`, err.Error())
assert.Equal(t, `Value: less than 3`, err.Error())
})

t.Run("too old, using generated type validator", func(t *testing.T) {
Expand All @@ -45,7 +45,7 @@ func Test_Request5_Validate(t *testing.T) {

err := r.Age.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Value: more than 64]`, err.Error())
assert.Equal(t, `Value: more than 64`, err.Error())
})

t.Run("too young, using generated validator", func(t *testing.T) {
Expand All @@ -54,7 +54,7 @@ func Test_Request5_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Age.Value: less than 3]`, err.Error())
assert.Equal(t, `Age.Value: less than 3`, err.Error())
})

t.Run("too old, using generated validator", func(t *testing.T) {
Expand All @@ -63,7 +63,7 @@ func Test_Request5_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Age.Value: more than 64]`, err.Error())
assert.Equal(t, `Age.Value: more than 64`, err.Error())
})

t.Run("check min.", func(t *testing.T) {
Expand All @@ -72,7 +72,7 @@ func Test_Request5_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Some: less than 3]`, err.Error())
assert.Equal(t, `Some: less than 3`, err.Error())
})

t.Run("check max.", func(t *testing.T) {
Expand All @@ -81,7 +81,7 @@ func Test_Request5_Validate(t *testing.T) {

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[Some: more than 64]`, err.Error())
assert.Equal(t, `Some: more than 64`, err.Error())
})

})
Expand Down
Loading

0 comments on commit 49868b2

Please sign in to comment.