unevaluatedProperties error gets raised before more relevant validation errors
维护者通常 6 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 35/100
- Issue 类型
- 缺陷
- 描述清晰度
- 基本清楚
- 活跃度
- 停滞
- 技术栈
- python
调研方向
使用 jsonschema.validate 和 Draft202012Validator.validate 重现所提供的 schema,然后检查 jsonschema/_utils.py 中的 find_evaluated_property_keys_by_schema 以及 jsonschema/exceptions.py 中的错误选择逻辑。比较产生的错误,并确定 unevaluatedProperties 应如何避免掩盖更相关的 subschema 验证错误;当示例始终报告最大违规时即表示完成。
由索引模型根据 Issue 内容生成。
描述
Hello!
I noticed that in schemas using 'allOf' and unevaluatedProperties, when there is an error in a subschema, instead of showing the relevant error, unevaluatedProperties is (sometimes?) shown instead. This makes it really hard to see what went wrong in anything larger than trivial examples. This seems to be related to https://github.com/python-jsonschema/jsonschema/issues/1074, just with a different scenario.
Here is a small example:
import jsonschema
obj = {
"fridge_min_temp": 10
}
schema = {
"type": "object",
"allOf": [
{
"properties": {
"fridge_min_temp": { "type": "integer", "minimum": -20, "maximum": 5 }
},
"required": ["fridge_min_temp"]
}
],
"unevaluatedProperties": False
}
jsonschema.validate(obj, schema)
Which produces the error trace:
ValidationError: Unevaluated properties are not allowed ('fridge_min_temp' was unexpected)
Failed validating 'unevaluatedProperties' in schema:
{'allOf': [{'properties': {'fridge_min_temp': {'maximum': 5,
'minimum': -20,
...
'type': 'object',
'unevaluatedProperties': False}
On instance:
{'fridge_min_temp': 10}
If I validate instead using:
jsonschema.Draft202012Validator(schema).validate(obj)
Then the raised error is much more helpful:
ValidationError: 10 is greater than the maximum of 5
Failed validating 'maximum' in schema['allOf'][0]['properties']['fridge_min_temp']:
{'maximum': 5, 'minimum': -20, 'type': 'integer'}
On instance['fridge_min_temp']:
10
Seems like this happens because jsonschema.validate is equivalent to raise best_match(jsonschema.Draft202012Validator(schema).iter_errors(obj)) in this case, and the best_match heuristic downgrades any errors coming from 'allOf' and 'oneOf'.
With a more complex schema, I get an unevaluatedProperties error using both approaches.
3 ways of going about it could be:
-
The suggestion from https://github.com/python-jsonschema/jsonschema/issues/1074 to separately keep track of unevaluated properties and invalid properties, and write different error messages for them.
-
Add properties to evaluated keys even if there are errors resulting from
validator.descendin https://github.com/python-jsonschema/jsonschema/blob/main/jsonschema/_utils.py:
def find_evaluated_property_keys_by_schema(validator, instance, schema):
# ...
for keyword in ["allOf", "oneOf", "anyOf"]:
if keyword in schema:
for subschema in schema[keyword]:
errs = next(validator.descend(instance, subschema), None)
if errs is None:
evaluated_keys += find_evaluated_property_keys_by_schema(
validator, instance, subschema,
)
This way unevaluatedProperties will not get raised for properties with subschema errors at all (which shouldn't be a problem, because it'll get raised when those errors are fixed).
- Make
unevaluatedPropertiesmessages even weaker than "allOf" and "anyOf" related errors in https://github.com/python-jsonschema/jsonschema/blob/main/jsonschema/exceptions.py#L19
I'd be happy to attempt a PR; do you have any thoughts about which approach is best?
- 主要语言
- Python
- 星标
- 5k
- 派生
- 741
- 平均合并
- 4 天 17 小时
- 30 天内合并 PR
- 6
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python-jsonschema/jsonschema 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
python-jsonschema/jsonschema#1610 · 1 条评论 ·
维护者通常 6 天内回复
-
难度 2/5 1-3 小时 新手友好度 65/100
python-jsonschema/jsonschema#1588 · 2 条评论 ·
维护者通常 6 天内回复
-
Iterator error contexts lose parent links and absolute paths可能重新可做 @DawnofGenX 于 59 天前认领,目前没有进行中的 PR。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
python-jsonschema/jsonschema#1584 · 3 条评论 ·
维护者通常 6 天内回复
-
regex format checker crashes with RecursionError on deeply nested patterns可能已有人在做 @DawnofGenX 于 59 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
python-jsonschema/jsonschema#1538 · 1 条评论 ·
维护者通常 6 天内回复
-
is_duration raises an uncaught decimal.Overflow on a duration with a large exponent可能重新可做 关联的 PR 已关闭且未合并。 未关闭Needs Test Upstream
难度 2/5 1-3 小时 新手友好度 78/100
python-jsonschema/jsonschema#1511 · 1 条评论 ·
维护者通常 6 天内回复
查看 python-jsonschema/jsonschema 的全部 Issue
相似的 Issue
-
难度 1/5 1-3 小时 新手友好度 78/100
web-push-libs/pywebpush#216 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 72/100
AgriciDaniel/claude-seo#572 ·
维护者通常 5 天内回复
-
area:print-dispatch
难度 2/5 1-3 小时 新手友好度 92/100
维护者通常 1 天内回复
-
area: docs domain: technical-writing good first issue type: documentation
难度 2/5 1-3 小时 新手友好度 80/100
Deepak3699/Ai_Mentor#919 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复