Skip to content

Commit

Permalink
Merge pull request #16 from alexxero/unexpected-fields-wont-break-code
Browse files Browse the repository at this point in the history
Unexpected fields wont result an exception
  • Loading branch information
alexxero authored Jan 20, 2025
2 parents e7cc423 + d9e6ce7 commit 10a4beb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/types/map_type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ defmodule Talos.Types.MapType do
end

defp fields_by_key(keys, fields) do
Enum.map(keys, fn key -> Enum.find(fields, &(&1.key == key)) end)
keys
|> Enum.map(fn key -> Enum.find(fields, &(&1.key == key)) end)
|> Enum.filter(&(!is_nil(&1)))
end

defp find_field_errors(fields, map) do
Expand Down
14 changes: 14 additions & 0 deletions test/types/map_type_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -301,4 +301,18 @@ defmodule Talos.Types.MapTypeTest do
assert %{} == MapType.errors(schema, %{"name" => "Dmitry"})
assert %{} == MapType.errors(schema, %{"age" => 88})
end

test "#errors - with required_any_one = true and unexpected keys" do
schema =
map(
required_any_one: true,
fields: [
field(key: "name", type: string()),
field(key: "age", type: integer(gteq: 18))
]
)

assert %{} ==
MapType.errors(schema, %{"age" => 18, "unexpected" => true})
end
end

0 comments on commit 10a4beb

Please sign in to comment.