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

support validating []map[string]interface{} in []interface #955 #956

Open leftjs opened 2 years ago

leftjs commented 2 years ago

Fix #955 Fix #915

Fixes Or Enhances

json.Unmarshal() will convert JSON Array to []interface, currently ValidateMapCtx does not support validating []map[string]interface{} in []interface

It should support checking []map[string]interface{} in []interface, e.g.

bs, err := ioutil.ReadFile("./test/deployment.json")
if err != nil {
    panic(err)
}

var mmm map[string]interface{}
err = json.Unmarshal(bs, &mmm)
if err != nil {
    panic(err)
}

bs, err = ioutil.ReadFile("./test/rule.json")
if err != nil {
    panic(err)
}

var rules map[string]interface{}
_ = json.Unmarshal(bs, &rules)

res := validate.ValidateMapCtx(context.Background(), mmm, rules)

k8s deployment may contain multiple container definitions, so rule.json is defined as follows

{
    "spec": {
        "replicas": "required,number,gte=1,lte=300",
        "template": {
            "spec": {
                "terminationGracePeriodSeconds": "gte=0,lte=120",
                "containers": [
                    {
                        "name": "required,min=1,max=64,containername"
                    }
                ]
            }
        }
    }
}

Make sure that you've checked the boxes below before you submit PR:

@go-playground/validator-maintainers

coveralls commented 2 years ago

Coverage Status

Coverage increased (+0.01%) to 74.191% when pulling 165e95e961ba4e67a09fad68d06625c3e7188e85 on leftjs:master into 9e2ea4038020b5c7e3802a21cfa4e3afcfdcd276 on go-playground:master.