Created
January 5, 2015 10:09
-
-
Save nicolaiarocci/829c98eb5f8b4e9c96c1 to your computer and use it in GitHub Desktop.
Validating complex user objects with Cerberus
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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
if len(validator.errors):
→
if validator.errors: