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

Better union #42

Merged
merged 3 commits into from
Jan 29, 2024
Merged
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: 2 additions & 2 deletions lib/drops/type/dsl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ defmodule Drops.Type.DSL do
{:type, {type, predicates}}
end

def type({:type, {type, []}}, predicates) when is_atom(type) and is_list(predicates) do
{:type, {type, predicates}}
def type({:type, {type, predicates}}, more_predicates) when is_atom(type) do
{:type, {type, predicates ++ more_predicates}}
end

def type({:cast, {input_type, cast_opts}}, output_type)
Expand Down
20 changes: 20 additions & 0 deletions lib/drops/types/sum.ex → lib/drops/types/union.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ defmodule Drops.Types.Union do
"""

defmodule Validator do
def validate(%{left: %{primitive: _} = left, right: %{primitive: _} = right}, input) do
case Drops.Type.Validator.validate(left, input) do
{:ok, value} ->
{:ok, value}

{:error, meta} = left_error ->
if is_list(meta) and meta[:predicate] != :type? do
left_error
else
case Drops.Type.Validator.validate(right, input) do
{:ok, value} ->
{:ok, value}

{:error, _} = right_error ->
{:error, {:or, {left_error, right_error}}}
end
end
end
end

def validate(%{left: left, right: right}, input) do
case Drops.Type.Validator.validate(left, input) do
{:ok, value} ->
Expand Down
5 changes: 1 addition & 4 deletions test/contract/type_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,7 @@ defmodule Drops.Contract.TypeTest do
end

test "returns error from left predicates", %{contract: contract} do
assert_errors(
["test must be filled or test must be a map"],
contract.conform(%{test: []})
)
assert_errors(["test must be filled"], contract.conform(%{test: []}))
end

test "returns errors from right predicates", %{contract: contract} do
Expand Down
5 changes: 1 addition & 4 deletions test/contract/types/custom_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,7 @@ defmodule Drops.Contract.Types.CustomTest do
contract.conform(%{test: "Hello World"})
)

assert_errors(
["test must be greater than 0 or test must be a float"],
contract.conform(%{test: -1})
)
assert_errors(["test must be greater than 0"], contract.conform(%{test: -1}))
end
end

Expand Down
117 changes: 117 additions & 0 deletions test/contract/types/union_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
defmodule Drops.Contract.Types.UnionTest do
use Drops.ContractCase

describe "a union of two primitive types" do
contract do
schema do
%{required(:test) => union([string(), integer()])}
end
end

test "returns success when left side is a success", %{contract: contract} do
assert {:ok, _} = contract.conform(%{test: "Hello"})
end

test "returns success when right side is a success", %{contract: contract} do
assert {:ok, _} = contract.conform(%{test: 312})
end

test "returns error when left side and right are failures", %{contract: contract} do
assert_errors(
["test must be a string or test must be an integer"],
contract.conform(%{test: []})
)
end
end

describe "a union of two primitive types when left side is constrained" do
contract do
schema do
%{required(:test) => union([string(size?: 5), integer()])}
end
end

test "returns success when left side is a success", %{contract: contract} do
assert {:ok, _} = contract.conform(%{test: "Hello"})
end

test "returns success when right side is a success", %{contract: contract} do
assert {:ok, _} = contract.conform(%{test: 312})
end

test "returns error when left side is a failure", %{contract: contract} do
assert_errors(["test size must be 5"], contract.conform(%{test: "Hello World"}))
end

test "returns error when left side and right are failures", %{contract: contract} do
assert_errors(
["test must be a string or test must be an integer"],
contract.conform(%{test: []})
)
end
end

describe "a union of two primitive types when right side is constrained" do
contract do
schema do
%{required(:test) => union([string(), integer(gt?: 0)])}
end
end

test "returns success when left side is a success", %{contract: contract} do
assert {:ok, _} = contract.conform(%{test: "Hello"})
end

test "returns success when right side is a success", %{contract: contract} do
assert {:ok, _} = contract.conform(%{test: 312})
end

test "returns success when right side is a failure", %{contract: contract} do
assert_errors(
["test must be a string or test must be greater than 0"],
contract.conform(%{test: -3})
)
end

test "returns error when left side and right are failures", %{contract: contract} do
assert_errors(
["test must be a string or test must be an integer"],
contract.conform(%{test: []})
)
end
end

describe "a union of two primitive types when both sides are constrained" do
contract do
schema do
%{required(:test) => union([string(size?: 5), integer(gt?: 0)])}
end
end

test "returns success when left side is a success", %{contract: contract} do
assert {:ok, _} = contract.conform(%{test: "Hello"})
end

test "returns success when right side is a success", %{contract: contract} do
assert {:ok, _} = contract.conform(%{test: 312})
end

test "returns error when left side is a failure", %{contract: contract} do
assert_errors(["test size must be 5"], contract.conform(%{test: "Hello World"}))
end

test "returns success when right side is a failure", %{contract: contract} do
assert_errors(
["test must be a string or test must be greater than 0"],
contract.conform(%{test: -3})
)
end

test "returns error when left side and right are failures", %{contract: contract} do
assert_errors(
["test must be a string or test must be an integer"],
contract.conform(%{test: []})
)
end
end
end
Loading