From a475c6ec7c568ef6e81ac316123dc0b429c640ea Mon Sep 17 00:00:00 2001 From: Rui Cheng Date: Mon, 24 Jul 2023 23:17:22 +0800 Subject: [PATCH] bugfix: SliceValidationError has two elements, but the first one is nil --- binding/default_validator.go | 11 ++++------- binding/default_validator_test.go | 7 +++++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/binding/default_validator.go b/binding/default_validator.go index e216b854..ce157255 100644 --- a/binding/default_validator.go +++ b/binding/default_validator.go @@ -28,15 +28,12 @@ func (err SliceValidationError) Error() string { return "" default: var b strings.Builder - if err[0] != nil { - fmt.Fprintf(&b, "[%d]: %s", 0, err[0].Error()) - } - if n > 1 { - for i := 1; i < n; i++ { - if err[i] != nil { + for i := 0; i < n; i++ { + if err[i] != nil { + if b.Len() > 0 { b.WriteString("\n") - fmt.Fprintf(&b, "[%d]: %s", i, err[i].Error()) } + fmt.Fprintf(&b, "[%d]: %s", i, err[i].Error()) } } return b.String() diff --git a/binding/default_validator_test.go b/binding/default_validator_test.go index df7742b7..ef7f45b3 100644 --- a/binding/default_validator_test.go +++ b/binding/default_validator_test.go @@ -25,6 +25,13 @@ func TestSliceValidationError(t *testing.T) { }, "[0]: first error\n[1]: second error", }, + {"has two elements, but the first one is nil", + SliceValidationError{ + nil, + errors.New("first error at second place"), + }, + "[1]: first error at second place", + }, {"has many elements", SliceValidationError{ errors.New("first error"),