Unexpected error raised when validating `embeds_many` field
- Dominant language
- Elixir
- Stars
- 80
- Forks
- 8
- PR merge metrics
- No merged PRs in 30d
Description
## Description
We are using `strukt` for validating external data input. For example we defined a schema with `embeds_many` field: `bar`:
```Elixir
defmodule Foo do
use Strukt
defstruct do
embeds_many :bar, Bar do
field :baz, :string
end
end
end
```
when using `new/1` to parsing external data with wrong "embedded" type, it raise an unexpected error:
```
iex(2)> Foo.new(%{bar: "baz"})
** (Protocol.UndefinedError) protocol Enumerable not implemented for "baz" of type BitString. This protocol is implemented for the following type(s): Date.Range, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, Jason.OrderedObject, List, Map, MapSet, Range, Stream
(elixir 1.16.1) lib/enum.ex:1: Enumerable.impl_for!/1
(elixir 1.16.1) lib/enum.ex:166: Enumerable.reduce/3
(elixir 1.16.1) lib/enum.ex:4399: Enum.map_reduce/3
(elixir 1.16.1) lib/enum.ex:3865: Enum.with_index/2
(strukt 0.3.2) lib/params.ex:41: Strukt.Params.transform/4
(strukt 0.3.2) lib/params.ex:72: Strukt.Params.map_value_to_field/4
(elixir 1.16.1) lib/enum.ex:1700: Enum."-map/2-lists^map/1-1-"/2
iex:2: (file)
```
According to the stacktrace, it happens during conforming params:
https://github.com/bitwalker/strukt/blob/b2b853256b0360599d67ca9f44b361c2a2a75beb/lib/strukt.ex#L428
## Expected behaviour
returns a validation error:
```Elixir
{:error,
#Ecto.Changeset<
action: :insert,
changes: %{},
errors: [bar: {"is invalid", [validation: :embed, type: {:array, :map}]}],
data: #Foo<>,
valid?: false
>}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.