繁体   English   中英

Python jsonschema无法验证字符串枚举

[英]Python jsonschema fails to validate enum of strings

因此,我正在尝试为一组轴约束定义一个架构。 因此,我想将“ axis”元素的可能值限制为[“ x”,“ y”,“ z”]。

这是我当前的示例,它是输出。

JSON:

{
    "name_patterns": [
        {
            "regex": "block[_-]?([\\d]*)",
            "class": "block",
            "id_group": 1
        }
    ],

    "relationships": [
        {
            "src_class": "block",
            "dst_class": "block",
            "constraints": {
                "axis": "x"
            }
        }
    ]
}

架构:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "name_patterns": {"type":  "array",
                          "items": { "$ref": "#/definitions/name_entry" } },
        "relationships": {"type":  "array",
                          "items": { "anyof": [ {"$ref": "#/definitions/relation"} ] } }
    },

    "definitions": {
        "name_entry": {
            "type": "object",
            "properties": {
                "regex": {"type": "string"},
                "class": {"type": "string"},
                "id_group": {"type": "number"}
            },
            "required": ["regex", "class"]
        },
        "relation": {
            "type": "object",
            "properties": {
                "src_class": {"type": "string"},
                "dst_class": {"type": "string"},
                "constraints": {
                    "type": "object",
                    "properties": {
                        "axis": {
                            "enum": ["x", "y", "z"]
                        }
                    },
                    "required": ["axis"]
                }
            },
            "required": ["src_class", "dst_class", "constraints"]
        }

    }
}

如何修复我的架构以拒绝枚举器中未指定的值?

您的架构语法有些偏离。

首先,您需要将属性定义放入properties

{
    "properties": {
        "axis": {...}
    }
}

其次, type定义了类型(例如"string" ),但是您在这里不需要它。 enum应直接位于"axis"模式内:

{
    "properties": {
        "axis": {
            "enum": ["x", "y", "z"]
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM