RomainT

RomainT

Validate_inclusion with dynamic enum

Hello,

I want to validates a value among possible values with validate_inclusion but I’m stuck.

My use case is an avatar generation system, layer by layer. An avatar is defined by a pack (Dragon Ball, Among Us…) and many layers (body, hat, pants…). Let’s say that you chose the Dragon ball pack that only has 3 bodies while the Among Us pack has 10. I need the enum argument of validates_inclusion(:body, enum) to depend on the pack.

Is it possible? How could I implement that?

Thanks!

Marked As Solved

lud

lud

I dont think the enum has to be static. I think you can pass any valid enumerable.

So something like that should work:

  def changeset(changeset) do
    changeset
    |> cast(...)
    |> validate_required(...)
    |> validate_parts()
  end

  defp validate_parts(changeset) do
    pack = fetch_field!(changeset, :pack)
    
    changeset
    |> validate_inclusion(:body, Packs.parts_for(pack, :body))
    |> validate_inclusion(:pants, Packs.parts_for(pack, :pants))
  end

Also Liked

RomainT

RomainT

It works pefectly, thanks!

Last Post!

RomainT

RomainT

It works pefectly, thanks!

Where Next?

Popular in Questions Top

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
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

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
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement