Validixir.Failure (validixir v1.2.0)
Module containing data definition and functionality concering a Failure. An Failure is a struct representing a failed validation.
An Failure 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 directly used
Link to this section Summary
Functions
Combines two errors. That is, appending their error list.
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.
Link to this section Types
@type t() :: %Validixir.Failure{errors: [Validixir.Error.t()], message_lookup: map()}
Link to this section Functions
combine(failure1, failure2)
Combines two errors. That is, appending their error list.
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)
%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}
}
failure?(arg1)
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
make(errors)
Smart constructor of a failure.
make_from_error(candidate, message, context)
Constructs a failure with one error based on error parameters.
examples
Examples
iex> Validixir.Failure.make_from_error(12, :twelve_not_allowed, SomeContext)
%Validixir.Failure{errors: [%Validixir.Error{candidate: 12, message: :twelve_not_allowed, context: SomeContext}], message_lookup: %{twelve_not_allowed: true}}
map(failure, f)
@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)
%Validixir.Failure{errors: [%Validixir.Error{candidate: 2, message: :message_new, context: :context}], message_lookup: %{message_new: true}}
override_error_contexts(failure, new_context)
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)
%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}
}
override_error_messages(failure, new_message)
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)
%Validixir.Failure{errors: [
%Validixir.Error{candidate: 1, message: :nonono, context: SomeContext},
%Validixir.Error{candidate: 2, message: :nonono, context: SomeContext}
],
message_lookup: %{nonono: true}
}