What is the difference between Cerberus Custom Rules and Custom Validators?
<p>From the <a href="http://docs.python-cerberus.org/en/latest/customize.html#custom-rules" rel="nofollow">documentation</a>, it is not clear to me what the difference in use case for the Custom Rule and the Custom Validators are. In the examples given in the documentation, the only difference is an extra <code>if</code> statement that checks the value of <code>is_odd</code> in the custom rule. When should I prefer the Custom Rule, and when should I prefer the Custom Validator?</p>

<hr>

<p>Custom Rule</p>

<pre><code>schema = {'amount': {'isodd': True, 'type': 'integer'}}
from cerberus import Validator

class MyValidator(Validator):
    def _validate_isodd(self, isodd, field, value):
        """ Test the oddity of a value.

        The rule's arguments are validated against this schema:
        {'type': 'boolean'}
        """
        if isodd and not bool(value &amp; 1):
            self._error(field, "Must be an odd number")
</code></pre>

<hr>

<p>Custom Validator</p>

<pre><code>from cerberus import Validator
schema = {'amount': {'validator': 'oddity'}}

class AValidator(Validator):
    def _validator_oddity(self, field, value):
       if value &amp; 1:
           self._error(field, "Must be an odd number")
</code></pre>

<p>You use the <code>validator</code> rule when you want to delegate validation of a certain field to a custom function, like so:</p>

<pre><code>&gt;&gt;&gt; def oddity(field, value, error):
...   if not value &amp; 1:
...     error(field, "Must be an odd number")

&gt;&gt;&gt; schema = {'amount': {'validator': oddity}}
&gt;&gt;&gt; v = Validator(schema)
&gt;&gt;&gt; v.validate({'amount': 10})
False

&gt;&gt;&gt; v.errors
{'amount': 'Must be an odd number'}

&gt;&gt;&gt; v.validate({'amount': 9})
True
</code></pre>

<p>Custom rules are only used in your <code>Validator</code> subclass whereas custom validators are delegate functions. It appears that the documentation for the <code>validator</code> rule is misleading if not plain wrong, and I should go and fix it as soon as possible! Thanks for reporting.</p>

python cerberus