0

I'm evaluating cerberus' capability for adding new validation rules and I stumbled upon the following issue, custom rules do not work with the "nullable" rule. Here is an example of using a custom validator from the documentation with nullable:

class MyValidator(cerberus.Validator):
  def _validate_is_odd(self, constraint, field, value):
    """ {'type': 'boolean'} """
    if constraint is True and not (value & 1):
      self._error(field, "must be an odd number")


validator = MyValidator({
  "number": {"type": "integer", 
             "is_odd": True,
             "nullable": True}
})

validator.validate({"number": None})

I expected that since the "nullable" rule is present and the value is None then all remaining rules are dropped and the document passes validation. Instead, it runs the "is_odd" validator regardless and throws an exception. After diving into the "_validate_nullable" implementation I found that it only drops some of the built-in remaining rules, not all of them. Is it a bug or intended behaviour? What are "proper" ways to deal with nulls? Should I override "_validate_nullable" in "MyValidator" or add a null check at the beginning of the validation method?

warownia1
  • 2,771
  • 1
  • 22
  • 30

0 Answers0