如何验证具有丰富性的词典列表?

时间:2019-09-23 08:37:19

标签: python voluptuous

我正在尝试验证具有丰富含义的字典列表,并使用错误消息捕获无效值,但似乎不支持验证这种嵌套数据。如果可以,我应该如何更改代码以满足自己的需求?

我使用python3.7和voluptuous == 0.11.5。

这样的代码:

from voluptuous import Required, Schema, MultipleInvalid

schema = Schema(
    [{Required("a"): {Required("a1"): int}, Required("b"): {Required("b1"): int}}]
)

data_test = [
    1,
    {"a": {"a1": 1}, "b": {"b1": 2}},
    {"a": {"a1": 3}, "b": {"b1": "4"}},
    {"a": {"a1": "5"}, "b": {"b1": 6}},
]

try:
    data_test = schema(data_test)
    print(data_test)
except MultipleInvalid as e:
    list_error = [str(m) for m in e.errors]
    exc = e.error_message
    print(str(list_error))

输出为:

["expected int for dictionary value @ data[2]['b']['b1']"]

我期望的是

["expected a dictionary @ data[0]", "expected int for dictionary value @ data[2]['b']['b1']", "expected int for dictionary value @ data[3]['a']['a1']"]

上面的代码有什么问题吗?

0 个答案:

没有答案