Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DATA = {
- 'name' : 'Christian',
- 'address' : {
- 'street' : "Straße",
- 'city' : "Aachen",
- 'zip' : 12345
- },
- 'more' : [
- {
- 'street' : "Straße 1",
- 'city' : "Aachen 1",
- 'zip' : 1
- },
- {
- 'street' : "Straße 2",
- 'city' : "Aachen 2",
- 'zip' : 2
- },
- {
- 'street' : "Straße 3",
- 'city' : "Aachen 3",
- 'zip' : 3
- },
- ],
- 'additional' : "foobar",
- }
- ERROR = {
- 'address' : {
- 'street' : "Straße",
- 'city' : "Aachen",
- 'zip' : 12345
- },
- 'more' : [
- {
- 'street' : "Straße 1",
- 'city' : "Aachen 1",
- 'zip' : 1
- },
- {
- 'street' : "Straße 2",
- 'city' : "Aachen 2",
- 'zip' : 2
- },
- {
- 'street' : "Straße 3",
- 'city' : "Aachen 3",
- 'zip' : 3
- },
- ],
- 'additional' : "foobar",
- }
- from cerberus import Validator
- address_schema = {
- 'street': {'type': 'string', 'required': True, 'minlength': 2},
- 'city': {'type': 'string', 'required': True, 'minlength': 2},
- 'zip': {'type': 'integer', 'required': True, 'max': 99999}
- }
- schema = {
- 'name': {'type': 'string', 'required': True, 'minlength': 2},
- 'address': {
- 'type': 'dict',
- 'schema': address_schema,
- },
- 'hello' : {'type' : 'string', 'default': 'nose'},
- 'more' : {
- 'type' : 'list',
- 'schema' : {
- 'type' : 'dict',
- 'schema': address_schema,
- },
- }
- }
- v = Validator(schema, purge_unknown=True)
- print(v.validate(DATA))
- import pprint
- pprint.pprint(v.document)
- print(v.validate(ERROR))
- print(v.errors)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement