Mold.Error exception (Mold v0.1.0)

Copy Markdown View Source

Exception raised/returned by Mold when parsing fails.

Fields

  • :reason – atom or term explaining the failure (see Reasons below).
  • :value – the offending value.
  • :trace – path to the failing value (list indexes and/or map keys), or nil.

Reasons

Shared (all types)

ReasonWhen
:unexpected_nilValue is nil (or empty string for :string/date-time types) and type is not nilable
:unexpected_typeValue doesn't match expected input type (e.g. passing a list to :string)
{:not_in, enumerable}Parsed value not in the :in set
:validation_failedCustom :validate function returned false

String

ReasonWhen
{:invalid_format, regex}Value doesn't match the :format regex
{:too_short, min_length: n}String shorter than :min_length
{:too_long, max_length: n}String longer than :max_length

Integer / Float

ReasonWhen
:invalid_formatCan't parse as number
{:too_small, min: n}Below :min
{:too_large, max: n}Above :max

Boolean

ReasonWhen
:invalid_formatNot a recognized boolean value

Atom

ReasonWhen
:unknown_atomString doesn't correspond to an existing atom

Date & Time

Reasons are passed through from Elixir's standard library parsers (Date.from_iso8601/1, DateTime.from_iso8601/1, etc.):

ReasonWhen
:invalid_formatNot a valid ISO8601 string
:invalid_dateValid format but invalid date (e.g. month 13)
:invalid_timeValid format but invalid time (e.g. hour 25)
:missing_offsetDateTime string missing timezone offset

Map

ReasonWhen
{:missing_field, key}Required field not found in input

List

ReasonWhen
{:too_short, min_length: n}List shorter than :min_length
{:too_long, max_length: n}List longer than :max_length

Tuple

ReasonWhen
{:unexpected_length, expected: n, got: n}Wrong number of elements

Union

ReasonWhen
{:unknown_variant, key}:by function returned a key not in :of

Custom function

ReasonWhen
:invalidFunction returned bare :error
any termPassed through from {:error, reason}

Summary

Types

t()

@type t() :: %Mold.Error{
  __exception__: true,
  reason: any(),
  trace: [any()] | nil,
  value: any()
}