Last active
August 29, 2015 14:21
-
-
Save misja/d9b88ea849b9ab86af95 to your computer and use it in GitHub Desktop.
A data cleaning cerberus validator (remove unknown fields)
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
import copy | |
import collections | |
from cerberus import Validator | |
class CleaningValidator(Validator): | |
def validate_clean(self, document, *args, **kwargs): | |
self.validate(document, *args, **kwargs) | |
if self.errors and self.allow_unknown == False: | |
self._clean() | |
return len(self.errors) == 0 | |
def _clean(self, document=None, errors=None, context=(None, {})): | |
if not document: | |
document = self.document | |
if not errors: | |
errors = self._errors | |
key, parent = context | |
clean_errors = copy.copy(errors) | |
for k, v in clean_errors.items(): | |
if v == 'unknown field': | |
del(document[k]) | |
del(errors[k]) | |
if isinstance(v, collections.Mapping): | |
self._clean(document[k], errors[k], (k, errors)) | |
if key and parent: | |
if not parent.get(key): | |
del(parent[key]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment