fix: ValidateMapCtx support for more baked-in validators

pull/958/head
Alonso Villegas 3 years ago
parent 9e2ea40380
commit c0399677b0
  1. 9
      validator_instance.go
  2. 42
      validator_test.go

@ -172,10 +172,17 @@ func (v Validate) ValidateMapCtx(ctx context.Context, data map[string]interface{
errs[field] = errors.New("The field: '" + field + "' is not a map to dive") errs[field] = errors.New("The field: '" + field + "' is not a map to dive")
} }
} else if ruleStr, ok := rule.(string); ok { } else if ruleStr, ok := rule.(string); ok {
err := v.VarCtx(ctx, data[field], ruleStr) if dataField, ok := data[field]; ok {
err := v.VarWithValueCtx(ctx, dataField, data, ruleStr)
if err != nil { if err != nil {
errs[field] = err errs[field] = err
} }
} else {
err := v.VarWithValueCtx(ctx, "", data, ruleStr)
if err != nil {
errs[field] = err
}
}
} }
} }
return errs return errs

@ -12214,6 +12214,48 @@ func TestValidate_ValidateMapCtx(t *testing.T) {
}, },
want: 1, 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 { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

Loading…
Cancel
Save