View Source DataSpecs (dataspecs v2.1.0)

DataSpecs.

Summary

Functions

Defines a cast helper function in a struct module.

Functions

Link to this macro

__using__(opts)

View Source (macro)
@spec __using__(any()) :: Macro.t()

Defines a cast helper function in a struct module.

defmodule Person do
  use DataSpecs

  @enforce_keys [:name, :surname]
  defstruct @enforce_keys ++ [:birth_date]

  @type t :: %__MODULE__{
              name: String.t(),
              surname: String.t(),
              birth_date: nil | Date.t(),
            }
end
Person.cast(%{"name" => "John", surname => "Smith", "birth_date": "1980-12-31"})

%Person{
  name: "John",
  surname: "Smith",
  birth_date: ~D[1980-12-31]
}

equivalent to

DataSpecs.cast(%{"name" => "John", surname => "Smith"}, {Person, :t})
Link to this function

cast(value, arg, custom_type_casts \\ Cast.Extra.type_casts(), type_params_casts \\ [])

View Source
@spec cast(
  data :: DataSpecs.Types.value(),
  type_ref :: DataSpecs.Types.mt(),
  custom_type_casts :: DataSpecs.Types.custom_type_casts(),
  type_params_casts :: [DataSpecs.Types.type_cast_fun()]
) :: DataSpecs.Types.cast_result(term())

Cast a value that should conform to a typespec.

Info

custom_type_casts defaults to DataSpecs.Cast.Extra.type_casts/0. This will by default map types such as Date.t/0, DateTime.t/0, MapSet.t/0, ... as describer in the module documentation.