go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
16.83k stars 1.32k forks source link

required_if panics with ValidateMap on simple map with a single rule #893

Open dosco opened 2 years ago

dosco commented 2 years ago

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

required_if panics panic("Invalid field namespace") when using ValidateMap with simple rules.

Code sample, to showcase or reproduce:

https://go.dev/play/p/6do5LR2UroT


package main

import (
    "fmt"

    "github.com/go-playground/validator/v10"
)

func main() {
    data := map[string]interface{}{
        "name": "hello",
        "id":   123,
    }

    rules := map[string]interface{}{
        "name": "required_if=id 345",
        "id":   123,
    }

    v := validator.New()
    fmt.Println(v.ValidateMap(data, rules))
}
panic: Invalid field namespace

goroutine 1 [running]:
github.com/go-playground/validator/v10.(*validate).getStructFieldOKInternal(0xe55630636d8df4e, {0x538880, 0x595648, 0xc00029f670}, {0x5672a7, 0xc00030a6e0})
    /tmp/gopath1590948958/pkg/mod/github.com/go-playground/validator/v10@v10.10.0/util.go:220 +0x160b
github.com/go-playground/validator/v10.(*validate).GetStructFieldOKAdvanced2(0x6bc240, {0x538880, 0x595648, 0x6}, {0x5672a7, 0x0})
    /tmp/gopath1590948958/pkg/mod/github.com/go-playground/validator/v10@v10.10.0/field_level.go:119 +0x28
github.com/go-playground/validator/v10.requireCheckFieldValue({0x59a318, 0xc00007b9e0}, {0x5672a7, 0x2}, {0x5672aa, 0x3}, 0x8)
    /tmp/gopath1590948958/pkg/mod/github.com/
cchengubnt commented 1 year ago

this issue has been a long time, any plan fix it and merge into main branch?

zein-adi commented 1 year ago

+1 i used version 10.15.5 the problem still exists

thedenisnikulin commented 9 months ago

this is weird... How such a long maintained and popular package has such a bug which hasn't been fixed since 2022? Isn't "required_if" an essential tag?