View Source Funx.Errors.ValidationError exception (funx v0.1.0)

Represents a validation error in the Funx library.

A ValidationError wraps one or more domain-level validation messages. It is typically used with Either.Left to indicate that a value failed validation and should not proceed in a computation. It can also be raised directly, as it implements the Exception behaviour.

This module provides functions to construct, merge, and convert validation errors, enabling structured, composable error handling across pipelines and validation chains.

Functions

  • new/1 – Creates a ValidationError from a single error string or a list of error strings.
  • empty/0 – Returns an empty ValidationError.
  • merge/2 – Combines two ValidationError structs into one.
  • from_tagged/1 – Converts a tagged error tuple ({:error, errors}) into a ValidationError.

This module also implements the Exception, String.Chars, and Funx.Summarizable protocols, supporting both human-readable output and structured reporting.

Usage in validation

You can validate a value using a list of validator functions. Each validator returns an Either.Right if the check passes, or an Either.Left with an error message if it fails. If any validation fails, all errors are aggregated and returned in a single Left.

In contexts where an error must halt execution, ValidationError can be raised directly using raise/1.

Examples

You can also use a ValidationError to hold errors:

alias Funx.Errors.ValidationError

validate_positive = fn x ->
  Funx.Monad.Either.lift_predicate(x, &(&1 > 0), fn v -> "Value must be positive: " <> to_string(v) end)
  |> Funx.Monad.Either.map_left(&ValidationError.new/1)
end

validate_even = fn x ->
  Funx.Monad.Either.lift_predicate(x, &(rem(&1, 2) == 0), fn v -> "Value must be even: " <> to_string(v) end)
  |> Funx.Monad.Either.map_left(&ValidationError.new/1)
end

Funx.Monad.Either.validate(-3, [validate_positive, validate_even])
#=> %Funx.Monad.Either.Left{
#     left: %ValidationError{
#       errors: ["Value must be positive: -3", "Value must be even: -3"]
#     }
#   }

Summary

Functions

Returns an empty ValidationError.

Converts a tagged error tuple into a ValidationError.

Merges two ValidationError structs into one by concatenating their error lists.

Creates a ValidationError from a single string or list of strings.

Types

@type t() :: %Funx.Errors.ValidationError{__exception__: true, errors: [String.t()]}

Functions

@spec empty() :: t()

Returns an empty ValidationError.

Examples

iex> Funx.Errors.ValidationError.empty()
%Funx.Errors.ValidationError{errors: []}
@spec from_tagged({:error, [String.t()]}) :: t()

Converts a tagged error tuple into a ValidationError.

Examples

iex> Funx.Errors.ValidationError.from_tagged({:error, ["must be positive"]})
%Funx.Errors.ValidationError{errors: ["must be positive"]}
Link to this function

merge(validation_error1, validation_error2)

View Source
@spec merge(t(), t()) :: t()

Merges two ValidationError structs into one by concatenating their error lists.

Examples

iex> e1 = Funx.Errors.ValidationError.new("must be positive")
iex> e2 = Funx.Errors.ValidationError.new("must be even")
iex> Funx.Errors.ValidationError.merge(e1, e2)
%Funx.Errors.ValidationError{errors: ["must be positive", "must be even"]}
@spec new([String.t()]) :: t()
@spec new(String.t()) :: t()

Creates a ValidationError from a single string or list of strings.

Examples

iex> Funx.Errors.ValidationError.new("must be positive")
%Funx.Errors.ValidationError{errors: ["must be positive"]}

iex> Funx.Errors.ValidationError.new(["must be positive", "must be even"])
%Funx.Errors.ValidationError{errors: ["must be positive", "must be even"]}