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.

Gigitsu

Gigitsu

@benwilson512 so being explicit is the only option here. I was hoping there’d be a shortcut for defining nullable fields. Thanks!

@billylanchantin thank you for the answer. I’ve seen some libraries (like the one you provided) used to help with this, but I’m trying to keep things vanilla Ecto if possible.

zorn

zorn

In some of my recent projects, I’ve preferred to model the schema type to be a description of a repo-sourced value, thus allowing me to type things like inserted_at: DateTime.t() instead of inserted_at: DateTime.t() | nil.

To allow for some functions which expected a non-repo-sourced value I would make a @type struct_t() :: %__MODULE__{}.

I can’t say I observe many other Elixir codebases doing this, and my quick round-the-room check with some other devs says this nuance was not on their radar.

Where Next?

Popular in Questions Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New

We're in Beta

About us Mission Statement