Skip to content

Commit

Permalink
fix: do not raise on schemas with string keys (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
iurimateus authored Jul 23, 2024
1 parent b79dfbd commit 266b5a2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/peri.ex
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ defmodule Peri do
end

defp enumerable_has_key?(data, key) when is_map(data) do
Map.has_key?(data, key) or Map.has_key?(data, Atom.to_string(key))
Map.has_key?(data, key) or Map.has_key?(data, (is_binary(key) && key) || Atom.to_string(key))
end

defp enumerable_has_key?(data, key) when is_list(data) do
Expand Down Expand Up @@ -334,7 +334,7 @@ defmodule Peri do

defp get_enumerable_value(enum, key) do
case Access.get(enum, key) do
nil when is_map(enum) -> Map.get(enum, Atom.to_string(key))
nil when is_map(enum) -> Map.get(enum, (is_binary(key) && key) || Atom.to_string(key))
val -> val
end
end
Expand Down
13 changes: 13 additions & 0 deletions test/peri_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ defmodule PeriTest do
email: {:required, :string}
})

defschema(:simple_mixed_keys, %{
"email" => {:required, :string},
name: :string,
age: :integer
})

defschema(:nested, %{
user: %{
name: :string,
Expand Down Expand Up @@ -68,6 +74,13 @@ defmodule PeriTest do
} =
simple(data)
end

test "does not raise on simple schema with string keys" do
data = %{name: "John", age: 30}

assert {:error, [%Peri.Error{path: ["email"], message: "is required"}]} =
simple_mixed_keys(data)
end
end

describe "nested schema validation" do
Expand Down

0 comments on commit 266b5a2

Please sign in to comment.