validator icon indicating copy to clipboard operation
validator copied to clipboard

ValidateMapCtx does not support validating []map[string]interface{} in []interface

Open leftjs opened this issue 2 years ago • 0 comments

  • [x] I have looked at the documentation here first?
  • [x] I have looked at the examples provided that may showcase my question here?

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

Issue & Enhancement

Code sample, to showcase or reproduce:

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"
                    }
                ]
            }
        }
    }
}

leftjs avatar Jun 02 '22 06:06 leftjs