Zodish.Issue exception (zodish v0.2.4)

View Source

Represents an issue while parsing a value.

Summary

Functions

Appends a set of segments to the given issue's path.

Flattens the issues of a given Zodish.Issue struct.

Creates a new issue with the given message.

Creates a new issue after replacing any variables in the given message for their value in the given context map.

Calculates the parse score of a given parsed value.

Prepends a set of segments to the given issue's path.

Types

segment()

@type segment() :: atom() | non_neg_integer() | String.t()

t()

@type t() :: %Zodish.Issue{
  __exception__: true,
  issues: [t()],
  message: String.t(),
  parse_score: non_neg_integer(),
  path: [String.t()]
}

Functions

append_path(issue, segments)

@spec append_path(issue :: t(), segments :: [segment()]) :: t()

Appends a set of segments to the given issue's path.

iex> %Zodish.Issue{path: ["a"], message: "An error occurred"}
iex> |> append_path([:b, :c])
%Zodish.Issue{path: ["a", "b", "c"], message: "An error occurred"}

flatten(issue)

@spec flatten(issue :: t()) :: t()

Flattens the issues of a given Zodish.Issue struct.

iex> flatten(%Zodish.Issue{message: "One or more items failed validation", issues: [
iex>   %Zodish.Issue{
iex>     path: ["0"],
iex>     message: "One or more fields failed validation",
iex>     issues: [%Zodish.Issue{path: ["email"], message: "Is required"}],
iex>     parse_score: 1
iex>   },
iex>   %Zodish.Issue{
iex>     path: ["1"],
iex>     message: "One or more fields failed validation",
iex>     issues: [%Zodish.Issue{path: ["name"], message: "Is required"}],
iex>     parse_score: 1,
iex>   }
iex> ]})
%Zodish.Issue{message: "One or more items failed validation", issues: [
  %Zodish.Issue{path: ["0", "email"], message: "Is required"},
  %Zodish.Issue{path: ["1", "name"], message: "Is required"}
]}

issue(message)

@spec issue(message :: String.t()) :: t()

Creates a new issue with the given message.

iex> issue("An error occurred")
%Zodish.Issue{message: "An error occurred"}

issue(message, ctx)

@spec issue(message :: String.t(), ctx :: map()) :: t()

Creates a new issue after replacing any variables in the given message for their value in the given context map.

iex> issue("The value of {{key}} is invalid", %{key: "foo"})
%Zodish.Issue{message: "The value of foo is invalid"}

You can also use pluralization slots that can automatically pluralize words based on a numeric context variable:

iex> issue("At least {{count | item}} required", %{count: 1})
%Zodish.Issue{message: "At least 1 item required"}

iex> issue("At least {{count | item}} required", %{count: 2})
%Zodish.Issue{message: "At least 2 items required"}

parse_score(value)

@spec parse_score(value :: any()) :: non_neg_integer()

Calculates the parse score of a given parsed value.

iex> parse_score(%{ # +1
iex>   foo: [ # +1
iex>     %{bar: :bar}, # +2
iex>     %{baz: {:ok, :baz}} # +4
iex>   ]
iex> })
8

prepend_path(issue, segments)

@spec prepend_path(issue :: t(), segments :: [segment()]) :: t()

Prepends a set of segments to the given issue's path.

iex> %Zodish.Issue{path: ["c"], message: "An error occurred"}
iex> |> prepend_path([:a, :b])
%Zodish.Issue{path: ["a", "b", "c"], message: "An error occurred"}