How to define a JSON schema that requires at least one of many properties

北城余情 提交于 2019-12-01 15:35:25

To require at least one of a set of properties, use required inside a series of anyOf options:

{
    "type": "object",
    "anyOf": [
        {"required": ["id"]},
        {"required": ["email"]}
        // any other properties, in a similar way
    ],
    "properties": {
        // Your actual property definitions here
    }
{

If any of the properties you want is present ("id", "email"), then it will pass the corresponding option in allOf.

You may use minProperties: number (and maxProperties: number if needed). That would shorten the schema definition:

{
     type: "object",
     minProperties: 1,
     properties: [/* your actual properties definitions */],
}

Link to documentation: https://json-schema.org/understanding-json-schema/reference/object.html#size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!