Python jsonschema 无法验证字符串枚举

标签 python json enums jsonschema

所以,我正在尝试为一组轴约束定义一个模式。因此,我想将“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"]
        }
    }
}

关于Python jsonschema 无法验证字符串枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25916555/

相关文章:

java - 是否可以将 Java-Enum 作为参数从 Cucumber 功能文件传递(以更文本友好的方式)?

python - 如何通过 Python 将存储在文本文件中的数据加载到 PostgreSQL 数据库中

java - 如何创建抽象枚举?

python csv模块读取用逗号分隔的csv,但忽略双引号或单引号内的逗号

PHP: Json解码错误

c# - 如果使用 C# 的内容长度 > 7kb,则无法在 WebRequest 上发布

json - 在 HTTP 响应中处理接口(interface)的最佳方式

c++ - 枚举,命名空间和命名空间之外的声明导致 "call of overloaded (...) is ambiguous"

python - MTurk API : Unable to see my created HIT on dashboard

python - 简化Python中的Ctype联合(在Windows中发送键盘事件)