Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not raise on schema/data with string keys #4

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading