Skip to content

Commit

Permalink
Add basic doctest for custom types
Browse files Browse the repository at this point in the history
  • Loading branch information
solnic committed Jan 31, 2024
1 parent bec6897 commit 804993a
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 31 deletions.
30 changes: 0 additions & 30 deletions examples/types/custom-01.ex

This file was deleted.

18 changes: 18 additions & 0 deletions examples/types/custom-01.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule Email do
use Drops.Type, string()
end

defmodule UserContract do
use Drops.Contract

schema do
%{
required(:email) => Email
}
end
end
UserContract.conform(%{email: "[email protected]"})
{:error, errors} = UserContract.conform(%{email: 1})
Enum.map(errors, &to_string/1)
[%{type: type}]= UserContract.schema().keys
type
17 changes: 17 additions & 0 deletions examples/types/custom-02.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule Email do
use Drops.Type, string(:filled?)
end
defmodule UserContract do
use Drops.Contract

schema do
%{
required(:email) => Email
}
end
end
UserContract.conform(%{email: "[email protected]"})
{:error, errors} = UserContract.conform(%{email: ""})
Enum.map(errors, &to_string/1)
[%{type: type}]= UserContract.schema().keys
type
86 changes: 85 additions & 1 deletion lib/drops/type.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,91 @@
defmodule Drops.Type do
@moduledoc ~S"""
Type behaviour
Type behaviour and definition macros.
## Defining a basic primitive type
defmodule Email do
use Drops.Type, string()
end
iex> defmodule UserContract do
...> use Drops.Contract
...>
...> schema do
...> %{
...> required(:email) => Email
...> }
...> end
...> end
iex> UserContract.conform(%{email: "[email protected]"})
{:ok, %{email: "[email protected]"}}
iex> {:error, errors} = UserContract.conform(%{email: 1})
{:error,
[
%Drops.Validator.Messages.Error.Type{
path: [:email],
text: "must be a string",
meta: [predicate: :type?, args: [:string, 1]]
}
]}
iex> Enum.map(errors, &to_string/1)
["email must be a string"]
iex> [%{type: type}]= UserContract.schema().keys
[
%Drops.Types.Map.Key{
path: [:email],
presence: :required,
type: %Email{
primitive: :string,
constraints: [predicate: {:type?, :string}],
opts: []
}
}
]
## Defining a custom constrained type
defmodule FilledEmail do
use Drops.Type, string(:filled?)
end
iex> defmodule UserContract do
...> use Drops.Contract
...>
...> schema do
...> %{
...> required(:email) => FilledEmail
...> }
...> end
...> end
iex> UserContract.conform(%{email: "[email protected]"})
{:ok, %{email: "[email protected]"}}
iex> {:error, errors} = UserContract.conform(%{email: ""})
{:error,
[
%Drops.Validator.Messages.Error.Type{
path: [:email],
text: "must be filled",
meta: [predicate: :filled?, args: [""]]
}
]}
iex> Enum.map(errors, &to_string/1)
["email must be filled"]
iex> [%{type: type}]= UserContract.schema().keys
[
%Drops.Types.Map.Key{
path: [:email],
presence: :required,
type: %FilledEmail{
primitive: :string,
constraints: {:and,
[predicate: {:type?, :string}, predicate: {:filled?, []}]},
opts: []
}
}
]
"""
@moduledoc since: "0.2.0"

alias __MODULE__
alias Drops.Type.Compiler
Expand Down
13 changes: 13 additions & 0 deletions test/drops/type_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Drops.TypeTest do
use Drops.ContractCase

defmodule Email do
use Drops.Type, string()
end

defmodule FilledEmail do
use Drops.Type, string(:filled?)
end

doctest Drops.Type
end

0 comments on commit 804993a

Please sign in to comment.