Embeds_one with either 2 schema

Hello *

We have an Ecto embedded schema that we use to validate a request payload to our API (our schema is not tied to a database, we are using it just for validation purposes).
We had an :action field which wasn’t validated and it had 2 kind of different possible “types”, now we would like to validate it against 2 different schemas according to the action type.

Hello *

We have an Ecto embedded schema that we use to validate a request payload to our API (our schema is not tied to a database, we are using it just for validation purposes).
We had an :action field which wasn’t validated and it had 2 kind of different possible “types”, now we would like to validate it against 2 different schemas according to the action type.

MainSchema:

embedded_schema do
  field :name, :string
  embeds_one :action, either: [FooAction, BarAction] 
end

FooAction and BarAction are 2 different Ecto embedded schemas with their own validation rules and different fields.

FooAction:

embedded_schema do
   field :a, :string
   field :b, :string
end

BarAction

embedded_schema do
   field :c, :integer
end

Use a changeset (possibly schemaless) to validate the action. Then do a case statement validating depending on the selected action.

I think I’m still far away from a result, now what I get when I do apply_action is a {ok, map} (the result) which contains some changesets.
I would love to use put_embed but it requires to have a valid embed which requires an Ecto.Relation as a field type, however it doesn’t seem to me a public API.

(I answered prev. before you edited your entry post)
How exactly do you know if action is of type FooAction or BarAction?

I just pattern match on the parameters before validation. I just need to pattern match for “a” and “c”.