Is it possible to define a type as follows:
defmodule Role do
use Exnumerator, values: ["admin", "regular", "restricted"]
@type t :: "admin" | "regular" | "restricted"
@spec default() :: t
def default() do
"regular"
end
end
to make a better analyze for the code like:
@type valid_attributes :: %{optional(:email) => String.t,
optional(:password) => String.t,
optional(:role) => Role.t}
@spec changeset(User.t, valid_attributes) :: Ecto.Changeset.t
def changeset(%User{} = user, attrs = %{}) do
# ...
end
# ...
User.changeset(%User{}, %{role: "superadmin"}) |> Repo.insert()
I know that I can define this type as @type t :: String.t
, but then, Dialyzer won't complain about using a different value than possible (possible from the application point of view).
I didn't saw any hints about this use case in the documentation for the Typespecs, but maybe I'm missing something.
dialyzer
is all yours. – Heritable<<"admin">> | ...
work? Looks like it should, according to the linked typespecs page. – Heritable