Skip to content

Commit

Permalink
Merge branch 'main' of github.com:zoedsoupe/peri
Browse files Browse the repository at this point in the history
* 'main' of github.com:zoedsoupe/peri:
  fix: do not raise on schemas with string keys (#4)
  • Loading branch information
zoedsoupe committed Jul 23, 2024
2 parents cb1b250 + 266b5a2 commit f8702b8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 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
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Peri.MixProject do
use Mix.Project

@version "0.2.6"
@version "0.2.7"
@source_url "https://github.com/zoedsoupe/peri"

def project do
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 f8702b8

Please sign in to comment.