Xantrac

Xantrac

Embed Validation for Form data

Good evening,

I’m working on a system where an ecto schema has an embed JSON field. I was trying to figure out what is the best option.

The schema

  schema "integrations" do
    field :type, :string
    field :configuration, :map
    belongs_to :organization, Organization

    timestamps()
  end

let’s skip the protocol and behavior enforcement because the configuration is polymorphic, for now, this is one of the shapes the configuration embed can have

defmodule ConfigA.Auth do
  defstruct project_id: "", api_key: ""
end

defmodule ConfigA do
  defstruct authentication: %ConfigA.Auth{}
end

Given ConfigA signature, and given the data will come from a Phoenix form, where is the best place to validate the data?

In the Changeset signature, which

  def config_a_changeset(%Integration{type: "config_a"} = integration, %{configurations: %Config{}} = attrs) do
    Integration.create_changeset(integration)
  end

or through casting the raw string inside the create_changeset function.

Or is there a better way to achieve this?

Thank you

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
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
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement