maite.utils.validation.chain_validators#
- maite.utils.validation.chain_validators(*validators)[source]#
Enable validators to be chained together.
Validators are functions like
(name: str, arg: T, [...]) -> T
. This is meant to be used with partial’d validators, where only thename
andarg
fields need be populated.- Parameters:
- *validatorsCallable[[str, T], T]
Accepts
name
andarg
, and returnsarg
if it is a valid input, otherwise should raiseInvalidArgument
.
- Returns:
- Callable[[str, T], T]
A function of signature
chain(name: str, arg: T) -> T
that calls each validator asval(name, arg)
in order from low-index to high-index.
Examples
>>> from maite.utils.validation import check_type, check_domain, chain_validators >>> from functools import partial >>> is_int = partial(check_type, type_=int) >>> is_pos = partial(check_domain, lower=0) >>> check_pos_int = chain_validators(is_int, is_pos) >>> check_pos_int("foo", 10) 10 >>> try: ... check_pos_int("foo", ["a"]) ... except: ... print("maite.errors.InvalidArgument: Expected `foo` to be of type `int`. Got `['a']` (type: `list`).") maite.errors.InvalidArgument: Expected `foo` to be of type `int`. Got `['a']` (type: `list`). >>> try: ... check_pos_int("foo", -1) ... except: ... print("maite.errors.InvalidArgument: `foo` must satisfy `0 <= foo`. Got: `-1`.") maite.errors.InvalidArgument: `foo` must satisfy `0 <= foo`. Got: `-1`.