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

Example of the structure defined with Tyyppi.Struct.defstruct/1, all values be Tyyppi.Value

The original code of this module follows:

use Tyyppi

@defaults foo: Value.optional(Value.atom()),
          bar: Value.integer(42),
          baz: Value.date_time(~U[1973-09-30 02:46:30Z])
defstruct foo: Value.t(atom()), bar: Value.t(integer()), baz: Value.t(DateTime.t())

def validate_bar(%Value{value: value}) when value < 100, do: {:ok, value}
def validate_bar(%Value{}), do: {:error, "Expected a value to be less than 100"}

This module defines a struct having two fields, foo of a type Value.t(atom()), and bar of a type Value.t(integer()).

Link to this section Summary

Types

t()

The type describing Tyyppi.Example.Value. 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.Value.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 t() :: %Tyyppi.Example.Value{
  foo: Tyyppi.Value.t(atom()),
  bar: Tyyppi.Value.t(integer()),
  baz: Tyyppi.Value.t(DateTime.t()),
  str: Tyyppi.Value.t(String.t())
}

The type describing Tyyppi.Example.Value. 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.Value.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.Value 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