Skip to content

Instantly share code, notes, and snippets.

@michaelbartnett
Last active December 17, 2015 07:09
Show Gist options
  • Save michaelbartnett/5570704 to your computer and use it in GitHub Desktop.
Save michaelbartnett/5570704 to your computer and use it in GitHub Desktop.
Breaking Voluptuous
from voluptuous import Schema, Required, Range
schema = Schema({
Required('a'): 1,
Required('b'): {
Range(min=1,max=20): [
0,
{
Required('foo'): 42
},
2
]
}
})
def geterr(doc):
print(doc)
try:
schema(doc)
except Exception as exc:
print('\n'.join(['{}: {}'.format(e.msg, e.path) for e in exc.errors]))
print('')
geterr({
'b': {
1: ['no'],
-1: 'more no'
}
})
# invalid list value: ['b', 1, 0]
# required key not provided: ['b']
# required key not provided: ['a']
geterr({
'b': {
999: ['no'],
-1: 'more no'
}
})
# extra keys not allowed: ['b', -1]
# required key not provided: ['b']
# required key not provided: ['a']
geterr({
'b': {
999: ['no']
}
})
# extra keys not allowed: ['b', 999]
# required key not provided: ['b']
# required key not provided: ['a']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment