Funx.Validator.LiftPredicate (funx v0.8.2)

View Source

Lifts a predicate into the validation context.

LiftPredicate adapts a predicate function into a validator that conforms to Funx.Validate.Behaviour. It allows predicate-style logic to participate in the validation pipeline, producing Either results and ValidationErrors instead of booleans.

This module is intended as an escape hatch for ad-hoc or externally-defined predicates. For reusable domain rules, prefer defining a dedicated validator module or using the Predicate DSL directly.

Options

  • :pred (required) A predicate function (value -> boolean) that determines whether validation succeeds.

  • :message (optional) A callback (value -> String.t()) used to override the default error message when the predicate fails.

Semantics

  • If the predicate returns true, validation succeeds and the value is returned.

  • If the predicate returns false, validation fails with a ValidationError.

  • Nothing values succeed without invoking the predicate.

  • Just values are unwrapped before validation. . Examples

    iex> Funx.Validator.LiftPredicate.validate(150, pred: fn v -> v > 100 end) %Funx.Monad.Either.Right{right: 150}

    iex> Funx.Validator.LiftPredicate.validate(50, pred: fn v -> v > 100 end) %Funx.Monad.Either.Left{

    left: %Funx.Errors.ValidationError{errors: ["invalid value"]}

    }

    iex> Funx.Validator.LiftPredicate.validate( ...> 50, ...> pred: fn v -> v > 100 end, ...> message: fn _ -> "must be greater than 100" end ...> ) %Funx.Monad.Either.Left{

    left: %Funx.Errors.ValidationError{errors: ["must be greater than 100"]}

    }

Summary

Functions

validate(value)

validate(value, opts)