Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Question about validators across $refs

オープン
#1,341 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
停滞
技術スタック
python

調査の方向性

報告されている outer.json、nested.json、script から始め、その後 jsonschema 4.9.1 と 4.10.0 に対して比較を実行してください。$ref をまたいでカスタム Draft4 バリデーターがどのように選択されるかを追跡し、$schema を削除せずに以前のデフォルト値の補完動作を維持できるかどうかを記録してください。

索引モデルが issue の本文から書いたものです。

説明

Hi, I have a question regarding the change in 4.10.0 and the default filling custom validator suggested here.

Before this version, I could use this enhanced validator to fill in default values even across "$ref"s to different schema.

But 4.10.0 added support "for referencing schemas with $ref across different versions of the specification", which means that now the validator is determined dynamically based on the "$schema" and the custom one is not used for the referred schema.

Here is an example:

outer:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "properties": {
        "count": {
            "default": 0,
            "type": "integer"
        },
        "nested": {
            "$ref": "file://nested.json"
        }
    }
}

nested:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "properties": {
        "nestedcount": {
            "default": 0,
            "type": "integer"
        },
        "name": {
            "type": "string"
        }
    }
}

script:

import copy
import json
import jsonschema

def extend_with_default(validator_class):
    validate_properties = validator_class.VALIDATORS["properties"]

    def set_defaults(validator, properties, instance, schema):
        for property, subschema in properties.items():
            if "default" in subschema:
                instance.setdefault(property, subschema["default"])

        for error in validate_properties(
            validator, properties, instance, schema,
        ):
            yield error

    return jsonschema.validators.extend(
        validator_class, {"properties" : set_defaults},
    )

Draft4ValidatorWithDefaults = extend_with_default(jsonschema.Draft4Validator)


def load_schema(uri):
	with open(uri.removeprefix("file://")) as ifile:
		return json.load(ifile)

schema = load_schema("outer.json")

resolver = jsonschema.RefResolver(
    "outer.json", schema,
    handlers={"file": load_schema})

validator_with_defaults = Draft4ValidatorWithDefaults(schema, resolver=resolver)
validator = jsonschema.Draft4Validator(schema, resolver=resolver)

content = {
	"nested": {"name": "foo"}
}

print(jsonschema.__version__)
validator.validate(content)
print(content)
validator_with_defaults.validate(content)
print(content)

When I run this with jsonschema 4.9.1, I get:

4.9.1
{'nested': {'name': 'foo'}}
{'nested': {'name': 'foo', 'nestedcount': 0}, 'count': 0}

when I use 4.10.0, it looks like this:

4.10.0
{'nested': {'name': 'foo'}}
{'nested': {'name': 'foo'}, 'count': 0}

My question is, is there a way to revert to the previous behavior? I found out that when I delete the "$schema" key from the referred schema, it works as before, but that doesn't seem exactly nice (and I am not sure that won't cause other issues)...

主要言語
Python
スター
5k
フォーク
700
平均マージ
1日 20時間
マージ済み PR(30日)
6

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python-jsonschema/jsonschema のほかの issue

python-jsonschema/jsonschema の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。