You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fixed missing keys from returned errors in map validation (#1284)
## Fixes Or Enhances
### Bug Description:
The `ValidateMapCtx` function calls the `VarCtx(ctx context.Context,
field interface{}, tag string) (err error)` method, but the returned
errors do not contain keys.
```go
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.
```go
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.
@go-playground/validator-maintainers
0 commit comments