Skip to content

Issue in a FAQ answer (default property) #742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
iuridiniz opened this issue Sep 23, 2020 · 3 comments
Closed

Issue in a FAQ answer (default property) #742

iuridiniz opened this issue Sep 23, 2020 · 3 comments
Labels
Enhancement Some new desired functionality

Comments

@iuridiniz
Copy link

Hello guys,

In your FAQ, you provide a sample that shows how to make default property set a default value.

If the schema has a 'required' property and it comes before the 'properties' property, a jsonschema.exceptions.ValidationError will be raised.

I think that documentation must be updated in order to avoid this kind of error by someone else. I don't think that this kind of error must be fixed in this project, I think this is outside the scope of the project.

Here a sample:

from jsonschema import Draft7Validator, validators


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 validators.extend(
        validator_class, {"properties" : set_defaults},
    )


DefaultValidatingDraft7Validator = extend_with_default(Draft7Validator)


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

# no raises here
obj = {}
schema_required_after_properties = {'properties': {'foo': {'default': 'bar'}, 'required': 'foo'}}
DefaultValidatingDraft7Validator(schema_required_after_properties).validate(obj)
assert obj == {'foo': 'bar'}

# this code will raise jsonschema.exceptions.ValidationError
obj = {}
schema_required_before_properties = {'required': 'foo', 'properties': {'foo': {'default': 'bar'}}}
DefaultValidatingDraft7Validator(schema_required_before_properties).validate(obj)
assert obj == {'foo': 'bar'}
@Julian
Copy link
Member

Julian commented Sep 23, 2020

Hi! Thanks for the report.

I'm probably happy to consider a PR to fix this, though to be honest I'm not sure what the expected fix is -- this way of making default work isn't really JSON Schema (i.e. it's not part of the specification), so it's more about what someone doing it expects. jsonschema does give tools to allow for implementing whatever behavior is desired here, but I could imagine someone expecting the error you're flagging here -- that required means "you have to have provided it, otherwise you get a validation error". I could imagine someone not expecting that too, so both seem valid, and yeah this isn't part of the spec, so I don't know which behavior someone coming to that page is looking for (in fact personally, I've maintained I'm skeptical of this whole realm -- I think jsonschema is good at validation. If someone wants validation + coersion/instantiation/blueprinting whatever you want to call this as either a pre-step or post-step).

But yeah if there's a trivial improvement to be made to the example code, definitely happy to consider it!

#367 is also relevant.

@Julian Julian added the Enhancement Some new desired functionality label Oct 3, 2020
@timgkeller
Copy link

Hello Julian,

I found another issue with the given example which can be observed when using it in combination with the anyOf keyword. I have provided a detailed example and error description on stackoverflow.

Best regards,
Tim

@Julian
Copy link
Member

Julian commented Nov 19, 2020

Going to close this given the above, but happy to hear follow-ups.

@timgkeller if you've found another issue you believe to be a bug, happy to see another issue about it, or better a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Some new desired functionality
Projects
None yet
Development

No branches or pull requests

3 participants