zac

zac

How to make dynamic validations, & ref attributes from inside validation?

I’m trying to set up a couple of validations – unclear what the best approach is:

    attribute :allowed_types, {:array, :atom} do
      allow_nil? false
      default [:value_add, :non_value_add]
    end

    attribute :default_type, :atom do
      allow_nil? false
    end

  validations do
    # this works but is clunky (what if allowed set is > 2?)
    validate one_of(:allowed_types, [[:value_add, :non_value_add], [:value_add], [:non_value_add]])
    # this doesn't work ... :allowed_types is supposed to be an attribute, not an atom... :/
    validate one_of(:default_type, [:allowed_types])
  end

Potentially pretty obvious, but the intention of the two validations:

  1. The first is to make sure the :allowed_types conforms to some combination of one or more of a set (currently, just [:value_add, :non_value_add])
  2. The second is to make sure that the value of :default_type is one of the types specified in the attribute :allowed_types

I can live with #1 for now (until the list grows, but then it becomes a problem). #2 seems like it may require a custom validation, but I’m unsure if that’s correct… or how to write one quite yet…

Most Liked

zachdaniel

zachdaniel

Creator of Ash

You can use the constraints on the :atom type to do this, or an Ash.Type.Enum

With an enum type:

defmodule YourApp.Types.AllowedWhateverTypes do
  use Ash.Type.Enum, values: ~w(value_add non_value_add)a
end


attributes do
  attribute :allowed_types, {:array, YourApp.Types.AllowedWhateverTypes} do
    allow_nil? false
  end

  attribute :default_type, YourApp.Types.AllowedWhateverTypes do
    allow_nil? false
  end
end

With constraints:

@allowed_types ~w(value_add non_value_add)a

attributes do
  attribute :allowed_types, {:array, :atom} do
    constraints items: [one_of: @allowed_types]
    allow_nil? false
  end

  attribute :default_type, :atom do
    constraints one_of: @allowed_types
    allow_nil? false
  end
end

Where Next?

Popular in Questions Top

New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
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
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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 39297 209
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement