Skip to content

Commit

Permalink
Only allow binary values for :string types
Browse files Browse the repository at this point in the history
  • Loading branch information
abelino committed Sep 28, 2024
1 parent ea9e08f commit e2e3ff2
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/speck.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions protocol/test/wrong_type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ attribute :param3, :boolean
attribute :param4, :datetime
attribute :param5, :date
attribute :param6, :time
attribute :param7, :string
2 changes: 2 additions & 0 deletions test/speck_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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) ==
Expand All @@ -108,6 +109,7 @@ defmodule Speck.Test do
param4: :wrong_type,
param5: :wrong_type,
param6: :wrong_type,
param7: :wrong_type,
}}
end

Expand Down

0 comments on commit e2e3ff2

Please sign in to comment.