作为参数的地图列表(A list of maps as argument)

编程入门 行业动态 更新时间:2024-10-11 21:22:48
作为参数的地图列表(A list of maps as argument)

我创建了一个如下所示的模式:

schema "countries" do field :code, :string field :en, :string field :de, :string field :it, :string field :fr, :string timestamps end

现在我想创建一个接受地图列表的变更集功能。 我该如何定义它? 这是对的(我会说这是错的):

def changeset_all(model,[])do

那么如何验证传递的参数是否包含地图?

第二个问题是,我怎样才能遍历地图列表,在获得所有传递值的变更后? 我会这样做:

def changeset_all(model, [params]) do for param <- [params] do model |> cast(param, [:code, :en, :de, :it, :fr]) |> validate_required([:code, :en]) |> validate_length(:code, max: 3) |> unique_constraint(:code) end end

我希望我的问题很清楚。

I created a schema that looks as follow:

schema "countries" do field :code, :string field :en, :string field :de, :string field :it, :string field :fr, :string timestamps end

Now a I want to create changeset function that would accept a list of map. How can I define it? Is this right(I would say it is wrong):

def changeset_all(model, []) do

Then how can I validate if the passed parameter contains maps or not?

The second question is, how can I loop through the list of maps, that after I would get a changeset of all pass values? I would do like this:

def changeset_all(model, [params]) do for param <- [params] do model |> cast(param, [:code, :en, :de, :it, :fr]) |> validate_required([:code, :en]) |> validate_length(:code, max: 3) |> unique_constraint(:code) end end

I hope my questions are clear.

最满意答案

def changeset_all(model, [params]) do只接受1个元素的列表。 要接受任何列表,您可以:

def changeset_all(model, params) when is_list(params) do

要创建变更集列表,只需for param <- params 。 最终代码应如下所示:

def changeset_all(model, params) when is_list(params) do for param <- params do model |> cast(param, [:code, :en, :de, :it, :fr]) |> validate_required([:code, :en]) |> validate_length(:code, max: 3) |> unique_constraint(:code) end end

那么如何验证传递的参数是否包含地图?

你真的不需要这样做,就像传递一个无效的参数一样, cast会处理抛出错误。 如果你想这样做,你可以遍历列表并使用if来检查它是否是一张地图:

for param <- params if !is_map(param) do raise "#{param} is not a map" end end for param <- params do # changeset stuff end

我建议单独创建一个changeset/2函数,然后在changeset_all/2使用Enum.map调用它:

def changeset(model, params) do model |> cast(params, [:code, :en, :de, :it, :fr]) |> validate_required([:code, :en]) |> validate_length(:code, max: 3) |> unique_constraint(:code) end def changeset_all(model, list) do list |> Enum.map(fn params -> changeset(model, params) end) end

def changeset_all(model, [params]) do will only accept a list of 1 element. To accept any lists, you can do:

def changeset_all(model, params) when is_list(params) do

To create a list of changesets, just do for param <- params. The final code should look like:

def changeset_all(model, params) when is_list(params) do for param <- params do model |> cast(param, [:code, :en, :de, :it, :fr]) |> validate_required([:code, :en]) |> validate_length(:code, max: 3) |> unique_constraint(:code) end end

Then how can I validate if the passed parameter contains maps or not?

You don't really need to do this as if you pass an invalid parameter, cast will handle throwing an error. If you want to do this, you can loop through the list and use if to check if it's a map:

for param <- params if !is_map(param) do raise "#{param} is not a map" end end for param <- params do # changeset stuff end

I would recommend creating a changeset/2 function separately and then calling it using Enum.map in changeset_all/2:

def changeset(model, params) do model |> cast(params, [:code, :en, :de, :it, :fr]) |> validate_required([:code, :en]) |> validate_length(:code, max: 3) |> unique_constraint(:code) end def changeset_all(model, list) do list |> Enum.map(fn params -> changeset(model, params) end) end

更多推荐

本文发布于:2023-08-01 05:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353901.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   地图   列表   argument   maps

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!