Skip to content

Use properties defaults #22

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
jcollado opened this issue Aug 1, 2012 · 3 comments
Closed

Use properties defaults #22

jcollado opened this issue Aug 1, 2012 · 3 comments

Comments

@jcollado
Copy link

jcollado commented Aug 1, 2012

When using a schema that has some default values for properties, the validation
passes when those properties are missing.

That's completely fine, but I'd expect also that when a property is missing,
the data passed to the validator is overwritten to set the property to the
default value according to the schema.

For example:

>>> schema = {'type': 'object',
              'properties': {
              'answer': {'type': 'integer', 'default': 42},
              },
             }
>>> data = {}
>>> jsonschema.validate(data, schema)
>>> print data
{}

What I'd expect is:

>>> print data
{'answer': 42}

I realize that if the schema type doesn't match a mutable type, this won't work
because the jsonschema.validate won't be able to modify the data in place. In such a
case, maybe a change in the API so that it returns the new data would be fine:

>>> schema = {'type': 'integer',
              'default': 42,
             }
>>> new_data = jsonschema.validate(0, schema)
>>> print new_data
42
@Julian
Copy link
Member

Julian commented Aug 1, 2012

Hi!

This is a duplicate of #4, you can check that out for one or two examples of some code that does this. The JSON Schema specification doesn't specify that the default property should do this, and by default, IMHO, it shouldn't :). Since it's relatively easy to do this yourself (see there), I think that should be good. Let me know if that works for you.

Julian.

@Julian Julian closed this as completed Aug 1, 2012
@jcollado
Copy link
Author

jcollado commented Aug 1, 2012

@Julian Thanks for the reference to issue #4. Yes, that fixes my problem indeed.

@Julian
Copy link
Member

Julian commented Aug 1, 2012

Glad to hear it :)! Thanks.

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

No branches or pull requests

2 participants