Tyyppi.Example (tyyppi v0.6.0) View Source

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

The original code of this module follows:

import Kernel, except: [defstruct: 1]
import Tyyppi.Struct, only: [defstruct: 1]

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

defstruct foo: atom(), bar: GenServer.on_start(), baz: my_type()

Link to this section Summary

Types

The user type exported

The user type defined before defstruct/1 declaration

t()

The type describing this struct. This type will be used to validate upserts when called via Access and/or Tyyppi.Example.put/3, Tyyppi.Example.update/4.

Functions

Returns the field types of this struct as keyword of {field :: atom, type :: Tyyppi.T.t()} 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

Specs

my_map_1() :: %{foo: atom()}

The user type exported

Specs

my_map_2() :: %{required(atom()) => integer()}

Specs

my_map_3() :: %{optional(binary()) => my_type()}

Specs

my_struct() :: %DateTime{
  calendar: term(),
  day: term(),
  hour: term(),
  microsecond: term(),
  minute: term(),
  month: term(),
  second: term(),
  std_offset: term(),
  time_zone: term(),
  utc_offset: term(),
  year: term(),
  zone_abbr: term()
}

Specs

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

The user type defined before defstruct/1 declaration

Specs

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

The type describing this struct. This type will be used to validate upserts when called via Access and/or Tyyppi.Example.put/3, Tyyppi.Example.update/4.

Link to this section Functions

Specs

types() :: [{atom(), Tyyppi.T.t()}]

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

Specs

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

Updates the struct

Specs

validate(t()) :: {:ok, t()} | {:error, term()}

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