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