在JsonSchema验证错误中包含键值

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

我有一个json文件并创建了一个jsonSchema。现在我进行了验证。它工作正常。在错误消息中,它显示如下错误:

['id', 4180, 'name', 'amount']
2.0 is greater than the maximum of 1

我也要打印发生错误的ID号,以便可以轻松地在JSON文件中跟踪错误。

我尝试了以下代码:

jsondata = json.loads(jsonInput.read())

jsonschema = json.loads(schemaInput.read())

validator = jsonschema.Draft7Validator(jsonschema )

errors = sorted(validator.iter_errors(jsondata ),key=str)  # get all validation errors

error_count=1;   
for error in errors:
    print(error)

我该怎么做?

python json python-3.x python-2.7 json-schema-validator
1个回答
0
投票

您需要进一步检查得到的错误。

这里是the documentation的示例:

for error in errors:
    for suberror in sorted(error.context, key=lambda e: e.schema_path):
        print(list(suberror.schema_path), suberror.message, sep=", ")
© www.soinside.com 2019 - 2024. All rights reserved.