Skip to content

Commit

Permalink
Merge pull request #22 from nicksanders/add-valid
Browse files Browse the repository at this point in the history
Add valid function that returns {:ok, data} or {:error, errors}
  • Loading branch information
benwilson512 committed Feb 19, 2016
2 parents df5d0dc + 1f0ec81 commit 704c31b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/vex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ defmodule Vex do
errors(data, settings) |> length == 0
end

def validate(data) do
validate(data, Vex.Extract.settings(data))
end
def validate(data, settings) do
case errors(data, settings) do
errors when length(errors) > 0 -> {:error, errors}
_ -> {:ok, data}
end
end

def errors(data) do
errors(data, Vex.Extract.settings(data))
end
Expand Down
8 changes: 8 additions & 0 deletions test/vex_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ defmodule VexTest do
assert Vex.valid?(user)
end

test "validate returns {:ok, data} on success" do
assert {:ok, [name: "Foo"]} = Vex.validate([name: "Foo"], name: [length: [min: 2, max: 10], format: ~r(^Fo.$)])
end

test "validate returns {:error, errors} on error" do
assert {:error, [{:error, :name, :length, "must have a length of at least 4"}]} = Vex.validate([name: "Foo"], name: [length: [min: 4]])
end

test "validator lookup by structure" do
validator = Vex.validator(:criteria, [TestValidatorSourceByStructure])
assert validator == TestValidatorSourceByStructure.Criteria
Expand Down

0 comments on commit 704c31b

Please sign in to comment.