From f71396e78c6bfc74c6d0101b8c53e1a6f131a3fb Mon Sep 17 00:00:00 2001 From: Max Chechel Date: Fri, 31 Aug 2018 18:39:42 +0300 Subject: [PATCH] leave field errors in a slice --- errlist/errlist.go | 3 +++ examples/aliases/entities_test.go | 6 +++--- examples/complicated/entities_test.go | 18 +++++++++--------- examples/overriding/ex1_full_test.go | 2 +- examples/overriding/ex2_by_method_test.go | 8 ++++---- examples/overriding/ex3_by_func_test.go | 10 +++++----- .../overriding/ex4_by_method_and_func_test.go | 8 ++++---- examples/overriding/ex5_additional_test.go | 12 ++++++------ examples/simple/entities_test.go | 16 ++++++++-------- 9 files changed, 43 insertions(+), 40 deletions(-) diff --git a/errlist/errlist.go b/errlist/errlist.go index f31ef5a..a1c05ab 100644 --- a/errlist/errlist.go +++ b/errlist/errlist.go @@ -73,6 +73,9 @@ func (e List) ErrorOrNil() error { } if len(e) == 1 { + if _, ok := e[0].(Field); ok { + return e + } return e[0] } diff --git a/examples/aliases/entities_test.go b/examples/aliases/entities_test.go index f369f17..fadc7ee 100644 --- a/examples/aliases/entities_test.go +++ b/examples/aliases/entities_test.go @@ -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) { @@ -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) { @@ -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()) }) }) } diff --git a/examples/complicated/entities_test.go b/examples/complicated/entities_test.go index ac04551..a1da708 100644 --- a/examples/complicated/entities_test.go +++ b/examples/complicated/entities_test.go @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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()) }) }) } diff --git a/examples/overriding/ex1_full_test.go b/examples/overriding/ex1_full_test.go index d9c4507..0745d4d 100644 --- a/examples/overriding/ex1_full_test.go +++ b/examples/overriding/ex1_full_test.go @@ -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) { diff --git a/examples/overriding/ex2_by_method_test.go b/examples/overriding/ex2_by_method_test.go index bdebb50..9634287 100644 --- a/examples/overriding/ex2_by_method_test.go +++ b/examples/overriding/ex2_by_method_test.go @@ -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) { @@ -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) { @@ -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) { @@ -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()) }) }) diff --git a/examples/overriding/ex3_by_func_test.go b/examples/overriding/ex3_by_func_test.go index 6d38718..2daeb55 100644 --- a/examples/overriding/ex3_by_func_test.go +++ b/examples/overriding/ex3_by_func_test.go @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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()) }) }) diff --git a/examples/overriding/ex4_by_method_and_func_test.go b/examples/overriding/ex4_by_method_and_func_test.go index 863378b..e0a6483 100644 --- a/examples/overriding/ex4_by_method_and_func_test.go +++ b/examples/overriding/ex4_by_method_and_func_test.go @@ -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) { @@ -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) { @@ -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) { @@ -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()) }) }) diff --git a/examples/overriding/ex5_additional_test.go b/examples/overriding/ex5_additional_test.go index 61b0b4c..b92798e 100644 --- a/examples/overriding/ex5_additional_test.go +++ b/examples/overriding/ex5_additional_test.go @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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()) }) }) diff --git a/examples/simple/entities_test.go b/examples/simple/entities_test.go index d0e4e52..8528117 100644 --- a/examples/simple/entities_test.go +++ b/examples/simple/entities_test.go @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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()) }) }) }