Skip to content

Commit

Permalink
leave field errors in a slice
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Chechel committed Aug 31, 2018
1 parent 49868b2 commit f71396e
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 40 deletions.
3 changes: 3 additions & 0 deletions errlist/errlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func (e List) ErrorOrNil() error {
}

if len(e) == 1 {
if _, ok := e[0].(Field); ok {
return e
}
return e[0]
}

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
12 changes: 6 additions & 6 deletions examples/overriding/ex5_additional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 8 additions & 8 deletions examples/simple/entities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Test_User_Validate(t *testing.T) {

err := user.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("too long name", func(t *testing.T) {
Expand All @@ -40,7 +40,7 @@ func Test_User_Validate(t *testing.T) {

err := user.Validate()
require.NotNil(t, err)
assert.Equal(t, `Name: longer than 64 chars`, err.Error())
assert.Equal(t, `[Name: longer than 64 chars]`, err.Error())
})

t.Run("nil title", func(t *testing.T) {
Expand All @@ -49,7 +49,7 @@ func Test_User_Validate(t *testing.T) {

err := user.Validate()
require.NotNil(t, err)
assert.Equal(t, "Title: cannot be nil", err.Error())
assert.Equal(t, "[Title: cannot be nil]", err.Error())
})

t.Run("bad title", func(t *testing.T) {
Expand All @@ -59,7 +59,7 @@ func Test_User_Validate(t *testing.T) {

err := user.Validate()
require.NotNil(t, err)
assert.Equal(t, "Title: invalid value for enum Title: Jedi", err.Error())
assert.Equal(t, "[Title: invalid value for enum Title: Jedi]", err.Error())
})

t.Run("no email", func(t *testing.T) {
Expand All @@ -68,7 +68,7 @@ func Test_User_Validate(t *testing.T) {

err := user.Validate()
require.NotNil(t, err)
assert.Equal(t, "Emails: less items than 1", err.Error())
assert.Equal(t, "[Emails: less items than 1]", err.Error())
})

t.Run("bad email key and value", func(t *testing.T) {
Expand All @@ -86,7 +86,7 @@ func Test_User_Validate(t *testing.T) {

err := user.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("too old", func(t *testing.T) {
Expand All @@ -95,7 +95,7 @@ func Test_User_Validate(t *testing.T) {

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

t.Run("bad dog", func(t *testing.T) {
Expand All @@ -104,7 +104,7 @@ func Test_User_Validate(t *testing.T) {

err := user.Validate()
require.NotNil(t, err)
assert.Equal(t, `Dog.Name: shorter than 1 chars`, err.Error())
assert.Equal(t, `[Dog.Name: shorter than 1 chars]`, err.Error())
})
})
}
Expand Down

0 comments on commit f71396e

Please sign in to comment.