How to use the cerberus.errors.BasicErrorHandler function in Cerberus

To help you get started, we’ve selected a few Cerberus examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pyeve / cerberus / cerberus / errors.py View on Github external
for i, definition_errors in error.definitions_errors.items():
            if not definition_errors:
                continue

            nodename = '%s definition %s' % (error.rule, i)
            path = error.document_path + (nodename,)

            for child_error in definition_errors:
                rel_path = child_error.document_path[child_start:]
                child_error.document_path = path + rel_path

                self._rewrite_error_path(child_error, offset + 1)


class SchemaErrorHandler(BasicErrorHandler):
    messages = BasicErrorHandler.messages.copy()
    messages[0x03] = "unknown rule"
github pyeve / eve / eve / validation.py View on Github external
return self._config.get("document_id", None)

    @document_id.setter
    def document_id(self, value):
        self._config["document_id"] = value

    @property
    def persisted_document(self):
        return self._config.get("persisted_document", None)

    @persisted_document.setter
    def persisted_document(self, value):
        self._config["persisted_document"] = value


class SingleErrorAsStringErrorHandler(cerberus.errors.BasicErrorHandler):
    """ Default Cerberus error handler for Eve.

    Since Cerberus 1.0, error messages for fields will always be returned as
    lists, even in the case of a single error. To maintain compatibility with
    clients, this error handler will unpack single-element error lists unless
    the config item VALIDATION_ERROR_AS_LIST is True.
    """

    @property
    def pretty_tree(self):
        pretty = super(SingleErrorAsStringErrorHandler, self).pretty_tree
        self._unpack_single_element_lists(pretty)
        return pretty

    def _unpack_single_element_lists(self, tree):
        for field in tree:
github pyeve / cerberus / cerberus / validator.py View on Github external
def __init_error_handler(kwargs):
        error_handler = kwargs.pop('error_handler', errors.BasicErrorHandler)
        if isinstance(error_handler, tuple):
            error_handler, eh_config = error_handler
        else:
            eh_config = {}
        if isinstance(error_handler, type) and issubclass(
            error_handler, errors.BaseErrorHandler
        ):
            return error_handler(**eh_config)
        elif isinstance(error_handler, errors.BaseErrorHandler):
            return error_handler
        else:
            raise RuntimeError('Invalid error_handler.')
github indietyp / hawthorne / api / validation.py View on Github external
import re

from cerberus import Validator
from cerberus.errors import BasicErrorHandler
from django.utils.translation import gettext_lazy as _

from api import codes


class HumanReadableValidationError(BasicErrorHandler):
  messages = {0x00: "{0}",

              0x01: _("document is missing"),
              0x02: _("required field"),
              0x03: _("unknown field"),
              0x04: _("field '{0}' is required"),
              0x05: _("depends on these values: {constraint}"),
              0x06: _("{0} must not be present with '{field}'"),

              0x21: _("'{0}' is not a document, must be a dict"),
              0x22: _("empty values not allowed"),
              0x23: _("value needs to be supplied"),
              0x24: _("must be of {constraint} type"),
              0x25: _("must be of dict type"),
              0x26: _("length of list should be {constraint}, it is {0}"),
              0x27: _("min length is {constraint}"),
github pypa / pipenv / pipenv / vendor / cerberus / validator.py View on Github external
def __init_error_handler(kwargs):
        error_handler = kwargs.pop('error_handler', errors.BasicErrorHandler)
        if isinstance(error_handler, tuple):
            error_handler, eh_config = error_handler
        else:
            eh_config = {}
        if isinstance(error_handler, type) and issubclass(
            error_handler, errors.BaseErrorHandler
        ):
            return error_handler(**eh_config)
        elif isinstance(error_handler, errors.BaseErrorHandler):
            return error_handler
        else:
            raise RuntimeError('Invalid error_handler.')
github pyeve / cerberus / cerberus / base.py View on Github external
def __init__(
        self,
        schema: Schema = None,
        *,
        allow_unknown: AllowUnknown = False,
        error_handler: ErrorHandlerConfig = errors.BasicErrorHandler,
        ignore_none_values: bool = False,
        purge_unknown: bool = False,
        purge_readonly: bool = False,
        require_all: bool = False,
        **extra_config: Any
    ):
        self._config = extra_config  # type: Dict[str, Any]
        """ This dictionary holds the configuration arguments that were used to
            initialize the :class:`Validator` instance except the ``error_handler``. """
        self._config.update(
            {
                'allow_unknown': allow_unknown,
                'error_handler': error_handler,
                'ignore_none_values': ignore_none_values,
                'purge_readonly': purge_readonly,
                'purge_unknown': purge_unknown,
github pyeve / cerberus / cerberus / errors.py View on Github external
for i, definition_errors in error.definitions_errors.items():
            if not definition_errors:
                continue

            nodename = '%s definition %s' % (error.rule, i)
            path = error.document_path + (nodename,)

            for child_error in definition_errors:
                rel_path = child_error.document_path[child_start:]
                child_error.document_path = path + rel_path

                self._rewrite_error_path(child_error, offset + 1)


class SchemaErrorHandler(BasicErrorHandler):
    messages = BasicErrorHandler.messages.copy()
    messages[0x03] = "unknown rule"
github CentOS-PaaS-SIG / linchpin / linchpin / exceptions / __init__.py View on Github external
class TopologyError(LinchpinError):

    def __init__(self, *args, **kwargs):
        LinchpinError.__init__(self, *args, **kwargs)


class ActionError(LinchpinError):

    def __init__(self, *args, **kwargs):
        LinchpinError.__init__(self, *args, **kwargs)


class ValidationErrorHandler(cerberus_errors.BasicErrorHandler):
    messages = cerberus_errors.BasicErrorHandler.messages.copy()
    messages[cerberus_errors.REQUIRED_FIELD.code] = "field '{field}' is "\
        + "required"
    messages[cerberus_errors.UNKNOWN_FIELD.code] = "field '{field}' could not "\
        + "be recognized within the schema provided"
    messages[cerberus_errors.BAD_TYPE.code] = "value for field '{field}' must "\
        + "be of type '{constraint}'"
    messages[cerberus_errors.UNALLOWED_VALUE.code] = "unallowed value " \
        + "'{value}' for field '{field}'. Allowed values are: {constraint}"
github CentOS-PaaS-SIG / linchpin / linchpin / exceptions / __init__.py View on Github external
LinchpinError.__init__(self, *args, **kwargs)


class TopologyError(LinchpinError):

    def __init__(self, *args, **kwargs):
        LinchpinError.__init__(self, *args, **kwargs)


class ActionError(LinchpinError):

    def __init__(self, *args, **kwargs):
        LinchpinError.__init__(self, *args, **kwargs)


class ValidationErrorHandler(cerberus_errors.BasicErrorHandler):
    messages = cerberus_errors.BasicErrorHandler.messages.copy()
    messages[cerberus_errors.REQUIRED_FIELD.code] = "field '{field}' is "\
        + "required"
    messages[cerberus_errors.UNKNOWN_FIELD.code] = "field '{field}' could not "\
        + "be recognized within the schema provided"
    messages[cerberus_errors.BAD_TYPE.code] = "value for field '{field}' must "\
        + "be of type '{constraint}'"
    messages[cerberus_errors.UNALLOWED_VALUE.code] = "unallowed value " \
        + "'{value}' for field '{field}'. Allowed values are: {constraint}"