Support additional (Python) types in more than the `type` validator
还没有人认领这个 Issue。
评估
调研方向
首先阅读 issue #364 及其相关的 pull request,然后使用 required 和 additionalProperties validators 重现该 issue 中的 NamedTuple 示例。定义应如何一致地识别和访问 NamedTuple 属性,并在两个示例都能验证预期属性且不将其值当作属性名称时,视为工作完成。
由索引模型根据 Issue 内容生成。
描述
In issue #364 and the related PR the type checker was expanded to allow redefining how jsonschema identifies/validates a type, with the intention of allowing a named tuple to be used as a jsonschema "object" type. In practice, this does not appear to be enough, as other portions of jsonschema use the python "in" expression to test for or iterate over properties in an object. In a NamedTuple, this will actually test the values of the properties. Two specific examples (required and additionalProperties) highlight this:
from typing import NamedTuple
from jsonschema import Draft7Validator
from jsonschema.validators import extend
from jsonschema.exceptions import ValidationError
# dict or tuple w/ _fields is object (allowed NamedTuple as object)
def is_object_or_namedtuple(checker, instance):
return isinstance(instance, dict) or (
isinstance(instance, tuple) and hasattr(instance, '_fields'))
# redefine object to allow namedtuple
type_checker = Draft7Validator.TYPE_CHECKER.redefine(
'object', is_object_or_namedtuple)
# extend validator with new type checker
Validator = extend(Draft7Validator, type_checker=type_checker)
# validator with foo as required property
validator_foo_required = Validator(schema={
"type": "object",
"properties": {
"foo": {
"type": "string"
}
},
"required": ["foo"]
})
# validator with foo as optional property, no additional
validator_foo_optional_no_additional = Validator(schema={
"type": "object",
"properties": {
"foo": {
"type": "string"
}
},
"additionalProperties": False
})
class Example(NamedTuple):
foo: str
example = Example(foo="bar")
try:
validator_foo_required.validate(example)
except ValidationError as e:
print(f"Error: {e.message}")
try:
validator_foo_optional_no_additional.validate(example)
except ValidationError as e:
print(f"Error: {e.message}")
# the problem is that 'foo' is not "in" example..
print('Is foo in example? ', ('foo' in example))
# ... but 'bar' is
print('Is bar in example? ', ('bar' in example))
This is more of an FYI, as #364 was targeted at supporting NamedTuple, but it looks like there would be some bigger changes required to facilitate redefining how properties are identified/accessed in the NamedTuple.
- 主要语言
- Python
- 星标
- 5k
- 派生
- 706
- 平均合并
- 2 天 1 小时
- 30 天内合并 PR
- 5
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
python-jsonschema/jsonschema 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 65/100
python-jsonschema/jsonschema#1588 · 2 条评论 ·
-
Iterator error contexts lose parent links and absolute paths可能重新可做 @DawnofGenX 于 17 天前认领,目前没有进行中的 PR。 未关闭
难度 2/5 1-3 小时 新手友好度 76/100
python-jsonschema/jsonschema#1584 · 3 条评论 ·
-
regex format checker crashes with RecursionError on deeply nested patterns可能已有人在做 @DawnofGenX 于 17 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
python-jsonschema/jsonschema#1538 · 1 条评论 ·
-
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 条评论 ·
-
难度 3/5 1-2 天 新手友好度 55/100
python-jsonschema/jsonschema#1590 · 5 条评论 ·
查看 python-jsonschema/jsonschema 的全部 Issue
相似的 Issue
-
good first issue priority: low size: XS testing
难度 1/5 1-3 小时 新手友好度 86/100
cubrid-lab/cubrid-mcp-server#385 ·
维护者通常 1 天内回复
-
good first issue priority: low size: S testing
难度 2/5 1-3 小时 新手友好度 85/100
cubrid-lab/cubrid-cookbook-python#447 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
ArduCAM/Arducam_tof_camera#125 ·
-
area:curator enhancement
难度 2/5 1-3 小时 新手友好度 74/100
CassioDalla/Scrinalia#150 ·
维护者通常 1 天内回复
-
documentation good first issue
难度 2/5 1-3 小时 新手友好度 72/100