Skip to content

Instantly share code, notes, and snippets.

@nicolaiarocci
Created January 5, 2015 10:09
Show Gist options
  • Save nicolaiarocci/829c98eb5f8b4e9c96c1 to your computer and use it in GitHub Desktop.
Save nicolaiarocci/829c98eb5f8b4e9c96c1 to your computer and use it in GitHub Desktop.
Validating complex user objects with Cerberus
import copy
from cerberus import Validator
from cerberus import errors
from collections import Mapping, Sequence
class ObjectValidator(Validator):
def __init__(self, *args, **kwargs):
self.allow_unknown = True
super(ObjectValidator, self).__init__(*args, **kwargs)
def validate_object(self, obj):
return self.validate(obj.__dict__)
def _validate_type_object(self, field, value):
# objects which are not Mapping or Sequence types are allowed.
# (Mapping and Sequence types are dealt elsewhere.)
if not (isinstance(value, object) and \
not isinstance(value, (Sequence, Mapping))):
self._error(field, errors.ERROR_BAD_TYPE % "object")
def _validate_schema(self, schema, field, value):
if isinstance(value, (Sequence, Mapping)):
super(ObjectValidator, self)._validate_schema(schema, field, value)
elif isinstance(value, object):
validator = copy.copy(self)
validator.schema = schema
validator.validate(value.__dict__, context=self.document)
if len(validator.errors):
self._error(field, validator.errors)
@liberforce
Copy link

if len(validator.errors):

if validator.errors:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment