drops
drops copied to clipboard
Mixing optional and bare field names results in function clause error
The README has this example:
defmodule UserContract do
use Drops.Contract
schema do
%{
optional(:name) => string(),
:name => string()
}
end
end
UserContract.conform(%{email: "janedoe.org"})
# {:ok, %{email: "janedoe.org"}}
UserContract.conform(%{name: "Jane", email: "janedoe.org"})
# {:ok, %{name: "Jane", email: "janedoe.org"}}
From context, it appears that the schema should be:
schema do
%{
optional(:name) => string(),
:email => string()
}
end
Either way, compiling this code results in the following error:
== Compilation error in file lib/user_contract.ex ==
** (FunctionClauseError) no function clause matching in anonymous fn/1 in Drops.Type.Compiler.visit/2
The following arguments were given to anonymous fn/1 in Drops.Type.Compiler.visit/2:
# 1
{:email, {:type, {:string, []}}}
(drops 0.2.1) lib/drops/type/compiler.ex:35: anonymous fn/1 in Drops.Type.Compiler.visit/2
(elixir 1.18.4) lib/enum.ex:1722: anonymous fn/3 in Enum.map/2
(stdlib 7.0.2) maps.erl:894: :maps.fold_1/4
(elixir 1.18.4) lib/enum.ex:2558: Enum.map/2
(drops 0.2.1) lib/drops/type/compiler.ex:35: Drops.Type.Compiler.visit/2
lib/user_contract.ex:4: (module)
Wrapping the second key in required(...) makes the code compile, however.
Elixir: 1.18.4 Erlang/OTP: 28.0.2
Thanks for reporting this. I'll get that fixed!