senconscious

senconscious

Embeds in schemaless changesets

Hello everyone. I want to use schemaless changesets to validate external data. And I wonder how to properly use embeds with them.

Let’s assume I want to validate the nested map under the user key in my params payload. And I want my data to be a map in the end. So to get this work I managed to write this “hack”:

defmodule Params do
  import Ecto.Changeset

  @user_types %{id: :integer}

  @user_fields Map.keys(@user_types)

  @types %{
    name: :string,
    page: :integer,
    page_size: :integer,
    ages: {:array, :integer},
    user:
      {:embed,
       elem(
         Ecto.ParameterizedType.init(Ecto.Embedded,
           cardinality: :one,
           related: __MODULE__,
           field: :user
         ),
         2
       )}
  }

  @default %{
    page: 1,
    page_size: 10
  }

  @fields Map.keys(@types) -- [:user]

  def build(entity \\ @default, attrs \\ %{}) do
    entity
    |> changeset(attrs)
    |> apply_action(:insert)
  end

  def changeset(entity, attrs) do
    {entity, @types}
    |> cast(attrs, @fields)
    |> cast_embed(:user, with: &user_changeset/2)
  end

  def user_changeset(entity, attrs) do
    {entity, @user_types}
    |> cast(attrs, @user_fields)
  end

  def __schema__(:primary_key), do: []
  def __struct__, do: %{}
end

Although it’s kinda working doesn’t seems good. As I need to define __schema__/1 and __struct__/0 functons with custom behavior while setting related key to not being relation module, but parent module.

I want to know better approaches to how to validate nested data using schemaless changesets. Or maybe that’s not proper usage of schemaless changeset. And I need to use schema/embedded_schema to avoid “hacking” ?

Most Liked

jswanner

jswanner

When wanting to validate external data with Ecto like this and it’s a nested data structure, I prefer using embedded_schema over schemaless changesets (which I use when validating flat data structures). I’ve also used Goal, which is a wrapper around Ecto.

I’ve also been wanting to try the aforementioned Drops library, but have not yet done so.

sodapopcan

sodapopcan

You should check out Drops which seems like it would be more suited to what you’re looking for. Sorry I don’t have a direct answer. I do use Ecto for non-database-backed validation but always with a(n embedded) schema. I’m actually not sure how it would work without one as the casting functions are looking at the schema definition for the types. But again, no experience with schemaless, so I’m not saying there is not a way for sure, just recommending Drops :slight_smile:

senconscious

senconscious

The reasons why I’m looking for a solution using schemaless changesets (instead of an easy solution with embedded schemas) are:

  1. I want my success payload to be a map. So I don’t need to invoke Map.from_struct/1 function recursively or define implementation of protocols (like Enumarable);
  2. I want my success payload to contain only those keys that were sent by a user. In some scenarios it’s important whether the key was explicitly set to nil or it wasn’t included in the payload;

There’re plenty of libraries that solves this problems with building schemas under the hood. But I look for more simpler solution that doesn’t require any external deps.

One more idea that I’ve got is define key with :map type. And writing in changeset custom function to put value after applying changes. The code for this solution:

defmodule NewParams do
    import Ecto.Changeset

    @user_types %{id: :integer}

    @user_fields Map.keys(@user_types)

    @types %{
      name: :string,
      page: :integer,
      page_size: :integer,
      ages: {:array, :integer},
      user: :map
    }

    @default %{
      page: 1,
      page_size: 10
    }

    @fields Map.keys(@types)

    def build(entity \\ @default, attrs \\ %{}) do
      @default
      |> Map.merge(entity)
      |> changeset(attrs)
      |> apply_action(:insert)
    end

    def changeset(entity, attrs) do
      {entity, @types}
      |> cast(attrs, @fields)
      |> cast_user()
    end

    def cast_user(%{changes: %{user: changes_user}} = changeset) do
      data_user = changeset.data[:user] || %{}

      case build_user(data_user, changes_user) do
        {:ok, user} ->
          put_change(changeset, :user, user)

        {:error, user_changeset} ->
          changeset
          |> put_change(:user, user_changeset)
          |> Map.put(:valid?, false)
      end
    end

    def cast_user(changeset), do: changeset

    def build_user(entity, attrs) do
      entity
      |> user_changeset(attrs)
      |> apply_action(:validate)
    end

    def user_changeset(entity, attrs) do
      {entity, @user_types}
      |> cast(attrs, @user_fields)
    end
  end

But not sure whether it’s better than using schemaless embed type. Still looking for solution

rhcarvalho

rhcarvalho

This thread sparkled my curiosity, was it possible to use schemaless changesets with nested data?

It could simplify a form I’m writing that edits multiple entities at once (and they could be expressed as a multi-level map).

senconscious

senconscious

should be hackable according to the first message

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

We're in Beta

About us Mission Statement