Skip to content

Commit

Permalink
Check item rules for array maps without properties
Browse files Browse the repository at this point in the history
  • Loading branch information
martinthenth committed Nov 20, 2024
1 parent 5bc9b26 commit 1f53222
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
8 changes: 7 additions & 1 deletion lib/goal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,13 @@ defmodule Goal do
|> put_in([Access.key(:changes), Access.key(field)], Enum.reverse(changesets))
|> Map.update!(:valid?, &Kernel.&&(&1, valid?))
else
changeset
item_rules = Keyword.get(rules, :rules)

if item_rules do
validate_fields(item_rules, field, changeset)
else
changeset
end
end
end

Expand Down
22 changes: 19 additions & 3 deletions test/goal_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,9 +1126,7 @@ defmodule GoalTest do
]
}

schema = %{
list: [type: {:array, :map}]
}
schema = %{list: [type: {:array, :map}]}

changeset = Goal.build_changeset(schema, data)

Expand Down Expand Up @@ -1163,6 +1161,24 @@ defmodule GoalTest do
}
end

test "list of maps, min and max" do
schema = %{
list: [
type: {:array, :map},
rules: [min: 1, max: 1]
]
}

data_1 = %{"list" => []}
data_2 = %{"list" => [%{"hello" => "world"}, %{"hello" => "world"}]}

changeset_1 = Goal.build_changeset(schema, data_1)
changeset_2 = Goal.build_changeset(schema, data_2)

assert errors_on(changeset_1) == %{list: ["should have at least 1 item(s)"]}
assert errors_on(changeset_2) == %{list: ["should have at most 1 item(s)"]}
end

test "list of maps, invalid type given" do
schema = %{
list: [
Expand Down

0 comments on commit 1f53222

Please sign in to comment.