elixir - Ecto changeset add multiple errors -


i have config map want validate inside changeset. config has multiple keys , each of them has constraints.

i run validator each of them when data received forces me write lot of code can done smarter i'm running loop , trying gather failed validations instead of running them 1 one.

defp validate_config_flags(changeset)     config = get_field(changeset, :config)       {k, v} <- config          if !map.has_key?(@config_flags, k) || !validate_config_flag(k, v)             add_error(changeset, :"#{k}", "invalid value `#{k}`")         end     end      changeset end 

obv problem return changeset though fields fail , question related that. how add more 1 error message/failed validation result instead of returning @ first add_error somehow?

most of times when want repeatedly modify term in elixir, you're looking enum.reduce/3:

defp validate_config_flags(changeset)   enum.reduce(get_field(changeset, :config), changeset, fn {k, v}, changeset ->     if !map.has_key?(@config_flags, k) || !validate_config_flag(k, v)       add_error(changeset, :"#{k}", "invalid value `#{k}`")     else       changeset     end   end) end 

this code equivalent yours keeps track of new changeset returned add_error @ every step, similar following if elixir had mutable variables:

for {k, v} <- config    if !map.has_key?(@config_flags, k) || !validate_config_flag(k, v)     changeset = add_error(changeset, :"#{k}", "invalid value `#{k}`")   end end 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -