From c0399677b08277849b0833b06e594ba28f6b5252 Mon Sep 17 00:00:00 2001 From: Alonso Villegas Date: Sat, 4 Jun 2022 16:40:32 -0500 Subject: [PATCH] fix: ValidateMapCtx support for more baked-in validators --- validator_instance.go | 13 +++++-- validator_test.go | 86 ++++++++++++++++++++++++++++++++----------- 2 files changed, 74 insertions(+), 25 deletions(-) diff --git a/validator_instance.go b/validator_instance.go index 9493da4..75e2f4a 100644 --- a/validator_instance.go +++ b/validator_instance.go @@ -172,9 +172,16 @@ func (v Validate) ValidateMapCtx(ctx context.Context, data map[string]interface{ errs[field] = errors.New("The field: '" + field + "' is not a map to dive") } } else if ruleStr, ok := rule.(string); ok { - err := v.VarCtx(ctx, data[field], ruleStr) - if err != nil { - errs[field] = err + if dataField, ok := data[field]; ok { + err := v.VarWithValueCtx(ctx, dataField, data, ruleStr) + if err != nil { + errs[field] = err + } + } else { + err := v.VarWithValueCtx(ctx, "", data, ruleStr) + if err != nil { + errs[field] = err + } } } } diff --git a/validator_test.go b/validator_test.go index 1ebdea5..2374256 100644 --- a/validator_test.go +++ b/validator_test.go @@ -12214,6 +12214,48 @@ func TestValidate_ValidateMapCtx(t *testing.T) { }, want: 1, }, + + { + name: "test map with relative rules", + args: args{ + data: map[string]interface{}{ + "Test_A": "Test_A", + "Test_B": "Test_B", + "Test_C": "Test_C", + "Test_D": "Test_D", + "Test_F": "Test_F", + }, + rules: map[string]interface{}{ + "Test_A": "required_if=[Test_B] Test_B", + "Test_B": "required_with=[Test_A]", + "Test_C": "required_with_all=[Test_A] [Test_B]", + "Test_D": "required_without=[Test_F]", + "Test_E": "required_without_all=[Test_D] [Test_F]", + "Test_F": "required_unless=[Test_E] Test_E", + }, + }, + want: 0, + }, + + { + name: "test map err with relative rules", + args: args{ + data: map[string]interface{}{ + "Test_B": "Test_B", + "Test_C": "Test_C", + "Test_D": "Test_D", + }, + rules: map[string]interface{}{ + "Test_A": "required_if=[Test_B] Test_B", + "Test_B": "required_with=[Test_A]", + "Test_C": "required_with_all=[Test_A] [Test_B]", + "Test_D": "required_without=[Test_F]", + "Test_E": "required_without_all=[Test_F] [Test_G]", + "Test_F": "required_unless=[Test_E] Test_E", + }, + }, + want: 3, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -12264,25 +12306,25 @@ func TestCreditCardFormatValidation(t *testing.T) { } func TestMultiOrOperatorGroup(t *testing.T) { - tests := []struct { - Value int `validate:"eq=1|gte=5,eq=1|lt=7"` - expected bool - }{ - {1, true}, {2, false}, {5, true}, {6, true}, {8, false}, - } - - validate := New() - - for i, test := range tests { - errs := validate.Struct(test) - if test.expected { - if !IsEqual(errs, nil) { - t.Fatalf("Index: %d multi_group_of_OR_operators failed Error: %s", i, errs) - } - } else { - if IsEqual(errs, nil) { - t.Fatalf("Index: %d multi_group_of_OR_operators should have errs", i) - } - } - } - } + tests := []struct { + Value int `validate:"eq=1|gte=5,eq=1|lt=7"` + expected bool + }{ + {1, true}, {2, false}, {5, true}, {6, true}, {8, false}, + } + + validate := New() + + for i, test := range tests { + errs := validate.Struct(test) + if test.expected { + if !IsEqual(errs, nil) { + t.Fatalf("Index: %d multi_group_of_OR_operators failed Error: %s", i, errs) + } + } else { + if IsEqual(errs, nil) { + t.Fatalf("Index: %d multi_group_of_OR_operators should have errs", i) + } + } + } +}