schematic icon indicating copy to clipboard operation
schematic copied to clipboard

Chain (reduce) schematics

Open jfpedroza opened this issue 1 year ago • 0 comments

When adding extra validations to a schematic, like "this field must have a value when this other field has this value" or "this integer must be positive", none of the primitives allow easily doing that.

all seems to be an option for that, but I find that it has two issues. It returns a list of errors instead of stopping at the first occurrence. When one schematic makes a transformation, the following don't have access to it and must deal with the "raw" input.

So, I came up with this function:

  @doc """
  Returns a schematic that reduces over the list of schematics, passing the output
  of one as the input of the next and stopping on the first error.

  When dumping, the order of the schematics is reversed.
  """
  @spec chain([Schematic.t()]) :: Schematic.t()
  def chain(schematics) when is_list(schematics) do
    message = fn -> Enum.map(schematics, & &1.message) end

    %Schematic{
      kind: "chain",
      message: message,
      unify: fn input, dir ->
        schematics =
          case dir do
            :to -> schematics
            :from -> Enum.reverse(schematics)
          end

        Enum.reduce_while(schematics, {:ok, input}, fn schematic, {:ok, input} ->
          case Schematic.Unification.unify(schematic, input, dir) do
            {:ok, _} = result -> {:cont, result}
            {:error, _} = error -> {:halt, error}
          end
        end)
      end
    }
  end

Examples

iex> schematic = chain([int(), raw(&(Kernel.rem(&1, 2) == 0), message: "must be divisible by 2")])
iex> unify(schematic, 8)
{:ok, 8}
iex> dump(schematic, 8)
{:ok, 8}
iex> unify(schematic, "8")
{:error, "expected an integer"}
iex> unify(schematic, 15)
{:error, "must be divisible by 2"}
main_schematic = schema(__MODULE__, %{foo: str(), bar: nullable(int())})
bar_schematic = oneof(fn
  %__MODULE__{foo: foo, bar: bar} ->
    if some_validation(foo, bar) do
      any()
    else
      {:error, "expected something"}
    end
  _ ->
    # Let through other values so that when dumping the main schematic returns a proper message
    any()
)

chain([main_schematic, bar_schematic])

What do you think?

I can open a PR if this is something that could be in the library.

jfpedroza avatar Jan 30 '25 04:01 jfpedroza