Skip to content

Instantly share code, notes, and snippets.

@cjw296
Last active April 21, 2016 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjw296/6384d1e0e9d2a4d14085 to your computer and use it in GitHub Desktop.
Save cjw296/6384d1e0e9d2a4d14085 to your computer and use it in GitHub Desktop.
Voluptuous double validate trick
# schema as follows, currently:
>>> schema = Schema({'logging': {Required('file_level', default='info'): str,
... Required('console_level', default='info'): str}})
# this works as desired:
>>> schema({'logging': {'file_level': 'warning'}})
{'logging': {'file_level': 'warning', 'console_level': info'}}
# this doesn't:
>>> schema({}})
{}
# what's the recommended way to have this happen:
>>> schema({}})
{'logging': {'file_level': 'info', 'console_level': info'}}
@alecthomas
Copy link

Check this glorious hack out:

>>> schema = Schema({Required('logging', default={}): {Required('file_level', default='info'): str,
                              Required('console_level', default='info'): str}})
>>> schema({})
    {'logging': {}}
>>> schema(schema({}))
    {'logging': {'console_level': 'info', 'file_level': 'info'}}

@cjw296
Copy link
Author

cjw296 commented Oct 23, 2015

Could Voluptuous do this when it sees something like:

schema = Schema({Required('logging', default=DictWithDefaults): {Required('file_level', default='info'): str,
                              Required('console_level', default='info'): str}})

Can't think of a good name...

@cjw296
Copy link
Author

cjw296 commented Oct 23, 2015

schema = Schema({Required('logging', default=PopulateWithDefaults({})): {Required('file_level', default='info'): str,
                              Required('console_level', default='info'): str}})

?

@cjw296
Copy link
Author

cjw296 commented Oct 23, 2015

schema = Schema({Required('logging', default=Populate({})): {
                                   Required('file_level', default='info'): str,
                                   Required('console_level', default='info'): str
}})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment