JSON模式验证 - 具有混合对象类型数组的oneOf

时间:2016-01-13 14:24:04

标签: json jsonschema

我正在尝试构建一个JSON模式,其中JSON数据具有一组混合对象类型。我正在尝试使用oneOf,但似乎我遗漏了一些东西,因为我的JSON数据无法针对架构进行验证。

以下是我到目前为止所做的工作。

Schema:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "definitions": {
        "Entity": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "Property": {
                    "type": "string"
                },
                "Value": {
                    "type": "string"
                }
            },
            "required": [ "Property", "Value" ]
        },
        "NavEntity": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "Property": {
                    "type": "string"
                },
                "NavigationalEntities": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Entity"
                    }
                }
            },
            "required": [ "Property", "NavigationalEntities" ]
        }
    },
    "additionalProperties": true,
    "name": "/",
    "properties": {
        "Entities": {
            "type": "array",
            "minLength": 1,
            "uniqueItems": true,
            "items": {
                "oneOf": [
                    { "$ref": "#/definitions/Entity" },
                    { "$ref": "#/definitions/NavEntity" }
                ],
                "additionalProperties": false
            }
        }
    }
}

这是我的JSON数据:

{
    "Entities": [
        {
            "Property": "ABC",
            "NavigationalEntities": [
                {
                    "Property": "ABC1",
                    "Value": "123"
                }
            ]
        },
        {
            "Property": "ABCD",
            "Value": "ABCD"
        }
    ]
}

当我尝试验证时,我收到错误:"Additional properties not allowed"。这也可以看作here

请告诉我这里缺少的东西。

1 个答案:

答案 0 :(得分:2)

问题是"additionalProperties": false属性中items关键字中包含的Entities。 你指定了两个:

  • 所有项目都不应具有任何其他属性 在items对象中定义(并且您没有定义任何对象)。
  • 所有项目都必须验证EntityNavEntity
  • 之一

如果删除最后一个"additionalProperties": false,一切正常。并且您不需要它,因为EntityNavEntity都包含它。

提议的架构:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "definitions": {
        "Entity": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "Property": {
                    "type": "string"
                },
                "Value": {
                    "type": "string"
                }
            },
            "required": [ "Property", "Value" ]
        },
        "NavEntity": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "Property": {
                    "type": "string"
                },
                "NavigationalEntities": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/Entity"
                    }
                }
            },
            "required": [ "Property", "NavigationalEntities" ]
        }
    },
    "additionalProperties": true,
    "name": "/",
    "properties": {
        "Entities": {
            "type": "array",
            "minLength": 1,
            "uniqueItems": true,
            "items": {
                "oneOf": [
                    { "$ref": "#/definitions/Entity" },
                    { "$ref": "#/definitions/NavEntity" }
                ]
            }
        }
    }
}