def validate(*rules): """ Creates a validation function that will check if it's input satisfies `rules`. `rules` should be an (arbitrarily nested) sequence of functions that take one argument and return a tuple of: ``(valid: bool, error: string or None)`` where `valid` says if the argument satisfies the rule and `error` says why not if it doesn't. The rules are checked sequentially and when an error is encountered, it is returned immediately from the function, without checking the rest of the rules. The returned value is the one returned from the rule, i.e. ``(False, "error message")`` If no error is encountered the function returns ``(True, None)``. (Note that the validation function itself is a rule.) """ rules = rules > flatten | tuple return lambda val: rules > foreach(X(val)) | select_first(X != OK) or OK
def test_call(self): f = ~X(42) assert f(lambda n: n / 2) == 21