From the documentation, 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 if
statement that checks the value of is_odd
in the custom rule. When should I prefer the Custom Rule, and when should I prefer the Custom Validator?
Custom Rule
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 & 1):
self._error(field, "Must be an odd number")
Custom Validator
from cerberus import Validator
schema = {'amount': {'validator': 'oddity'}}
class AValidator(Validator):
def _validator_oddity(self, field, value):
if value & 1:
self._error(field, "Must be an odd number")