Validixir.Failure (validixir v1.2.2)

Module containing data definition and functionality concerning a Failure. A Failure is a error tuple that contains a Failure struct representing a failed validation.

A Failure struct consists of the following:

  • A list of Errors, displaying multiple causes for the validation to fail.
  • A message lookup that is a map with all messages as keys. This lookup is used internally and should not be used directly.
  • Optional meta information that is defined by the user.

Link to this section Summary

Functions

Combines two errors. That is, appending their error lists.

Returns true if a value is a failure.

Smart constructor of a failure.

Constructs a failure with one error based on error parameters.

Applies a function to each error in a failure's errors.

Overrides the context of all errors of a failure.

Overrides the message of all errors of a failure.

Puts meta into the failure.

Link to this section Types

@type failure_t() :: %Validixir.Failure{
  __message_lookup: map(),
  errors: [Validixir.Error.t()],
  meta: any()
}
@type t() :: {:error, failure_t()}

Link to this section Functions

Link to this function

combine(arg1, arg2)

@spec combine(t(), t()) :: t()

Combines two errors. That is, appending their error lists.

examples

Examples

iex> error_1 = Validixir.Error.make(1, :not_allowed, SomeContext)
iex> error_2 = Validixir.Error.make(2, :also_not_allowed, SomeContext)
iex> failure_1 = Validixir.Failure.make([error_1])
iex> failure_2 = Validixir.Failure.make([error_2])
iex> Validixir.Failure.combine(failure_1, failure_2)
{:error, %Validixir.Failure{errors: [
      %Validixir.Error{candidate: 1, message: :not_allowed, context: SomeContext},
      %Validixir.Error{candidate: 2, message: :also_not_allowed, context: SomeContext}
    ],
    __message_lookup: %{not_allowed: true, also_not_allowed: true}
}}
@spec failure?(any()) :: boolean()

Returns true if a value is a failure.

examples

Examples

iex> f = Validixir.Failure.make([])
iex> Validixir.Failure.failure?(f)
true

iex> s = Validixir.Success.make(1)
iex> Validixir.Failure.failure?(s)
false
Link to this function

make(errors, meta \\ nil)

@spec make([Validixir.Error.t()], any()) :: t()

Smart constructor of a failure.

Link to this function

make_from_error(candidate, message, context)

@spec make_from_error(any(), any(), any()) :: t()

Constructs a failure with one error based on error parameters.

examples

Examples

iex> Validixir.Failure.make_from_error(12, :twelve_not_allowed, SomeContext)
{:error, %Validixir.Failure{errors: [%Validixir.Error{candidate: 12, message: :twelve_not_allowed, context: SomeContext}], __message_lookup: %{twelve_not_allowed: true}}}
@spec map(t(), (Validixir.Error.t() -> Validixir.Error.t())) :: t()

Applies a function to each error in a failure's errors.

examples

Examples

iex> f = Validixir.Failure.make_from_error(1, :message, :context)
iex> Validixir.Failure.map(f, fn %Validixir.Error{candidate: c} -> Validixir.Error.make(c + 1, :message_new, :context) end)
{:error, %Validixir.Failure{errors: [%Validixir.Error{candidate: 2, message: :message_new, context: :context}], __message_lookup: %{message_new: true}}}
Link to this function

override_error_contexts(failure, new_context)

@spec override_error_contexts(t(), any()) :: t()

Overrides the context of all errors of a failure.

examples

Examples

iex> error_1 = Validixir.Error.make(1, :not_allowed, SomeContext)
iex> error_2 = Validixir.Error.make(2, :not_allowed, SomeOtherContext)
iex> f = Validixir.Failure.make([error_1, error_2])
iex> Validixir.Failure.override_error_contexts(f, NewFailureContext)
{:error, %Validixir.Failure{errors: [
    %Validixir.Error{candidate: 1, message: :not_allowed, context: NewFailureContext},
    %Validixir.Error{candidate: 2, message: :not_allowed, context: NewFailureContext}
    ],
  __message_lookup: %{not_allowed: true}
}}
Link to this function

override_error_messages(failure, new_message)

@spec override_error_messages(t(), any()) :: t()

Overrides the message of all errors of a failure.

examples

Examples

iex> error_1 = Validixir.Error.make(1, :not_allowed, SomeContext)
iex> error_2 = Validixir.Error.make(2, :also_not_allowed, SomeContext)
iex> f = Validixir.Failure.make([error_1, error_2])
iex> Validixir.Failure.override_error_messages(f, :nonono)
{:error, %Validixir.Failure{errors: [
      %Validixir.Error{candidate: 1, message: :nonono, context: SomeContext},
      %Validixir.Error{candidate: 2, message: :nonono, context: SomeContext}
    ],
    __message_lookup: %{nonono: true}
}}
Link to this function

put_meta(arg, meta)

@spec put_meta(t(), any()) :: t()

Puts meta into the failure.