From ced40351d38b071dcaea10e4f5a4976afbc5e663 Mon Sep 17 00:00:00 2001 From: inhere Date: Sat, 21 Dec 2024 14:13:00 +0800 Subject: [PATCH] :bug: fix: reflect: call of reflect.Value.IsNil on array Value. see #280 - perf: skip parse on slice/array elements is simple kind --- data_source.go | 6 +++++- issues_test.go | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/data_source.go b/data_source.go index 13c85b8..1f4307d 100644 --- a/data_source.go +++ b/data_source.go @@ -384,7 +384,11 @@ func (d *StructData) parseRulesFromTag(v *Validation) { fValue = removeValuePtr(fValue) // Check if the reflect.Value is valid and not a nil pointer - if !fValue.IsValid() || fValue.IsNil() { + if !fValue.IsValid() || (ft.Kind() == reflect.Slice && fValue.IsNil()) { + continue + } + // perf: skip parse on elements is simple kind + if reflects.IsSimpleKind(ft.Elem().Kind()) { continue } diff --git a/issues_test.go b/issues_test.go index 054886d..c69ea1e 100644 --- a/issues_test.go +++ b/issues_test.go @@ -1775,3 +1775,14 @@ func TestIssues_276(t *testing.T) { fmt.Println(v.Errors.OneError()) // returns a random error fmt.Println(v.Errors.Field("Age")) // returns error messages of the field } + +// https://github.com/gookit/validate/issues/280 +func TestIssues_280(t *testing.T) { + type TestData struct { + ID mockUUID + } + + data := TestData{} + v := validate.New(data) + assert.Nil(t, v.ValidateErr()) +}