-
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 #41 from solnic/number-type
Add built-in number type
- Loading branch information
Showing
12 changed files
with
245 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule ProductContract do | ||
use Drops.Contract | ||
|
||
schema do | ||
%{ | ||
required(:name) => string(:filled?), | ||
required(:price) => number() | ||
} | ||
end | ||
end | ||
|
||
ProductContract.conform(%{name: "Book", price: 31.2}) | ||
|
||
ProductContract.conform(%{name: "Book", price: 31}) | ||
|
||
{:error, errors} = ProductContract.conform(%{name: "Book", price: []}) | ||
Enum.map(errors, &to_string/1) |
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
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,39 @@ | ||
defmodule Drops.Types.Number do | ||
@moduledoc ~S""" | ||
Drops.Types.Number is a struct that represents a number type | ||
that can be either an integer or a float | ||
## Examples | ||
iex> defmodule ProductContract do | ||
...> use Drops.Contract | ||
...> | ||
...> schema do | ||
...> %{ | ||
...> required(:name) => string(:filled?), | ||
...> required(:price) => number() | ||
...> } | ||
...> end | ||
...> end | ||
iex> ProductContract.conform(%{name: "Book", price: 31.2}) | ||
{:ok, %{name: "Book", price: 31.2}} | ||
iex> ProductContract.conform(%{name: "Book", price: 31}) | ||
{:ok, %{name: "Book", price: 31}} | ||
iex> {:error, errors} = ProductContract.conform(%{name: "Book", price: []}) | ||
{:error, | ||
[ | ||
%Drops.Validator.Messages.Error.Type{ | ||
path: [:price], | ||
text: "must be a number", | ||
meta: [] | ||
} | ||
]} | ||
iex> Enum.map(errors, &to_string/1) | ||
["price must be a number"] | ||
""" | ||
@doc since: "0.2.0" | ||
|
||
use(Drops.Type, union([:integer, :float])) | ||
|
||
@opts name: :number | ||
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
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,73 @@ | ||
defmodule Drops.Contract.Types.NumberTest do | ||
use Drops.ContractCase | ||
|
||
doctest Drops.Types.Number | ||
|
||
describe "number/0" do | ||
contract do | ||
schema do | ||
%{required(:test) => number()} | ||
end | ||
end | ||
|
||
test "returns success with valid data", %{contract: contract} do | ||
assert {:ok, %{test: 312}} = contract.conform(%{test: 312}) | ||
end | ||
|
||
test "returns error with invalid data", %{contract: contract} do | ||
assert_errors(["test must be a number"], contract.conform(%{test: :invalid})) | ||
end | ||
end | ||
|
||
describe "number/1 with an extra predicate" do | ||
contract do | ||
schema do | ||
%{required(:test) => number(:odd?)} | ||
end | ||
end | ||
|
||
test "returns success with valid data", %{contract: contract} do | ||
assert {:ok, %{test: 311}} = contract.conform(%{test: 311}) | ||
end | ||
|
||
test "returns error with invalid data", %{contract: contract} do | ||
assert_errors(["test must be a number"], contract.conform(%{test: :invalid})) | ||
assert_errors(["test must be odd"], contract.conform(%{test: 312})) | ||
end | ||
end | ||
|
||
describe "number/1 with an extra predicate with args" do | ||
contract do | ||
schema do | ||
%{required(:test) => number(gt?: 2)} | ||
end | ||
end | ||
|
||
test "returns success with valid data", %{contract: contract} do | ||
assert {:ok, %{test: 312}} = contract.conform(%{test: 312}) | ||
end | ||
|
||
test "returns error with invalid data", %{contract: contract} do | ||
assert_errors(["test must be a number"], contract.conform(%{test: :invalid})) | ||
assert_errors(["test must be greater than 2"], contract.conform(%{test: 0})) | ||
end | ||
end | ||
|
||
describe "number/1 with extra predicates" do | ||
contract do | ||
schema do | ||
%{required(:test) => number([:even?, gt?: 2])} | ||
end | ||
end | ||
|
||
test "returns success with valid data", %{contract: contract} do | ||
assert {:ok, %{test: 312}} = contract.conform(%{test: 312}) | ||
end | ||
|
||
test "returns error with invalid data", %{contract: contract} do | ||
assert_errors(["test must be a number"], contract.conform(%{test: :invalid})) | ||
assert_errors(["test must be even"], contract.conform(%{test: 311})) | ||
assert_errors(["test must be greater than 2"], contract.conform(%{test: 0})) | ||
end | ||
end | ||
end |