valid8r.core.combinators
Combinators for creating complex validation rules.
This module provides functions to combine validators using logical operations like AND, OR, and NOT. These combinators allow for creation of complex validation chains.
Attributes
Functions
|
Combine two validators with logical AND (both must succeed). |
|
Combine two validators with logical OR (either can succeed). |
|
Negate a validator (success becomes failure and vice versa). |
|
Parse and validate a value, collecting ALL validation errors. |
Module Contents
- valid8r.core.combinators.and_then(first, second)[source]
Combine two validators with logical AND (both must succeed).
- Parameters:
first (collections.abc.Callable[[T], valid8r.core.maybe.Maybe[T]]) – The first validator function
second (collections.abc.Callable[[T], valid8r.core.maybe.Maybe[T]]) – The second validator function
- Returns:
A new validator function that passes only if both validators pass
- Return type:
- valid8r.core.combinators.or_else(first, second)[source]
Combine two validators with logical OR (either can succeed).
- Parameters:
first (collections.abc.Callable[[T], valid8r.core.maybe.Maybe[T]]) – The first validator function
second (collections.abc.Callable[[T], valid8r.core.maybe.Maybe[T]]) – The second validator function
- Returns:
A new validator function that passes if either validator passes
- Return type:
- valid8r.core.combinators.not_validator(validator, error_message)[source]
Negate a validator (success becomes failure and vice versa).
- Parameters:
validator (collections.abc.Callable[[T], valid8r.core.maybe.Maybe[T]]) – The validator function to negate
error_message (str) – Error message for when the negated validator fails
- Returns:
A new validator function that passes if the original validator fails
- Return type:
- valid8r.core.combinators.validate_all(value, parser, validators)[source]
Parse and validate a value, collecting ALL validation errors.
Unlike bind() which stops at the first failure, validate_all() runs all validators and collects every error. This is useful for form validation where users need to see all problems at once.
- Parameters:
value (str) – The string value to parse and validate
parser (collections.abc.Callable[[str], valid8r.core.maybe.Maybe[T]]) – A parser function that converts the string to type T
validators (collections.abc.Sequence[collections.abc.Callable[[T], valid8r.core.maybe.Maybe[T]]]) – A sequence of validator functions to apply to the parsed value
- Returns:
Success containing the validated value if all validators pass, or Failure containing a list of ValidationErrors if any validators fail. If the parser fails, returns the parser’s Failure immediately.
- Return type:
Examples
Collect all validation errors:
>>> from valid8r.core.parsers import parse_int >>> from valid8r.core.validators import minimum, maximum, predicate >>> is_positive = minimum(0, 'Must be positive') >>> is_even = predicate(lambda x: x % 2 == 0, 'Must be even') >>> result = validate_all('-5', parse_int, [is_positive, is_even]) >>> result.is_failure() True
Success when all validators pass:
>>> result = validate_all('42', parse_int, [is_positive, is_even]) >>> result.is_success() True >>> result.value_or(0) 42