The ValidateMapCtx function calls the VarCtx(ctx context.Context, field interface{}, tag string) (err error) method, but the returned errors do not contain keys.
package main
import (
"fmt"
"github.com/go-playground/validator/v10"
)
func main() {
validate := validator.New()
data := map[string]interface{}{"email": "emailaddress"}
rules := map[string]interface{}{"email": "required,email"}
errs := validate.ValidateMap(data, rules) // when VarCtx is called
fmt.Println(errs)
//output: map[email:Key: '' Error:Field validation for '' failed on the 'email' tag]
}
Fix:
Added a new method VarWithKeyCtx(ctx context.Context, key string, field interface{}, tag string) (err error) to support validating a single variable, ensuring that the returned error contains the key of the field. This retains compatibility with the current VarCtx(...) method. Now, ValidateMapCtx will call the new VarWithKeyCtx(...) method.
package main
import (
"fmt"
"github.com/go-playground/validator/v10"
)
func main() {
validate := validator.New()
data := map[string]interface{}{"email": "emailaddress"}
rules := map[string]interface{}{"email": "required,email"}
errs := validate.ValidateMap(data, rules) // when the new VarWithKeyCtx is called
fmt.Println(errs)
//output: map[email:Key: 'email' Error:Field validation for 'email' failed on the 'email' tag]
}
Make sure that you've checked the boxes below before you submit PR:
[x] Tests exist or have been written that cover this particular change.
coverage: 74.319% (+0.03%) from 74.291%
when pulling 1152639393d3fd55dc018c134928823258636138 on angelo1121:fix-map-validation-missing-keys
into a947377040f8ebaee09f20d09a745ec369396793 on go-playground:master.
Fixes Or Enhances
Bug Description:
The
ValidateMapCtx
function calls theVarCtx(ctx context.Context, field interface{}, tag string) (err error)
method, but the returned errors do not contain keys.Fix:
Added a new method
VarWithKeyCtx(ctx context.Context, key string, field interface{}, tag string) (err error)
to support validating a single variable, ensuring that the returned error contains the key of the field. This retains compatibility with the currentVarCtx(...)
method. Now,ValidateMapCtx
will call the newVarWithKeyCtx(...)
method.Make sure that you've checked the boxes below before you submit PR:
@go-playground/validator-maintainers