2

我有一个深度嵌套的 json,我正在尝试用 cerberus 进行验证。所以,我有以下结构:(注意:假设更深的巢)

我为第一级创建了架构,但我不知道如何深入 json。我无法手动编写深度模式,因为我不知道 json 会下降多少级。据我了解,cerberus 可以处理嵌套的 json,但我找不到针对这种确切情况的任何代码参考。知道如何实现吗?

{
"product_number": "123",
"product_version": "ABC",
"subproducts": [
    {
        "product_number": "444",
        "product_version": "ASD",
        "subproducts": [
            {
                "product_number": "666",
                "product_version": "FFF",
                "subproducts": [
                    {
                        "product_number": "888",
                        "product_version": "JUN",
                        "subproducts": []
                    },
                    {
                        "product_number": "644",
                        "product_version": "GYB",
                        "subproducts": [
                            {
                                "product_number": "8890",
                                "product_version": "KLM",
                                "subproducts": []
                            }
                        ]
                    }
                ],
            }
        ],
    }
],}
4

2 回答 2

0

You can use the schema registry for self-referencing schemas:

from cerberus import schema_registry, Validator

product_schema = {
    "product_number": {"type": "string", "regex": "\d+"},
    "product_version": {"type": "string", "regex": "[A-Z]+"},
    "subproducts": {"type": "list", "schema": "product_schema"}
}

schema_registry.add("product_schema", product_schema)
validator = Validator(product_schema)
于 2019-06-02T20:16:20.913 回答
-1

JSON Schema Specification 中定义的“$ref”关键字可能适合您的需要。

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "required": ["product_number", "product_version", "subproducts"],
    "properties": {
        "product_number": {
            "type": "string"
        },
        "product_version": {
            "type": "string"
        },
        "subproducts": {
            "type": "array",
            "items": {
                "$ref": "#"
            }
        }
    }
}

如果当且仅当兄弟不为空时属性"product_number"和是必需的,则应修改 JSON 模式,如下所示:"product_version""subproducts"

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "required": ["subproducts"],
    "properties": {
        "subproducts": {
            "type": "array",
            "items": {
                "$ref": "#"
            }
        }
    },
    "anyOf": [
        {
            "properties": {
                "subproducts": {
                    "maxItems": 0
                }
            }
        },
        {
            "required": ["product_number", "product_version"],
            "properties": {
                "subproducts": {
                    "minItems": 1
                },
                "product_number": {
                    "type": "string"
                },
                "product_version": {
                    "type": "string"
                }
            }
        }
    ]
}
于 2019-05-30T12:13:14.607 回答