View Source Parameter.Parametrizable behaviour (Parameter v0.5.1)

Custom types per fields to be implemented using Parameter.Parametrizable module. When the basic types are not enough for loading, validating and dumping data this module can be used to provide custom types.

example

Example

To create a parameterized type, create a module as shown below:

defmodule MyApp.CustomType do
  use Parameter.Parametrizable

  @impl true
  def load(value) do
    {:ok, value}
  end

  @impl true
  def validate(_value) do
    :ok
  end

  @impl true
  def dump(value) do
    {:ok, value}
  end
end

Then use the new custom type on a param schema:

param MyApp.CustomParam do
  field :custom_field, MyApp.CustomType, key: "customField"
end

In general is not necessary to implement dump function since using the macro use Parameter.Parametrizable will already use the validate function to dump the value as a default implementation.

Link to this section Summary

Link to this section Callbacks

@callback dump(any()) :: {:ok, any()} | {:error, any()}
@callback load(any()) :: {:ok, any()} | {:error, any()}
@callback validate(any()) :: :ok | {:error, any()}