Last active
December 17, 2015 07:09
-
-
Save michaelbartnett/5570704 to your computer and use it in GitHub Desktop.
Breaking Voluptuous
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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