Skip to content

Instantly share code, notes, and snippets.

@mrtopf
Created February 19, 2019 16:45
Show Gist options
  • Save mrtopf/430d4d00a00735c29a88703e0292b4cc to your computer and use it in GitHub Desktop.
Save mrtopf/430d4d00a00735c29a88703e0292b4cc to your computer and use it in GitHub Desktop.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment