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

T

Functions

and_then(first, second)

Combine two validators with logical AND (both must succeed).

or_else(first, second)

Combine two validators with logical OR (either can succeed).

not_validator(validator, error_message)

Negate a validator (success becomes failure and vice versa).

validate_all(value, parser, validators)

Parse and validate a value, collecting ALL validation errors.

Module Contents

valid8r.core.combinators.T[source]
valid8r.core.combinators.and_then(first, second)[source]

Combine two validators with logical AND (both must succeed).

Parameters:
Returns:

A new validator function that passes only if both validators pass

Return type:

collections.abc.Callable[[T], valid8r.core.maybe.Maybe[T]]

valid8r.core.combinators.or_else(first, second)[source]

Combine two validators with logical OR (either can succeed).

Parameters:
Returns:

A new validator function that passes if either validator passes

Return type:

collections.abc.Callable[[T], valid8r.core.maybe.Maybe[T]]

valid8r.core.combinators.not_validator(validator, error_message)[source]

Negate a validator (success becomes failure and vice versa).

Parameters:
Returns:

A new validator function that passes if the original validator fails

Return type:

collections.abc.Callable[[T], valid8r.core.maybe.Maybe[T]]

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:
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:

valid8r.core.maybe.Maybe[T]

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