よくある質問

このスキーマはデフォルト属性を持っているのに実際にはインスタンス上でデフォルトを設定してくれないのはなぜですか?

デフォルトのバリデータが実際に何かをしている必要はないというのが仕様です。

For an inkling as to why it doesn’t actually do anything, consider that none of the other validators modify the instance either. More importantly, having default modify the instance can produce quite peculiar things. It’s perfectly valid (and perhaps even useful) to have a default that is not valid under the schema it lives in! So an instance modified by the default would pass validation the first time, but fail the second!

Still, filling in defaults is a thing that is useful. jsonschema allows you to define your own validators, so you can easily create a IValidator that does do default setting. Here’s some code to get you started:

from jsonschema import Draft4Validator, validators


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

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

        for property, subschema in properties.iteritems():
            if "default" in subschema:
                instance.setdefault(property, subschema["default"])

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


DefaultValidatingDraft4Validator = extend_with_default(Draft4Validator)


# Example usage:
obj = {}
schema = {'properties': {'foo': {'default': 'bar'}}}
# Note jsonschem.validate(obj, schema, cls=DefaultValidatingDraft4Validator)
# will not work because the metaschema contains `default` directives.
DefaultValidatingDraft4Validator(schema).validate(obj)
assert obj == {'foo': 'bar'}

See the above-linked document for more info on how this works, but basically, it just extends the properties validator on a Draft4Validator to then go ahead and update all the defaults.

If you’re interested in a more interesting solution to a larger class of these types of transformations, keep an eye on Seep, which is an experimental data transformation and extraction library written on top of jsonschema.

jsonschema のバージョン番号のしくみは?

jsonschemaSemantic Versioning の仕様に準拠しています。

広く広報互換がない変更はマイナーリリースでリリースする必要があります。(and certainly not in dot releases) ※ コレ翻訳出来なかった。

全体像は後方互換性のない変更を構成するものを定義する必要があります。

以下は公開APIと考えられる簡単な例です。従ってメジャーバージョン番号をバンプすることなく変更されるべきではありません。

  • モジュール名と内容、Pythonのコンベンションではプライベートとしてはマークされません (先頭に1つのアンダー スコア)

  • 関数とオブジェクトのシグネーチャ (パラメータの順序と名前)

次は非公開APIです。これらは予告なく変更する場合があります。

  • the exact wording and contents of error messages; typical reasons to do this seem to involve unit tests. API users are encouraged to use the extensive introspection provided in ValidationErrors instead to make meaningful assertions about what failed.
  • バリデーションエラーが返されるか送出される順序

  • 非公開設定

With the exception of the last of those, flippant changes are avoided, but changes can and will be made if there is improvement to be had. Feel free to open an issue ticket if there is a specific issue or question worth raising.