diff --git a/lib/peri.ex b/lib/peri.ex index 9e3ed07..37730b8 100644 --- a/lib/peri.ex +++ b/lib/peri.ex @@ -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 @@ -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 diff --git a/mix.exs b/mix.exs index 7a299e7..a7cc49d 100644 --- a/mix.exs +++ b/mix.exs @@ -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 diff --git a/test/peri_test.exs b/test/peri_test.exs index 4a22949..086433c 100644 --- a/test/peri_test.exs +++ b/test/peri_test.exs @@ -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, @@ -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