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 小時以內 新手友好度 92/100
Techiebutler/freeframe#687 ·
維護者通常 1 天內回覆
-
bug verbose GC
難度 2/5 1-3 小時 新手友好度 74/100
IBM/javacore-analyser#677 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 79/100
維護者通常 2 天內回覆
-
area/config comp/plugins P1 platform/qqbot tool/file type/bug
難度 2/5 1-3 小時 新手友好度 75/100
NousResearch/hermes-agent#163932 · 1 則留言 ·
維護者通常 1 天內回覆
-
plugins/dsh: todayStr() computes UTC but is documented as local — journal entries written 00:00–08:00 local land in the previous day's file可能已有人在做 @Beehtrix 今天認領。 未關閉bug
難度 2/5 1-3 小時 新手友好度 82/100
zilliztech/memsearch#907 · 1 則留言 ·
維護者通常 1 天內回覆