如何使用jsonschema和python

问题描述 投票:0回答:1

我想使用jsonschema软件包对其进行验证。 我创建了这样的架构:

schema = {
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "key1": {
                "type": "boolean"
            },
            "key2": {
                "type": "number"
            }
        },
        "required": ["enabled"]
    }
}

但这对我的列表不正确,因为要使它有效,我的列表应该是这样:

list_dict = [{'key1': True, 'key2': 0.5}]

如何创建正确的模式来验证我的列表?
预先感谢您。
    

我认为您可能想使用
oneOf
构造。 基本上,您正在尝试描述一个可以包含两个不同类型的对象的列表。 there是一个使用的示例:

{ "type": "array", "items": { "$ref": "#/defs/element" }, "$defs": { "element": { "type": "object", "oneOf": [ { "$ref": "#/$defs/foo" }, { "$ref": "#/$defs/bar" } ] }, "foo": { "type": "object", "additionalProperties": false, "properties": { "key1": { "type": "boolean" } }, "required": [ "key1" ] }, "bar": { "type": "object", "additionalProperties": false, "properties": { "key2": { "type": "boolean" } }, "required": [ "key2" ] } } }
python jsonschema python-jsonschema
1个回答
5
投票
也有可能对您有用的组合。 查看

jSonschema关于组合的文档

有关更多信息。
    

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.