-
Notifications
You must be signed in to change notification settings - Fork 1
Validators
Krzysztof Czarnecki edited this page Dec 15, 2021
·
3 revisions
It is possible to quickly write your own validators. You need to create a function with a signature that matches mkcommit.model.ValidatorClosure, so a function that returns another function, where the inner function accepts a string and returns a boolean. Quick example:
from mkcommit.model import Validator
import re
def matches(pattern: str) -> Validator:
def _v(msg: str) -> bool:
if re.match(pattern, msg):
return True
else:
return False
_v.__doc__ = f"The pattern {pattern} hasn't been matched to the input"
return _vThe above is the implementation of the matches validator.
You can declare validators directly within your *.mkcommit.py file or import them from somewhere else.
-
matches(pattern)- matches an arbitrary RegEx pattern -
is_int- succeeds only if the input is a valid integer -
is_float- succeeds only if the input is a valid float -
min_len(ln)- succeeds only if the input is at leastlncharacters long -
max_len(ln)- succeeds only if the input does not exceedln -
validate_initials(letters_f, letters_l)- opinionated validator for human initials, succeeds only if the input consists of a set ofletters_fnumber of first name letters andletters_lnumber of last name letters. Additionally the first letter of both the first name and last name must be capitalized, e.g.validate_initials(2,2)will succeed with inputsKrCzandAbCdbut notKrzCzaorAbcDe. -
is_true- succeeds if input was provided or a yes/no question was answered with ay -
is_false- succeeds if input was not provided or a yes/no question was answered with ann -
are_keywords_selected- a descriptive alias formin_len(1)that is used with e.g.semanticsuite'sask_keyword