From e2e3ff2deb11de0350b7ee69c1d0594fc04269dd Mon Sep 17 00:00:00 2001 From: abelino Date: Sat, 28 Sep 2024 14:14:35 -0700 Subject: [PATCH] Only allow binary values for :string types --- lib/speck.ex | 3 ++- protocol/test/wrong_type.ex | 1 + test/speck_test.exs | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/speck.ex b/lib/speck.ex index 28fdb73..c4c3ab7 100644 --- a/lib/speck.ex +++ b/lib/speck.ex @@ -157,7 +157,8 @@ defmodule Speck do end end - defp do_validate(:string, value, _opts), do: to_string(value) + defp do_validate(:string, value, _opts) when is_binary(value), do: value + defp do_validate(:string, _value, _opts), do: {:error, :wrong_type} defp do_validate(:atom, value, _opts) when is_binary(value), do: String.to_atom(value) defp do_validate(:atom, value, _opts) when is_atom(value), do: value diff --git a/protocol/test/wrong_type.ex b/protocol/test/wrong_type.ex index d0cc0a0..f087b81 100644 --- a/protocol/test/wrong_type.ex +++ b/protocol/test/wrong_type.ex @@ -8,3 +8,4 @@ attribute :param3, :boolean attribute :param4, :datetime attribute :param5, :date attribute :param6, :time +attribute :param7, :string diff --git a/test/speck_test.exs b/test/speck_test.exs index 890738a..a347e7e 100644 --- a/test/speck_test.exs +++ b/test/speck_test.exs @@ -98,6 +98,7 @@ defmodule Speck.Test do "param4" => 2.7, "param5" => 2.7, "param6" => 2.7, + "param7" => 0, } assert Speck.validate(TestSchema.WrongType, params) == @@ -108,6 +109,7 @@ defmodule Speck.Test do param4: :wrong_type, param5: :wrong_type, param6: :wrong_type, + param7: :wrong_type, }} end