-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from solnic/17-support-multiple-types
17 support multiple types
- Loading branch information
Showing
5 changed files
with
186 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule Drops.Contract.Schema.Key do | ||
alias __MODULE__ | ||
|
||
defstruct [:path, :presence, :type, :predicates, children: []] | ||
|
||
def new(spec, attrs) do | ||
Map.merge( | ||
%Key{}, | ||
Enum.into(attrs, %{type: infer_type(spec), predicates: infer_predicates(spec)}) | ||
) | ||
end | ||
|
||
def present?(map, _) when not is_map(map) do | ||
true | ||
end | ||
|
||
def present?(_map, []) do | ||
true | ||
end | ||
|
||
def present?(map, %Key{} = key) do | ||
present?(map, key.path) | ||
end | ||
|
||
def present?(map, [key | tail]) do | ||
Map.has_key?(map, key) and present?(map[key], tail) | ||
end | ||
|
||
defp infer_type({:type, {type, _}}) do | ||
type | ||
end | ||
|
||
defp infer_type(spec) when is_list(spec) do | ||
Enum.map(spec, &infer_type/1) | ||
end | ||
|
||
defp infer_type({:coerce, {input_type, output_type}}) do | ||
{:coerce, {{infer_type(input_type), infer_predicates(input_type)}, infer_type(output_type)}} | ||
end | ||
|
||
defp infer_predicates({:coerce, {_input_type, output_type}}) do | ||
infer_predicates(output_type) | ||
end | ||
|
||
defp infer_predicates(spec) when is_list(spec) do | ||
{:or, Enum.map(spec, &infer_predicates/1)} | ||
end | ||
|
||
defp infer_predicates({:type, {type, predicates}}) when length(predicates) > 0 do | ||
{:and, [predicate(:type?, type) | Enum.map(predicates, &predicate/1)]} | ||
end | ||
|
||
defp infer_predicates({:type, {type, []}}) do | ||
[predicate(:type?, type)] | ||
end | ||
|
||
defp predicate(name, args) do | ||
{:predicate, {name, args}} | ||
end | ||
|
||
defp predicate(name) do | ||
{:predicate, {name, []}} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
defmodule Drops.Contract.TypeTest do | ||
use Drops.ContractCase | ||
|
||
describe "type/1 with a type atom" do | ||
contract do | ||
schema do | ||
%{required(:test) => type(:string)} | ||
end | ||
end | ||
|
||
test "returns success with valid data", %{contract: contract} do | ||
assert {:ok, %{test: "Hello"}} = contract.conform(%{test: "Hello"}) | ||
end | ||
|
||
test "returns error with invalid data", %{contract: contract} do | ||
assert {:error, [{:error, {:string?, [:test], 312}}]} = | ||
contract.conform(%{test: 312}) | ||
end | ||
end | ||
|
||
describe "type/1 with multiple types" do | ||
contract do | ||
schema do | ||
%{required(:test) => type([:integer, :string])} | ||
end | ||
end | ||
|
||
test "returns success with valid data", %{contract: contract} do | ||
assert {:ok, %{test: 312}} = contract.conform(%{test: 312}) | ||
assert {:ok, %{test: "Hello"}} = contract.conform(%{test: "Hello"}) | ||
end | ||
|
||
test "returns error with invalid data", %{contract: contract} do | ||
assert {:error, [{:error, {:string?, [:test], :invalid}}]} = | ||
contract.conform(%{test: :invalid}) | ||
end | ||
end | ||
|
||
describe "type/1 with multiple types and extra predicates" do | ||
contract do | ||
schema do | ||
%{required(:test) => type([:integer, {:string, [:filled?]}])} | ||
end | ||
end | ||
|
||
test "returns success with valid data", %{contract: contract} do | ||
assert {:ok, %{test: 312}} = contract.conform(%{test: 312}) | ||
assert {:ok, %{test: "Hello"}} = contract.conform(%{test: "Hello"}) | ||
end | ||
|
||
test "returns error with invalid data", %{contract: contract} do | ||
assert {:error, [{:error, {:string?, [:test], :invalid}}]} = | ||
contract.conform(%{test: :invalid}) | ||
|
||
assert {:error, [{:error, {:filled?, [:test], ""}}]} = | ||
contract.conform(%{test: ""}) | ||
end | ||
end | ||
end |