You use the validator rule if you want to delegate the verification of a specific field value to a user-defined function or method (the latter is a link as a string in a schema), for example:
>>> def oddity(field, value, error): ... if not value & 1: ... error(field, "Must be an odd number") >>> schema = {'amount': {'validator': oddity}} >>> v = Validator(schema) >>> v.validate({'amount': 10}) False >>> v.errors {'amount': 'Must be an odd number'} >>> v.validate({'amount': 9}) True
Note that the signature of them is fixed, they take exactly field (the name of the field in the document), value and error (the method of sending errors) as arguments.
Custom rules can only be defined in the validator subclass. Unlike the validator rule, where the verifying caller is defined as a restriction, for restrictions, user rules can be defined in the scheme, which are also passed to the implementation of the user rule (in this answer, it is called an example ). Therefore, the behavior of the rule can be controlled in the scheme.
The documentation for the validator rule validator to be misleading, if not completely wrong, and I have to go and fix it as soon as possible! Thank you for message.
source share