Is it possible to validate an embedded schema at runtime?

I have a schema that has a dynamic JSON field (check_result):

schema "checks" do
  field(:check_name, :string)
  field(:version, :integer)
  field(:status, Ecto.Enum, values: [:success, :failure, :inactive, :warning])
  field(:check_result, :map)


I have separated embedded schemas for different scenarios, an example would be:

embedded_schema do
  field(:content_type_options, {:array, :string})

The job of these embedded schemas is casting of data and validation.

I would like to have the ability of casting/validating data when the data is fetched from the database, however I would like it to be loosely coupled, for example the approach that polymorphic embeds uses is not what I am looking for, as you have to place all the definitions in the parent schema:

schema "reminders" do
    field :date, :utc_datetime
    field :text, :string

    polymorphic_embeds_one :channel,
      types: [
        sms: MyApp.Channel.SMS,
        email: MyApp.Channel.Email
      on_type_not_found: :raise,
      on_replace: :update

Has anyone dealt with this before?

Not sure I am getting you well but what’s stopping you from authoring a function e.g. Checks.load(id) which fetches stuff from DB and then invokes Changeset functions to do the relevant validation depending on the shape of the data? F.ex.:

defmodule Checks do
  def load(id) do
    record = Repo.get(Check, id)

  def validate(%{check_result: %{one_field: something}}), do: # ...
  def validate(%{check_result: %{another_field: something_else}}), do: # ...

It seems I was missing some knowledge related to how to transform changeset → schema.

After googling for a few times and searching for the wrong thing, I landed on Ecto.Changeset.apply_action/2, that is exactly what I needed, since I can validate the map and convert it to a struct that I can use further in my code.