json schema元素可选,具体取决于其他元素值

时间:2013-07-27 23:24:29

标签: json jsonschema

下面的

是我的json架构

我有一个依赖项,只有当actionType元素值为“SAVECONTACT”

时,所提到的所有可选标记才应该为true

我不知道如何实现这种依赖

请帮我解决这个问题

{
    "type": "object",
    "properties": {
        "userId": {
            "type": "string",
            "optional": true
        },
        "groupId": {
            "type": "string",
            "optional": true
        },
        "socialMediaContacts": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "smContactId": {
                        "type":"string"
                    },
                    "actionType": {
                        "type":"string",
                        "enum" : ["SAVECONTACT", "DELETECONTACT", "SAVEGROUP", "DELETEGROUP"]
                    },
                    "contactLastName": {
                        "type":"string",
                        "optional": true
                    },
                    "contactFirstName": {
                        "type":"string",
                        "optional": true
                    },
                    "nickName": {
                        "type":"string",
                        "optional": true
                    },
                    "contactType": {
                        "type":"string",
                        "enum" : ["SM", "REG"],
                        "optional": true
                    },
                    "mediaSource": {
                        "type":"string",
                        "enum" : ["FB", "FS", "TW"],
                        "optional": true
                    },
                    "socialMediaHandle": {
                        "type":"string",
                        "optional": true
                    },
                    "email": {
                        "type":"string",
                        "optional": true
                    },
                    "phone": {
                        "type":"string",
                        "optional": true
                    }
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

要处理这种情况,您可能需要"oneOf"

基本上,您为每种情况创建一个模式 - “SAVECONTACT”,而非“SAVECONTACT”:

{
    "type": "object",
    ... /* other constraints */
    "oneOf": [
        {
            "properties": {
                "actionType": {"enum": ["SAVECONTACT"]}
            },
            "required": [... all the properties ...]
        }
        {
            "properties": {
                "actionType": {"enum": ["DELETECONTACT", ...]}
            },
            "required": [/* empty */]
        }
    ]
}

答案 1 :(得分:0)

我认为您的问题的解决方案可能使用相同的方法that suggested in this question

首先,我会使用标准方式来使用“required”而不是自定义标签“optional”。如果您有一个Draft 4验证器,那么这样的事情就可以了:

{
"type": "object",
"properties": {
    "userId": {
        "type": "string",
        "optional": true
    },
    "groupId": {
        "type": "string",
        "optional": true
    },
    "socialMediaContacts": {
        "type": "array",
        "allOf":
        {
            "properties": {
                    "smContactId": {"type":"string"},
                    "actionType": {"enum" : ["SAVECONTACT", "DELETECONTACT", "SAVEGROUP", "DELETEGROUP"]},
                    "contactLastName": {"type":"string"},
                    "contactFirstName": {"type":"string"},
                    "nickName": {"type":"string"},
                    "contactType": {"type":"string","enum" : ["SM", "REG"]},
                    "mediaSource": {"type":"string","enum" : ["FB", "FS", "TW"]},
                    "socialMediaHandle": {"type":"string"},
                    "email": {"type":"string"},
                    "phone": {"type":"string"}
                },
                "required": ["actionType","smContactId"],
                "additionalProperties": False
            },
        "oneOf": [
            { "$ref": "#/definitions/savecontact" },
            { "$ref": "#/definitions/otheroperations" }
        ]
    }
},
"definitions": {
    "savecontact": {
        "properties": {
            "actionType": { "enum": [ "SAVECONTACT" ] },

        },
        "required": ["contactLastName","contactFirstName", etc. etc.],
    },
    "otheroperations": {
        properties": {
            "actionType": {"enum" : ["DELETECONTACT", "SAVEGROUP", "DELETEGROUP"]}
        }
    }
}

}

我不知道你的问题的完整上下文,但也许每个操作有一个模式可能更有意义。