Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Support additional (Python) types in more than the `type` validator

未关闭
#727 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
30/100
Issue 类型
功能
描述清晰度
需要澄清
活跃度
停滞
技术栈
python
领域
backend

调研方向

首先阅读 issue #364 及其相关的 pull request,然后使用 required 和 additionalProperties validators 重现该 issue 中的 NamedTuple 示例。定义应如何一致地识别和访问 NamedTuple 属性,并在两个示例都能验证预期属性且不将其值当作属性名称时,视为工作完成。

由索引模型根据 Issue 内容生成。

描述

Dialects v2 Enhancement

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 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python-jsonschema/jsonschema 的其他 Issue

查看 python-jsonschema/jsonschema 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。