Skip to content

Commit

Permalink
Add array enum support (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
davorbadrov authored Oct 17, 2024
1 parent e08a2e2 commit 97133f6
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/goal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,19 @@ defmodule Goal do
defp get_types(schema) do
Enum.reduce(schema, %{}, fn {field, rules}, acc ->
case Keyword.get(rules, :type, :any) do
{:array, :enum} ->
values =
rules
|> Keyword.get(:rules, [])
|> Keyword.get(:values, [])
|> Enum.map(&atomize/1)

Map.put(
acc,
field,
{:array, {:parameterized, {Ecto.Enum, Ecto.Enum.init(values: values)}}}
)

:enum ->
values =
rules
Expand Down Expand Up @@ -870,6 +883,9 @@ defmodule Goal do
when type in [:string, :integer, :decimal, :float, :boolean, :date, :time] ->
validate_array_basic_field(changes, field, schema, type, acc)

{field, {:array, {:parameterized, _}}}, acc ->
validate_array_enum_field(field, schema, acc)

{_field, _type}, acc ->
acc
end)
Expand Down Expand Up @@ -983,6 +999,16 @@ defmodule Goal do
end
end

defp validate_array_enum_field(field, schema, changeset) do
item_rules =
schema
|> Map.get(field)
|> Keyword.get(:rules, [])
|> Keyword.delete(:values)

validate_fields(item_rules, field, changeset)
end

defp atomize(atom) when is_atom(atom), do: atom
defp atomize(string) when is_binary(string), do: String.to_atom(string)

Expand Down
62 changes: 62 additions & 0 deletions test/goal_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,68 @@ defmodule GoalTest do
assert errors_on(changeset) == %{name: ["should be at most 3 character(s)"]}
end

test "list with enums, string-based" do
schema = %{
list: [type: {:array, :enum}, required: true, rules: [values: ["active", "inactive"]]]
}

changeset_1 = Goal.build_changeset(schema, %{"list" => ["active", "inactive"]})
assert changes_on(changeset_1) == %{list: [:active, :inactive]}

changeset_2 = Goal.build_changeset(schema, %{})
assert errors_on(changeset_2) == %{list: ["can't be blank"]}
end

test "list with enums, atom-based" do
schema = %{
list: [
type: {:array, :enum},
required: true,
rules: [values: [:active, :inactive], min: 1]
]
}

changeset_1 = Goal.build_changeset(schema, %{"list" => ["active", "inactive"]})
assert changes_on(changeset_1) == %{list: [:active, :inactive]}

changeset_2 = Goal.build_changeset(schema, %{})
assert errors_on(changeset_2) == %{list: ["can't be blank"]}

changeset_3 = Goal.build_changeset(schema, %{list: []})
assert errors_on(changeset_3) == %{list: ["should have at least 1 item(s)"]}
end

test "list with enums, nested inside of map" do
data = %{
"id" => "1",
"nested_map" => %{
"key" => "f45fb959-b0f9-4a32-b6ca-d32bdb53ee8e",
"list" => ["active"]
}
}

schema = %{
id: [type: :integer],
nested_map: [
type: :map,
properties: %{
key: [type: :string, format: :uuid],
list: [type: {:array, :enum}, rules: [values: ["active", "inactive"]]]
}
]
}

changeset = Goal.build_changeset(schema, data)

assert changes_on(changeset) == %{
id: 1,
nested_map: %{
key: "f45fb959-b0f9-4a32-b6ca-d32bdb53ee8e",
list: [:active]
}
}
end

test "missing schema rules" do
data = %{
"string_1" => "world",
Expand Down

0 comments on commit 97133f6

Please sign in to comment.