0

下面是我的 json 架构

我有一个依赖项,其中提到的所有可选标记只有在 actionType 元素值 id 为“SAVECONTACT”时才为真

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

这个你能帮我吗

{
    "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
                    }
                }
            }
        }
    }
}
4

2 回答 2

1

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

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

{
    "type": "object",
    ... /* other constraints */
    "oneOf": [
        {
            "properties": {
                "actionType": {"enum": ["SAVECONTACT"]}
            },
            "required": [... all the properties ...]
        }
        {
            "properties": {
                "actionType": {"enum": ["DELETECONTACT", ...]}
            },
            "required": [/* empty */]
        }
    ]
}
于 2013-09-10T15:59:48.060 回答
0

我认为您的问题的解决方案可能是使用与此问题中建议的方法相同的方法。

首先,我将使用标准方式来使用“必需”而不是您的自定义标签“可选”。如果你有一个 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"]}
        }
    }
}

}

我不知道您的问题的完整背景,但也许每个操作有一个模式可能更有意义。

于 2013-08-22T17:39:29.903 回答