View Source Estructura.Nested (estructura v1.5.0)

The nested struct with helpers to easily describe it and produce validation, coercion, and generation helpers.

Summary

Functions

DSL helper to produce coerce callbacks. The syntax is kinda weird, but bear with it, please.

Declares the shape of the target nested map. the values might be

DSL helper to produce validate callbacks. The syntax is kinda weird, but bear with it, please.

Functions

DSL helper to produce coerce callbacks. The syntax is kinda weird, but bear with it, please.

It’s known to produce warnings in credo, I’m working on it.

coerce do
  def data.age(age) when is_float(age), do: {:ok, age}
  def data.age(age) when is_integer(age), do: {:ok, 1.0 * age}
  def data.age(age) when is_binary(age), do: {:ok, String.to_float(age)}
  def data.age(age), do: {:error, "Could not cast #{inspect(age)} to float"}
end

Declares the shape of the target nested map. the values might be:

  • :string | :integer or another simple type understood by StreamData

  • [type] to declare a list of elements of a single type
  • map to declare a nesting level; in such a case, the module with the FQN is created, carrying the struct of the same behaviour
  • mfa tuple pointing out to the generator for this value

Example

defmodule User do
  use Estructura.Nested
  shape %{
    name: :string,
    address: %{city: :string, street: %{name: [:string], house: :string}},
    data: %{age: :float}
  }
end

%User{}

would result in

%User{
  address: %User.Address{
    city: nil,
    street: %User.Address.Street{house: nil, name: []}
  },
  data: %User.Data{age: nil},
  name: nil
  }
Link to this macro

validate(opts)

View Source (macro)

DSL helper to produce validate callbacks. The syntax is kinda weird, but bear with it, please.

It’s known to produce warnings in credo, I’m working on it.

validate do
  def address.street.postal_code(<<?0, code::binary-size(4)>>),
    do: {:ok, code}
  def address.street.postal_code(code),
    do: {:error, "Not a postal code (#{inspect(code)})"}
end