Commit 13130d2
authored
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-maintainers1 parent 94e89f0 commit 13130d2
2 files changed
+153
-1
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
181 | 181 | | |
182 | 182 | | |
183 | 183 | | |
184 | | - | |
| 184 | + | |
185 | 185 | | |
186 | 186 | | |
187 | 187 | | |
| |||
681 | 681 | | |
682 | 682 | | |
683 | 683 | | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
| 692 | + | |
| 693 | + | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
| 705 | + | |
| 706 | + | |
| 707 | + | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
| 741 | + | |
684 | 742 | | |
685 | 743 | | |
686 | 744 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13508 | 13508 | | |
13509 | 13509 | | |
13510 | 13510 | | |
| 13511 | + | |
| 13512 | + | |
| 13513 | + | |
| 13514 | + | |
| 13515 | + | |
| 13516 | + | |
| 13517 | + | |
| 13518 | + | |
| 13519 | + | |
| 13520 | + | |
| 13521 | + | |
| 13522 | + | |
| 13523 | + | |
| 13524 | + | |
| 13525 | + | |
| 13526 | + | |
| 13527 | + | |
| 13528 | + | |
| 13529 | + | |
| 13530 | + | |
| 13531 | + | |
| 13532 | + | |
| 13533 | + | |
| 13534 | + | |
| 13535 | + | |
| 13536 | + | |
| 13537 | + | |
| 13538 | + | |
| 13539 | + | |
| 13540 | + | |
| 13541 | + | |
| 13542 | + | |
| 13543 | + | |
| 13544 | + | |
| 13545 | + | |
| 13546 | + | |
| 13547 | + | |
| 13548 | + | |
| 13549 | + | |
| 13550 | + | |
| 13551 | + | |
| 13552 | + | |
| 13553 | + | |
| 13554 | + | |
| 13555 | + | |
| 13556 | + | |
| 13557 | + | |
| 13558 | + | |
| 13559 | + | |
| 13560 | + | |
| 13561 | + | |
| 13562 | + | |
| 13563 | + | |
| 13564 | + | |
| 13565 | + | |
| 13566 | + | |
| 13567 | + | |
| 13568 | + | |
| 13569 | + | |
| 13570 | + | |
| 13571 | + | |
| 13572 | + | |
| 13573 | + | |
| 13574 | + | |
| 13575 | + | |
| 13576 | + | |
| 13577 | + | |
| 13578 | + | |
| 13579 | + | |
| 13580 | + | |
| 13581 | + | |
| 13582 | + | |
| 13583 | + | |
| 13584 | + | |
| 13585 | + | |
| 13586 | + | |
| 13587 | + | |
| 13588 | + | |
| 13589 | + | |
| 13590 | + | |
| 13591 | + | |
| 13592 | + | |
| 13593 | + | |
| 13594 | + | |
| 13595 | + | |
| 13596 | + | |
| 13597 | + | |
| 13598 | + | |
| 13599 | + | |
| 13600 | + | |
| 13601 | + | |
| 13602 | + | |
| 13603 | + | |
| 13604 | + | |
13511 | 13605 | | |
13512 | 13606 | | |
13513 | 13607 | | |
| |||
0 commit comments