0

我在验证/理解“oneOf”运算符时遇到问题。

JSON模式:

{
        "$schema": "http://json-schema.org/draft-04/schema#",
        "type": "object",
        "properties": {
            "QuerySpecification": {
                "type": "array",
                "minItems": 1,
                "items": {
                    "type": "object",
                    "properties": {
                        "FieldName": {
                            "type": "string"
                        }
                    },
                    "required": [
                        "FieldName"
                    ],
                    "oneOf": [
                        {
                            "properties": {
                                "SimpleQuery": {
                                    "type": "string"
                                }
                            }
                        },
                        {
                            "properties": {
                                "CompositeQuery": {
                                    "type": "string"
                                }
                            },
                            "additionalProperties": false
                        }
                    ]
                }
            }
        }, "required": ["QuerySpecification"]
    }

我希望 JSON 输入中需要“SimpleQuery”或“CompositeQuery”,但它在没有指定它们的情况下验证 OK。

JSON输入:

{
    "QuerySpecification": [{
        "FieldName": "Andreas"
    }]
}
4

1 回答 1

0

想出了答案

JSON SCHEMA:

{   
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "QuerySpecification": {
            "type": "array",
            "minItems": 1,
            "items": {
                "type": "object",
                "properties": {
                    "FieldName": {
                        "type": "string"
                    },
                    "SimpleQuery": {
                        "type": "string"
                    },
                    "CompositeQuery": {
                        "type": "string"
                    }
                },
                "oneOf": [{
                    "required": ["SimpleQuery"]
                },{
                    "required": ["CompositeQuery"]
                }],
                "required": [
                    "FieldName"
                ],
                "additionalProperties": false
            }
        }
    }, "required": ["QuerySpecification"]
}

此架构将无法验证

{
    "QuerySpecification": [{
        "FieldName": "Andreas"
    }]
}

..但是这个

{
    "QuerySpecification": [{
        "FieldName": "Andreas",
        "SimpleQuery": "hei"
    }]
}

将验证

于 2015-05-21T10:12:44.720 回答