python-jsonschema / jsonschema

An implementation of the JSON Schema specification for Python
https://python-jsonschema.readthedocs.io
MIT License
4.52k stars 574 forks source link

NewValidator created after descend does not use extended validators from parent #1283

Closed gobater closed 2 weeks ago

gobater commented 2 weeks ago

I have created a custom validator, Extending Draft7Validator to set defaults following the example in https://python-jsonschema.readthedocs.io/en/stable/faq/#why-doesn-t-my-schema-s-default-property-set-the-default-on-my-instance

    def _extend_validator_with_defaults(self, validator_class):
        original_validator = validator_class.VALIDATORS["properties"]

        def _properties_set_defaults(validator, properties, instance, schema):
            # set the defaults before anything else. Then we will validate also the defaults
            for property, subschema in properties.items():
                if "default" in subschema:
                    instance.setdefault(property, subschema["default"])

                if property in instance:
                    yield from validator.descend(
                        instance[property],
                        subschema,
                        path=property,
                        schema_path=property,
                    )

            yield from original_validator(validator, properties, instance, schema)

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

As soon as I descend into a reference (which contains a subschema), the custom validator is not called anymore.

Julian commented 2 weeks ago

Hi there, this seems likely to be a duplicate of #994.