Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

leftjs
Copy link
Contributor

@leftjs leftjs commented Jun 2, 2022

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:

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

@go-playground/validator-maintainers

@leftjs leftjs requested a review from a team as a code owner June 2, 2022 07:02
@leftjs leftjs changed the title ValidateMapCtx does not support validating []map[string]interface{} i… support validating []map[string]interface{} in []interface #955 Jun 2, 2022
@coveralls
Copy link

Coverage Status

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants