validator icon indicating copy to clipboard operation
validator copied to clipboard

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

Open leftjs opened this issue 2 years ago • 1 comments

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:

  • [x] Tests exist or have been written that cover this particular change.

@go-playground/validator-maintainers

leftjs avatar Jun 02 '22 07:06 leftjs

Coverage Status

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

coveralls avatar Jun 02 '22 07:06 coveralls