简体   繁体   中英

json schema to validate array of objects with anyOf and oneOf requirements

I am trying to define a json schema to limit the properties of objects conatined in an array.

{
    "title":"myCollection",
    "properties":{
        "things":{
            "type":"array",
            "items":[{
                "title":"thingObj",
                "type":"object",
                "properties":{
                    "name":{
                        "type":"string"
                    },
                    "code":{
                        "type":"string"
                    },
                    "type":{
                         "type":"string",
                         "enum":["dog","cat"]
                    },
                    "rate":{
                        "type":"number"
                    },
                    "value":{
                        "type":"number"
                    }
                },
                "anyOf":[{
                    "properties":{
                        "name":{
                            "type":"string"
                        }
                    },"required":["name"]
                },{
                    "properties":{
                        "code":{
                            "type":"string"
                        }
                    },"required":["code"]
                },{
                    "properties":{
                        "type":{
                            "type":"string",
                            "enum":["new","existing"]
                        }
                    },"required":["type"]
                }],
                "oneOf":[{
                    "properties":{
                        "rate":{
                            "type":"number"
                        }
                    },
                    "required":["rate"]
                },{
                   "properties":{
                       "value":{
                            "type":"number"
                       }
                   },
                   "required":["value"]
                }],
                "additionalProperties":false
            }]
        }
    }
}

This is because you have (accidentally) used "tuple typing". This is enabled when the value of "items" is an array, and it matches schemas to specific positions in the array.

If you change "items" (in your schema) to be simply a schema (not an array of schemas), then it will validate all items the same way.

Kudos to @cloudfeet 's answer, I was struggling with this issue until I saw his answer. To be more clear, the [] around items should be removed.

{
    "title":"myCollection",
    "properties":{
        "things":{
            "type":"array",
            "items":**[**{
                "title":"thingObj",
                "type":"object",
                .
                .
                .
                   "required":["value"]
                }**]**,
                "additionalProperties":false
            }]
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM