Source code for django_sorcery.validators.runner

"""Validation runner."""

from django.core.exceptions import NON_FIELD_ERRORS, ValidationError

from ..exceptions import NestedValidationError

[docs]class ValidationRunner: """Helper class that can execute a bunch of validators on a given object.""" def __init__(self, name=None, validators=None, errors=None): = name or NON_FIELD_ERRORS self.validators = validators or [] self.errors = errors or {}
[docs] def is_valid(self, value, raise_exception=False): """Runs all validators on given value and collect validation errors.""" for v in self.validators: try: v(value) except ValidationError as e: e = NestedValidationError(e) try: e.error_list except AttributeError: self.errors = e.update_error_dict(self.errors) else: self.errors.setdefault(, []).extend(e.error_list) if self.errors and raise_exception: raise NestedValidationError(self.errors) return not self.errors