View Source Tyyppi.Example.Struct (tyyppi v0.12.3)

Example of the structure defined with Tyyppi.Struct.defstruct/1

The original code of this module follows:

use Tyyppi

@typedoc "The user type defined before `defstruct/1` declaration"
@type my_type :: :ok | {:error, term()}

@defaults bar: {:ok, :erlang.list_to_pid('<0.0.0>')}, baz: {:error, :reason}
defstruct foo: nil | atom(), bar: GenServer.on_start(), baz: my_type()

defp cast_baz(true), do: :ok
defp cast_baz(false), do: {:error, false}
defp cast_baz(value), do: value

usage-examples

Usage examples

iex> put_in(%Tyyppi.Example.Struct{}, [:foo], :ok)
%Tyyppi.Example.Struct{
  bar: {:ok, :erlang.list_to_pid('<0.0.0>')},
  baz: {:error, :reason},
  foo: :ok}
iex> put_in(%Tyyppi.Example.Struct{}, [:foo], 42)
** (ArgumentError) could not put/update key :foo with value 42 ([foo: [type: [expected: "atom()", got: 42]]])
iex> put_in(%Tyyppi.Example.Struct{}, [:baz], true)
%Tyyppi.Example.Struct{
  bar: {:ok, :erlang.list_to_pid('<0.0.0>')},
  baz: :ok,
  foo: nil}
iex> put_in(%Tyyppi.Example.Struct{}, [:baz], false)
%Tyyppi.Example.Struct{
  bar: {:ok, :erlang.list_to_pid('<0.0.0>')},
  baz: {:error, false},
  foo: nil}

Link to this section Summary

Types

The user type defined before defstruct/1 declaration

t()

The type describing Tyyppi.Example.Struct. This type will be used to validate upserts when called via Access and/or Tyyppi.Struct.put/3, Tyyppi.Struct.update/3, both delegating to generated Tyyppi.Example.Struct.update/2.

Functions

See Tyyppi.Struct.flatten/1.

See Tyyppi.Struct.flatten/2.

Helper guard to match instances of struct Tyyppi.Value

Returns the field types of this struct as keyword of {field :: atom, type :: Tyyppi.T.t(term())} pairs.

Updates the struct

This function is supposed to be overwritten in the implementation in cases when custom validation is required.

Link to this section Types

@type my_type() :: :ok | {:error, term()}

The user type defined before defstruct/1 declaration

@type t() :: %Tyyppi.Example.Struct{
  foo: atom(),
  bar: GenServer.on_start(),
  baz: my_type()
}

The type describing Tyyppi.Example.Struct. This type will be used to validate upserts when called via Access and/or Tyyppi.Struct.put/3, Tyyppi.Struct.update/3, both delegating to generated Tyyppi.Example.Struct.update/2.

Upon insertion, the value will be coerced to the expected type when available, the type itself will be validated, and then the custom validation will be applied when applicable.

Link to this section Functions

@spec as_value(keyword()) :: Tyyppi.Value.t({:any, [], []})

Factory for Elixir.Tyyppi.Example.Struct wrapped by Tyyppi.Value

@spec errors(t()) :: list()

See Tyyppi.Struct.flatten/1.

See Tyyppi.Struct.flatten/2.

Link to this macro

is_value(value)

View Source (since 0.9.0) (macro)

Helper guard to match instances of struct Tyyppi.Value

@spec types() :: [{atom(), Tyyppi.T.t(wrapped)}] when wrapped: term()

Returns the field types of this struct as keyword of {field :: atom, type :: Tyyppi.T.t(term())} pairs.

@spec update(target :: t(), values :: keyword()) :: {:ok, t()} | {:error, keyword()}

Updates the struct

@spec valid?(t()) :: boolean()
@spec validate(t()) :: Tyyppi.Valuable.either()

This function is supposed to be overwritten in the implementation in cases when custom validation is required.

It would be called after all casts and type validations, if the succeeded