From 4a073cc3250da474ce931fe9adbd67569244cce7 Mon Sep 17 00:00:00 2001 From: Ganeshdip Dumbare Date: Mon, 9 Sep 2024 02:09:32 +0200 Subject: [PATCH] fix(#1310): fix excluded_if for pointers (#1313) ## Fixes Or Enhances - fixes #1310 - add check for pointers in case of excluded_if --- baked_in.go | 9 ++++++++- validator_test.go | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/baked_in.go b/baked_in.go index b6fbaafa..d1a3656a 100644 --- a/baked_in.go +++ b/baked_in.go @@ -1828,7 +1828,14 @@ func requireCheckFieldValue( return int64(field.Len()) == asInt(value) case reflect.Bool: - return field.Bool() == asBool(value) + return field.Bool() == (value == "true") + + case reflect.Ptr: + if field.IsNil() { + return value == "nil" + } + // Handle non-nil pointers + return requireCheckFieldValue(fl, param, value, defaultNotFoundValue) } // default reflect.String: diff --git a/validator_test.go b/validator_test.go index 0f96b777..367a6b41 100644 --- a/validator_test.go +++ b/validator_test.go @@ -12002,6 +12002,25 @@ func TestExcludedIf(t *testing.T) { errs = validate.Struct(test10) Equal(t, errs, nil) + test11 := struct { + Field1 bool + Field2 *string `validate:"excluded_if=Field1 false"` + }{ + Field1: false, + Field2: nil, + } + errs = validate.Struct(test11) + Equal(t, errs, nil) + + test12 := struct { + Field1 bool + Field2 *string `validate:"excluded_if=Field1 !Field1"` + }{ + Field1: true, + Field2: nil, + } + errs = validate.Struct(test12) + Equal(t, errs, nil) // Checks number of params in struct tag is correct defer func() { if r := recover(); r == nil {