diff --git a/lib/drops/type.ex b/lib/drops/type.ex index ec78e31..5cf0fb5 100644 --- a/lib/drops/type.ex +++ b/lib/drops/type.ex @@ -273,6 +273,11 @@ defmodule Drops.Type do predicate(name, []) end + @doc false + def predicate(name, args) when name in [:in?, :not_in?] and length(args) == 1 do + {:predicate, {name, [args]}} + end + @doc false def predicate(name, args) do {:predicate, {name, args}} diff --git a/test/drops/contract/predicates_test.exs b/test/drops/contract/predicates_test.exs index 492d9ec..cf3183b 100644 --- a/test/drops/contract/predicates_test.exs +++ b/test/drops/contract/predicates_test.exs @@ -652,7 +652,26 @@ defmodule Drops.PredicatesTest do end end - describe "in?/2" do + describe "in?/2 with a single value" do + contract do + schema do + %{required(:test) => type(:string, in?: ["Hello"])} + end + end + + test "returns success when the value is in the list", %{contract: contract} do + assert {:ok, %{test: "Hello"}} = contract.conform(%{test: "Hello"}) + end + + test "returns error when the value is not in the list", %{contract: contract} do + assert_errors( + ["test must be one of: Hello"], + contract.conform(%{test: "312"}) + ) + end + end + + describe "in?/2 with multiple values" do contract do schema do %{required(:test) => type(:string, in?: ["Hello", "World"])} @@ -671,7 +690,26 @@ defmodule Drops.PredicatesTest do end end - describe "not_in?/2" do + describe "not_in?/2 with a single value" do + contract do + schema do + %{required(:test) => type(:string, not_in?: ["Hello"])} + end + end + + test "returns success when the value is not in the list", %{contract: contract} do + assert {:ok, %{test: "312"}} = contract.conform(%{test: "312"}) + end + + test "returns error when the value is in the list", %{contract: contract} do + assert_errors( + ["test must not be one of: Hello"], + contract.conform(%{test: "Hello"}) + ) + end + end + + describe "not_in?/2 with multiple values" do contract do schema do %{required(:test) => type(:string, not_in?: ["Hello", "World"])}