Skip to content
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

Making a key required while extending a dictionary validation schema fails #192

Closed
balloob opened this issue Aug 13, 2016 · 2 comments
Closed

Comments

@balloob
Copy link
Contributor

balloob commented Aug 13, 2016

Running voluptuous 0.9.2

Extending a dictionary validating schema will not work if one of the existing keys is overwritten and made required.

Given this code, you would expect both schema's to pass the data:

import voluptuous as vol

schema1 = vol.Schema({'platform': str})
schema2 = schema1.extend({vol.Required('platform'): str})

data = {'platform': 'hello'}

print('schema1')
print(schema1(data))
print('schema2')
print(schema2(data))

However, an invalid exception is raised for schema2:

› python3 pm_test.py
schema1
{'platform': 'hello'}
schema2
Traceback (most recent call last):
  File "pm_test.py", line 20, in <module>
    print(schema2(data))
  File "/Users/paulus/dev/python/home-assistant/lib/python3.4/site-packages/voluptuous-0.9.2-py3.4.egg/voluptuous/schema_builder.py", line 192, in __call__
  File "/Users/paulus/dev/python/home-assistant/lib/python3.4/site-packages/voluptuous-0.9.2-py3.4.egg/voluptuous/schema_builder.py", line 486, in validate_dict
  File "/Users/paulus/dev/python/home-assistant/lib/python3.4/site-packages/voluptuous-0.9.2-py3.4.egg/voluptuous/schema_builder.py", line 324, in validate_mapping
voluptuous.error.MultipleInvalid: required key not provided @ data['platform']
@sfelf
Copy link

sfelf commented Aug 13, 2016

This is the same behavior if you declare both the Optional and the Required in the initial Schema.

>>> from voluptuous import Required, Schema
>>> s = Schema({'test': str, Required('test'): str}, required=False)
>>> s({'test': 'this'})
Traceback (most recent call last):
  File "<input>", line 3, in <module>
  File "/Users/test/virtual/test/lib/python3.4/site-packages/voluptuous.py", line 337, in __call__ 
    return self._compiled([], data)
  File "/Users/test/virtual/test/lib/python3.4/site-packages/voluptuous.py", line 635, in validate_dict
    return base_validate(path, iteritems(data), out)
  File "/Users/test/virtual/test/lib/python3.4/site-packages/voluptuous.py", line 471, in validate_mapping
    raise MultipleInvalid(errors)
voluptuous.MultipleInvalid: required key not provided @ data['test']

@tusharmakkar08
Copy link
Collaborator

Closed via 99fb7bd

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

3 participants