Skip to content

Commit

Permalink
Handle nested map error properly
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy351 committed Jul 3, 2016
1 parent d10e3a5 commit 29cd7eb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
12 changes: 9 additions & 3 deletions lib/mayo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ defmodule Mayo do
iex> Mayo.validate %{username: "test"}, %{
...> username: Mayo.Any.string |> Mayo.String.min(6)
...> }
{:error, %Mayo.Error{type: "string.min", path: :username}}
{:error, %Mayo.Error{type: "string.min", paths: [:username]}}
"""
defmacro validate(value, schema) do
compile(schema, value)
Expand Down Expand Up @@ -64,8 +64,14 @@ defmodule Mayo do

_ ->
case unquote(pipe) do
{:error, err} -> {:error, %{err | path: unquote(key)}}
result -> Map.put(unquote(acc), unquote(key), result)
result when is_nil(result) ->
unquote(acc)

{:error, %Mayo.Error{paths: paths} = err} ->
{:error, %{err | paths: [unquote(key) | paths]}}

result ->
Map.put(unquote(acc), unquote(key), result)
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/mayo/error.ex
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
defmodule Mayo.Error do
defstruct [:type, :path]
defstruct type: "",
paths: []
end

0 comments on commit 29cd7eb

Please sign in to comment.