@@ -12,7 +12,9 @@ defmodule Norm.Spec.Collection do
1212 alias Norm.Conformer.Conformable
1313
1414 def conform ( % { spec: spec , opts: opts } , input , path ) do
15- with :ok <- check_distinct ( input , path , opts ) ,
15+ with :ok <- check_enumerable ( input , path , opts ) ,
16+ :ok <- check_map_of ( input , path , opts ) ,
17+ :ok <- check_distinct ( input , path , opts ) ,
1618 :ok <- check_counts ( input , path , opts ) do
1719 results =
1820 input
@@ -60,6 +62,29 @@ defmodule Norm.Spec.Collection do
6062 :ok
6163 end
6264 end
65+
66+ defp check_enumerable ( input , path , _opts ) do
67+ if Enumerable . impl_for ( input ) == nil do
68+ { :error , [ Conformer . error ( path , input , "not enumerable" ) ] }
69+ else
70+ :ok
71+ end
72+ end
73+
74+ defp check_map_of ( input , path , opts ) do
75+ cond do
76+ # if coll_of was used, accept every kind of list
77+ opts [ :into ] == [ ] ->
78+ :ok
79+
80+ # in case of map_of, check the format of the enumerable
81+ Enum . all? ( input , & match? ( { _ , _ } , & 1 ) ) ->
82+ :ok
83+
84+ true ->
85+ { :error , [ Conformer . error ( path , input , "not a map" ) ] }
86+ end
87+ end
6388 end
6489
6590 if Code . ensure_loaded? ( StreamData ) do
0 commit comments