colander icon indicating copy to clipboard operation
colander copied to clipboard

ComboBreaker exception that breaks the validation chain

Open jayd3e opened this issue 11 years ago • 0 comments

Proposing that we create an exception that can be raised in a validator, which indicates an Invalid state AND stops the validation chain. I have something hacked together which accomplishes this:


class ComboBreaker(colander.Invalid):
    pass


class All(colander.All):
    """
    Custom All validator, that allows me to throw a specific exception, if
    I want to end the validation chain.
    """
    def __call__(self, node, value):
        excs = []
        for validator in self.validators:
            try:
                validator(node, value)
            except ComboBreaker as e:
                excs.append(e)
                break
            except Invalid as e:
                excs.append(e)

        if excs:
            exc = Invalid(node, [exc.msg for exc in excs])
            for e in excs:
                exc.children.extend(e.children)
            raise exc

jayd3e avatar Feb 09 '14 19:02 jayd3e