From 75eeb3ffddb933bc7ccef84441e168b4edbf90f4 Mon Sep 17 00:00:00 2001 From: MysteriousPotato <123033965+MysteriousPotato@users.noreply.github.com> Date: Wed, 30 Aug 2023 05:00:34 +0000 Subject: [PATCH] Fix Type called on zero reflect.Value (#1156) Fixes #1155 Simply added back the check for invalid reflect types before calling `Type` on `reflect.Value`. --- validator.go | 4 ++++ validator_test.go | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/validator.go b/validator.go index d3ff51d1a..2cae8f7ec 100644 --- a/validator.go +++ b/validator.go @@ -162,6 +162,10 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr } } + if kind == reflect.Invalid { + return + } + case reflect.Struct: isNestedStruct = !current.Type().ConvertibleTo(timeType) // For backward compatibility before struct level validation tags were supported diff --git a/validator_test.go b/validator_test.go index d9b715cb9..1dfa85cca 100644 --- a/validator_test.go +++ b/validator_test.go @@ -2317,6 +2317,13 @@ func TestSQLValueValidation(t *testing.T) { Equal(t, len(errs.(ValidationErrors)), 2) AssertError(t, errs, "CustomMadeUpStruct.MadeUp", "CustomMadeUpStruct.MadeUp", "MadeUp", "MadeUp", "required") AssertError(t, errs, "CustomMadeUpStruct.OverriddenInt", "CustomMadeUpStruct.OverriddenInt", "OverriddenInt", "OverriddenInt", "gt") + + // Test for empty field on structs without tags + type InvalidValuePanicSafetyTest struct { + V valuer + } + errs = validate.Struct(InvalidValuePanicSafetyTest{}) + Equal(t, errs, nil) } func TestMACValidation(t *testing.T) {