Gigitsu

Gigitsu

How do you write Ecto schema typespecs with nullable fields?

Hi everyone :waving_hand:

I’m trying to improve the typespecs in my application contexts, but I’m running into dialyzer errors when dealing with schemas that have fields that can be nil.

Here’s an example:

defmodule MyApp.User do
  use Ecto.Schema

  import Ecto.Changeset

  @type t :: %__MODULE__{
          id: integer(),
          email: String.t(),
          age: non_neg_integer()
        }

  schema "users" do
    field :email, :string
    field :age, :integer
  end

  def changeset(user, attrs) do
    user |> cast(attrs, [:email, :age]) |> validate_required(:email)
  end
end

defmodule MyApp.UsersContext do
  alias MyApp.User

  @spec change_user(user :: User.t(), attrs :: map()) :: Ecto.Changeset.t()
  def change_user(%User{} = user, attrs \\ %{}) do
    User.changeset(user, attrs)
  end
end

defmodule MyApp.FakeController do
  alias MyApp.User

  def index() do
    MyApp.UsersContext.change_user(%User{}, %{})

    :ok
  end
end

In this schema, there are no default values so %User{} will generate a struct where every field is nil.

Using this setup, dialyzer complains with:

The function call will not succeed.

MyApp.UsersContext.change_user(
  %MyApp.User{
    :__meta__ => %Ecto.Schema.Metadata{
      :context => nil,
      :prefix => nil,
      :schema => MyApp.User,
      :source => <<117, 115, 101, 114, 115>>,
      :state => :built
    },
    :age => nil,
    :email => nil,
    :id => nil
  },
  %{}
)

breaks the contract
(user :: MyApp.User.t(), attrs :: map()) :: Ecto.Changeset.t()

To fix this error I have to explicitly put | nil in MyApp.User.t() type:

  @type t :: %__MODULE__{
          id: integer() | nil,
          email: String.t() | nil,
          age: non_neg_integer() | nil
        }

But here are some things I’m unsure about:

  • Is it idiomatic to be so explicit with | nil for every nullable field?
  • Is there a better or preferred way to declare the schema type (@type t) that keeps it maintainable and readable, especially in large schemas?

Another possible solution I’ve found is to declare, somewhere in the codebase, a generic schema typespec:

  @type schema_t(schema) :: %{
          optional(atom) => any,
          __struct__: schema,
          __meta__: Ecto.Schema.Metadata.t(schema)
        }

and use it in my specs:

  @spec change_user(user :: schema_t(User), attrs :: map()) :: Ecto.Changeset.t()

Curious how others approach this.

Thanks!

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

The short answer is yes.

integer() is simply different from integer() | nil. Your code is using %User{} which absolutely does not have an id value yet, it hasn’t been saved to the DB, so it isn’t valid to claim it has an integer id in all cases.

If you had code which did user.id + 10 that’s going to raise an exception, and you WANT dialyzer to give you heads up about such things.

Also Liked

billylanchantin

billylanchantin

Curious how others approach this.

There’s also the TypedEctoSchema library:

It auto-generates the typespec boiler plate for your Ecto schemas. It has sensible defaults, and you can use :: inline to override the typespec for a specific fields.

dogweather

dogweather

TypedEctoSchema and TypedStruct are life-savers. They eliminate unnecessary verbosity and make schema definitions readable.

Gigitsu

Gigitsu

I like this approach, and your struct_t resembles my idea of a schema_t type, though with less enforcement. Thank you.

I agree. I once wrote a typed_schema macro myself, but it started to feel like it was adding unnecessary complexity and non-standard conventions on top of Ecto, so I went back to manually crafted typespecs.

Where Next?

Popular in Questions Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New

We're in Beta

About us Mission Statement