Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support nested maps #56

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/vex/extract.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ defimpl Vex.Extract, for: Map do
def settings(map) do
Map.get(map, :_vex)
end

def attribute(map, path) when is_list(path) do
get_in map, path
end
def attribute(map, name) do
Map.get(map, name)
end
Expand Down
50 changes: 43 additions & 7 deletions test/validations/nested_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,61 @@ end
defmodule NestedTest do
use ExUnit.Case

test "nested" do
test "keyword list, nested" do
assert Vex.valid?([author: [name: "Foo"]], %{[:author, :name] => [presence: true]})
assert Vex.valid?([author: %{name: "Foo"}], %{[:author, :name] => [presence: true]})

nested_errors = [{:error, [:author, :name], :presence, "must be present"}]
assert nested_errors == Vex.errors([author: [name: ""]], %{[:author, :name] => [presence: true]})
nested_errors_list = [{:error, [:author, :name], :presence, "must be present"}]
assert nested_errors_list == Vex.errors([author: [name: ""]], %{[:author, :name] => [presence: true]})

nested_errors_mixed = [{:error, [:author, :name], :presence, "must be present"}]
assert nested_errors_mixed == Vex.errors([author: %{name: ""}], %{[:author, :name] => [presence: true]})
end

test "nested with _vex" do
test "keyword list, nested with _vex" do
assert Vex.valid?([author: [name: "Foo"], _vex: %{[:author, :name] => [presence: true]}])
assert Vex.valid?([author: %{name: "Foo"}, _vex: %{[:author, :name] => [presence: true]}])

nested_errors = [{:error, [:author, :name], :presence, "must be present"}]
assert nested_errors == Vex.errors([author: [name: ""], _vex: %{[:author, :name] => [presence: true]}])
nested_errors_list = [{:error, [:author, :name], :presence, "must be present"}]
assert nested_errors_list == Vex.errors([author: [name: ""], _vex: %{[:author, :name] => [presence: true]}])

nested_errors_mixed = [{:error, [:author, :name], :presence, "must be present"}]
assert nested_errors_mixed == Vex.errors([author: %{name: ""}, _vex: %{[:author, :name] => [presence: true]}])
end

test "nested in Record" do
test "keyword list, nested in Record" do
assert Vex.valid?(%NestedTestRecord{author: [name: "Foo"]})

nested_errors = [{:error, [:author, :name], :presence, "must be present"}]
assert nested_errors == Vex.errors(%NestedTestRecord{author: [name: ""]})
end

test "map, nested" do
assert Vex.valid?(%{author: %{name: "Foo"}}, %{[:author, :name] => [presence: true]})
assert Vex.valid?(%{author: [name: "Foo"]}, %{[:author, :name] => [presence: true]})

nested_errors_map = [{:error, [:author, :name], :presence, "must be present"}]
assert nested_errors_map == Vex.errors(%{author: %{name: ""}}, %{[:author, :name] => [presence: true]})

nested_errors_mixed = [{:error, [:author, :name], :presence, "must be present"}]
assert nested_errors_mixed == Vex.errors(%{author: [name: ""]}, %{[:author, :name] => [presence: true]})
end

test "map, nested with _vex" do
assert Vex.valid?(%{author: %{name: "Foo"}, _vex: %{[:author, :name] => [presence: true]}})
assert Vex.valid?(%{author: [name: "Foo"], _vex: %{[:author, :name] => [presence: true]}})

nested_errors_map = [{:error, [:author, :name], :presence, "must be present"}]
assert nested_errors_map == Vex.errors(%{author: %{name: ""}, _vex: %{[:author, :name] => [presence: true]}})

nested_errors_mixed = [{:error, [:author, :name], :presence, "must be present"}]
assert nested_errors_mixed == Vex.errors(%{author: [name: ""], _vex: %{[:author, :name] => [presence: true]}})
end

test "map, nested in Record" do
assert Vex.valid?(%NestedTestRecord{author: %{name: "Foo"}})

nested_errors = [{:error, [:author, :name], :presence, "must be present"}]
assert nested_errors == Vex.errors(%NestedTestRecord{author: %{name: ""}})
end
end