Chain (reduce) schematics
Nobody has claimed this yet.
- Dominant language
- Elixir
- Stars
- 92
- Forks
- 6
- PR merge metrics
- No merged PRs in 30d
Description
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.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the proposed chain/1 entry point and Schematic.Unification.unify, then compare its behavior with the existing all primitive. Done means chained schematics pass each transformed result to the next, stop on the first error, and reverse order when dumping; verify the examples for integer parsing and cross-field validation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- elixir
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100