使用JSON Schema 验证时,是不是递归验证子实体?

When using JSON Schema for validation, it is not recursively validate child entities?

使用以下架构时

{
    "$schema": "http://json-schema.org/schema#",
    "definitions":{
        "entity": {
            "type": "object",
            "properties": {
                "parent": {"type": ["null","string"]},
                "exclude": {"type": "boolean"},
                "count": {"type": ["null","integer"]},
                "EntityType": {"type": "string"},
                "Children": {
                    "type": "array",
                    "items": {"$ref":"#/definitions/entity"}
                }
            }
        }
    },
    "required": ["parent","EntityType","count"]
}

在 JSON

提供的正文中
{
    "parent": "null",
    "EntityType": "test",
    "count": "null",
    "Children": [
        {
            "EntityType": "test",
            "count": 3
        },
        {
            "EntityType": "test"
        }
    ],
    "Extra": "somevalue"
}

它应该返回我提供了一个无效的 Json 对象,但它似乎并没有这样做。

就是说,如果我要使根节点不成功(通过删除其中一个必填字段),则验证有效并表示我没有提供必填字段。是否有我无法递归验证 json 的原因?

您似乎希望 parentEntityTypecount 成为 entity 定义的必需属性。但是,它们仅在根目录中需要,而不是 entity 级别。我建议您将 required 关键字移动到 entity 定义中,然后将定义作为 allOf 的一部分引用以确保根符合要求。

{
    "$schema": "http://json-schema.org/schema#",
    "definitions":{
        "entity": {
            "type": "object",
            "properties": {
                "parent": {"type": ["null","string"]},
                "exclude": {"type": "boolean"},
                "count": {"type": ["null","integer"]},
                "EntityType": {"type": "string"},
                "Children": {
                    "type": "array",
                    "items": {"$ref":"#/definitions/entity"}
                }
            },
            "required": ["parent","EntityType","count"]
        }
    },
    "allOf": [{"$ref": "#/definitions/entity"}]
}