Created
May 26, 2017 14:46
-
-
Save btall/e07bc025021981779d3fe3909466006f to your computer and use it in GitHub Desktop.
colander empty mappingschema doesn't works
This file contains hidden or 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
#!/usr/bin/env python | |
from __future__ import print_function | |
import sys | |
import colander | |
person_1 = { | |
'firstname': 'foo', | |
'lastname': 'bar', | |
'address': {} | |
} | |
person_2 = { | |
'firstname': 'foobar', | |
'lastname': 'barbaz', | |
'address': { | |
'city': 'Paris', | |
'country': 'France', | |
'zipcode': 75009 | |
} | |
} | |
class Address(colander.MappingSchema): | |
city = colander.SchemaNode(colander.String()) | |
country = colander.SchemaNode(colander.String()) | |
zipcode = colander.SchemaNode(colander.Integer()) | |
class Person(colander.Schema): | |
lastname = colander.SchemaNode(colander.String()) | |
firstname = colander.SchemaNode(colander.String()) | |
address = Address(require=False) | |
def main(): | |
validator = Person() | |
for person in (person_1, person_2): | |
try: | |
cstruct = validator.deserialize(person) | |
except colander.Invalid as err: | |
print(err, file=sys.stderr) | |
else: | |
print('person={} cstruct={}'.format(person, cstruct)) | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment